logo
down
shadow

Unable to reach array->dict->set


Unable to reach array->dict->set

By : just TDUBB it
Date : November 22 2020, 02:42 PM
seems to work fine You cannot access a variable through a function. Instead in this case I would suggest you to return the set or store it in your class for later access:
code :
class Data: UIView {
    var set = Set<String>()

    func myFunc() -> Set<String> {
        ...
        let set1 = arr[0]["items"] as? Set<String> ?? Set<String>()

        // store it
        set = set1

        // or return it
        return set1
    }
}

// access
let data = Data()

// fist way
data.myFunc()
let newSet = data.set

// second way
let newSet = data.myFunc()


Share : facebook icon twitter icon
Create dict using a grouping column in an array and assigning the remaining columns to values of the dict

Create dict using a grouping column in an array and assigning the remaining columns to values of the dict


By : ElRouquin
Date : March 29 2020, 07:55 AM
With these it helps You can simply built them with defaultdict :
code :
d=collections.defaultdict(list)
for k,*v in s1 : d[k].append(list(v))
defaultdict(list,
            {1: [['R', 4], ['D', 3], ['I', 10], ['K', 0.0]],
             2: [['R', 11], ['D', 13], ['I', 1], ['K', 6]],
             3: [['R', 12], ['D', 17], ['I', 23], ['K', 10]]}) 
d=collections.defaultdict(dict)
for k1,k2,v in s1 : d[k1][k2]=v 

#defaultdict(dict,
#       {1: {'D': 3, 'I': 10, 'K': 0.0, 'R': 4},
#        2: {'D': 13, 'I': 1, 'K': 6, 'R': 11},
#        3: {'D': 17, 'I': 23, 'K': 10, 'R': 12}})

In [67]: d[2]['K']
Out[67]: 6
update second level dict array object using a reference dict in python

update second level dict array object using a reference dict in python


By : Giorgio Maggiolo
Date : March 29 2020, 07:55 AM
wish help you to fix your issue All you really need is two loops over your data and one loop over your updates (no nested loops). The idea is to create a dict or records that maps substepnumber to substepparams. (Note that this assumes no repetition of substepparams. This case can be handled with a bit extra work.)
For each update, you can access the respective substepnumber in amortized O(1) time and update each dict in the associated substepparams list.
code :
data = [
    {
        "substepheading": "Heading 1",
        "substepnumber": "1",
        "substepparams": [{"name": "upload file", "paramselected": "no"}],
    },
    {
        "substepheading": "Heading 2",
        "substepnumber": "2",
        "substepparams": [{"name": "clean data", "paramselected": "no"}],
    },
]

# converted substepnumber to str in order to match the data format.
updates = [{"substepnumber": "2", "substepparams": [{"name": "clean room"}]}]

records = {d["substepnumber"]: d["substepparams"] for d in data}
for num, params in ((d["substepnumber"], d["substepparams"]) for d in updates):
    [dct.update(upd) for dct in records[num] for upd in params]


[
    {
        "substepheading": "Heading 1",
        "substepnumber": "1",
        "substepparams": [{"name": "upload file", "paramselected": "no"}],
    },
    {
        "substepheading": "Heading 2",
        "substepnumber": "2",
        "substepparams": [{"name": "clean room", "paramselected": "no"}],
    },
]
Python : Find in an array of dict the element that match partially another dict

Python : Find in an array of dict the element that match partially another dict


By : Ramon van Dongen
Date : March 29 2020, 07:55 AM
this one helps. I have a dict myDict that look like this : , You can use a list comprehension, like this:
code :
ls=[d for d in myArray if d["surname"]==myDict["surname"] and d["name"]==myDict["name"]]
How to transform a dict array with multiple key values into a dict with single key value?

How to transform a dict array with multiple key values into a dict with single key value?


By : Johan Borchers
Date : March 29 2020, 07:55 AM
it helps some times I have the following input data , Creating dictionary with dict comprehensions -
code :
options_dict = {i['id']:i['label'] for i in options_d}
print(options_dict)
    {36: 'Angular',
     37: 'Java',
     38: 'PHP',
     39: 'Python',
     40: 'C#',
     41: 'C',
     42: '.NET',
     43: 'Ruby',
     44: 'Rails',
     45: 'OS-Linux',
     55: 'Maschinenbau',
     56: 'Automotive',
     57: 'Engineering'}
How to combine multiple dict variables into an array based on regex of dict variable names

How to combine multiple dict variables into an array based on regex of dict variable names


By : user2648815
Date : March 29 2020, 07:55 AM
it helps some times You're very close, but as you point out, the join method on a string is for joining strings. You want to append lists, which you accomplish with the + operator.
There are also a few other issues:
code :
hostvars['localhost'] | select('match', '^my_var_*')
[
  "my_var_1",
  "my_var_2"
]
---
- hosts: localhost
  gather_facts: false
  tasks:
    - name: set facts on localhost
      set_fact:
        my_var_1:
            element1: value11
            element2: value12
        my_var_2:
            element1: value21
            element2: value22

- hosts: localhost
  gather_facts: false
  tasks:
    - name: merge vars into my_var
      set_fact:
        my_var: "{{ hostvars['localhost']|dict2items|selectattr('key', 'match', '^my_var_')|map(attribute='value')|list }}"

    - name: show content of my_var
      debug:
        var: my_var
TASK [show content of my_var] ************************************************************************************
ok: [localhost] => {
    "my_var": [
        {
            "element1": "value11", 
            "element2": "value12"
        }, 
        {
            "element1": "value21", 
            "element2": "value22"
        }
    ]
}
TASK [show content of my_var] *****************************************************************************************
ok: [localhost] => {
    "my_var": [
        {
            "key": "my_var_1", 
            "value": {
                "element1": "value11", 
                "element2": "value12"
            }
        }, 
        {
            "key": "my_var_2", 
            "value": {
                "element1": "value21", 
                "element2": "value22"
            }
        }
    ]
}
Related Posts Related Posts :
  • How to close this bottom bar in iOS9 simulator?
  • Is there a way to change the string value of an NSTextView container in Swift?
  • Sources referencing each other in XCode Playgrounds
  • Get stuck at installation of Cocoapod
  • Errors when building with Xcode 7, Unity 5.2.3f1, iOS 9.1 base SDK
  • Try and catch around unwrapping line? Swift 2.0, XCode 7
  • buddy build - There was an internal error
  • Soundex returns 0 for every character
  • Access container view child properties swift
  • Folding variables list Xcode
  • How to Place Buttons Side-By-Side Xcode WatchKit
  • Assigning the Outlet from Different Class? (Core Plot)
  • How to convert string to NSDate that has extraneous characters (swift)?
  • Cannot convert value of type string to expected argument type soundex
  • How to get background images to scale to the same size in Xcode Autolayout?
  • Why is there no autocompletion for block arguments?
  • Why does the first navigation controller takes precedence?
  • Upgrading existing applications manually MobileFirst Platform Foundation 7.1
  • Redundant conformance of 'NSDate' to protocol 'Comparable' Swift
  • Swift Error: EXE_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
  • Xcode shortcut for last editing position
  • Cannot delete mac alias file
  • Xcode run button becomes analyze button
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co