Say that I have a spinner that is populated by
var list = listOf("One", "Two", "Three", "Four", "Five")
How would I access this list if I wanted to print the contents/dropdown list of the spinner?
I would want an output that's similar to how a list is outputted when the entire thing is printed, like:
['One', 'Two', 'Three', 'Four', 'Five']
UI elements like Spinner are not typically used as the owner of data. That would violate the principle of separation of concerns. You should hang onto your array or list reference that you passed to the Spinner and work with it directly. You can call joinToString() on it.
Related
I have the following dictionary:
{Jason:{vegetables:{broccoli, carrot}}, Bob:{vegetables:{broccoli}}}
How can I map the value "potato" to Bob so that it appears in vegetables? Like this:
{Jason:{vegetables:{broccoli, carrot}}, Bob:{vegetables:{broccoli, potato}}}
Thanks!
What you posted could be valid Python (if Jason, Bob etc are variables) but you likely are working with strings and lists so I'm going to assume the following structure:
my_dict = {
'Jason': {'vegetables': ['broccoli', 'carrot']},
'Bob': {'vegetables': ['broccoli']}
}
In that case, the vegetables is a list to which you can append items:
my_dict['Bob']['vegetables'].append('potato')
If you really meant the vegetables to be a set (indicated by using curly braces), you would use:
my_dict['Bob']['vegetables'].add('potato')
I have a dictionary of dictionary, which contains values as a list. I do not know how to access these values one by one
I don't know how to access these values one after the other, so really have not tried any method.
my_dictionary = {0: {'question': [1,2,3,4], 'answer': 0},
1: {'question': [5,6,7,8], 'answer': 0}
}
my_question_list = [5,9,4,3]
I want to compare every element in my_question_list with every element in key "question" of 0th key of my_dictionary.
I simply don't know how to access those values stored in a dictionary of dictionary, and then compare it with the elements of the list.
You can access the first dictionary (with 0 key) in my_dictionary , using my_dictionary[0].
You can also access the value of the question key, using
my_dictionary[0]['question'].
You can access the first element of the array by using my_dictionary[0]['question'][0].
I have looked but not found what I am looking for -- which is a Pythonic way of retrieving different parts of the tree knowing something at the lower levels.
I'm pretty sure it is because I am not asking the question correctly.
I have dictionary. The dictionary contains many things, among them a list of dictionaries. Those dictionaries have data, among them a list of dictionaries.
aa = {"ab": 23, # aa is a dictionary
"ac": "example",
"ad": [{"ad_a": 37, # ad is a list of dictionaries
"ad_b": "some text",
"ad_c": [{"ad_c_a": 49, # ad_c is a list of dictionaries
"ad_c_b": "some other text",
"the key": "THE IMPORTANT THING",
"ad_c_d": "more stuff"
},
{"bd_c_a": 49,
"bd_c_b": "some other text",
"bd_c_c": "some other text",
"bd_c_d": "more stuff"
}
{another dictionary},
]
},
{another dictionary},
{another dictionary}]
}
Currently, I loop through the various levels dictionaries until I find THE IMPORTANT THING is in aa["ad"]["ad_c"]
That tell me which list element of ad_c I am in. Knowing that, what is the Pythonic way to retrieve the values from aa["ad"]["ad_c"]["ad_c_a"] and other items in the list element?
I don't know where I'll end up in ad until I've done the looping, but after THE IMPORTANT THING is found, I do.
Just writing this down has given me some ideas on what to try, but I'll post it anyway in case I don't find the answer right away.
-Craig
a = {0:[[1,2,3], [1,3,4,5]]}
print([1,2,3] in a.values())
I get False. Because this list is in values I need True. Is it possible to check all lists in nested list as a value in dictionary? Maybe without loops?
Since you're using python3 you can do this:
[1,2,3] in list(a.values())[0]
a.values() returns a dictionary view. dictionary views
Then you can wrap the dictionary view into list but this list will contain only one element which can be accessed by index 0. However, if your dictionary contains several keys and corresponding values then list(a.values()) will contain the same number of elements (values mapped to keys) as keys in the dictionary.
Note that when you use some_value in some_collection construct and don't use loops explicitly it will still iterate through the collection.
Hi I'm new to Groovy and have been trying this out but could not come up with a correct solution.
Basically I have a list of objects that i would need to correlate part of a specific field and put it in a map with a transformed key. Given example below, I need correlate the values of the third field by the first four characters (e.g. key3,key4) and put them in a map. So all key3 objects and key4 objects in a separate map and combine them in 1 map with key3 and key4 as the keys and their original values in a list.
Foo[] foo = [
["field1a", "field2a", "key3a"],
["field1b", "field2b", "key3b"],
["field1c", "field2c", "key4c"]
]
into
result = [
"key3":[
["field1a", "field2a", "key3a"],
["field1b", "field2b", "key3b"]
],
"key4":[
["field1c", "field2c", "key4c"]
]
]
So far i've been able to get the unique keys by using a combination of collect(), substring() and unique(), but i am unable to build the map properly. I've used collectEntries() but it only creates a map of the object and not a map of lists.
If anyone can point me in the right direction it would really be a big help. Thanks!
Using groupBy.
assert foo.groupBy { it[-1][0..-2] } == [
key3:[
['field1a', 'field2a', 'key3a'],
['field1b', 'field2b', 'key3b']
],
key4:[
['field1c', 'field2c', 'key4c']
]
]
Explanation:
Group by the third/last element in the list it[-1] but only consider the substring key3, hence it[-1][0..-2]