How to access Keys/Names in nested Dictionaries - list

I have following nested dictionary:
nested_dict = { 'dict1': {'fun1' : {"a" :1,
"b" :2},
'fun2' : {"c" :3,
"d" :4}
'dict2': {'fun3' : {"e" :5,
"f" :6},
'fun4' : {"g" :7,
"h" :8}}}
How can I extract e.g. name/key "dict2" of the outer dictionary and the key/name "fun4" of the nested dictionary and store them to the other variables "outerDictName" and "innerDictName"?
I checked couple of post to this topics, but couldn't figure out the correct way/syntax.
Thx,
Toby

keys1=list(nested_dict.keys())
keys2=list(nested_dict["dict1"].keys())

Related

RethinkDB Multiple emits in Map

I've been trying out RethinkDB for a while and i still don't know how to do something like this MongoDB example:
https://docs.mongodb.com/manual/tutorial/map-reduce-examples/#calculate-order-and-total-quantity-with-average-quantity-per-item
In Mongo, in the map function, I could iterate over an array field of one document, and emit multiple values.
I don't know how to set the key to emit in map or return more than one value per document in the map function.
For example, i would like to get from this:
{
'num' : 1,
'lets': ['a','b,'c']
}
to
[
{'num': 1, 'let' : 'a' },
{'num': 1, 'let' : 'b' },
{'num': 1, 'let' : 'c' }
]
I'm not sure if I should think this differently in RethinkDB or use something different from map-reduce.
Thanks.
I'm not familiar with Mongo; addressing your transformation example directly:
r.expr({
'num' : 1,
'lets': ['a', 'b', 'c']
})
.do(function(row) {
return row('lets').map(function(l) {
return r.object(
'num', row('num'),
'let', l
)
})
})
You can, of course, use map() instead of do() (in case not a singular object)

Grabbing a dictionary given just a few keys and values

Suppose I have a list of dictionaries, all of which have the same keys. An instance of such a dictionary in the list might look like:
dict = {"Height": 6.0, "Weight": 201.5, "Name": "John", "Status": "Married"}
Given only a few (key, value) pairs, I want to extract all dictionaries satisfying those pairs. For example, if I have
attributes = {"Height": 5.5, "Name": "John"}
I want to extract all dictionaries whose height value is greater than or equal to 5.5 AND whose name value is John.
I'm able to write the code that can satisfy one OR the other, but dealing with mixed types (float and string) is throwing me off, so my AND operator is being confused I guess. The problem part of my code, for example, is:
for option in attributes.keys():
if dict[option] == attributes[option] and dict[option] >= attributes[option]
print dict
If you have multiple different condition you have to do all of them, but instead of using and you can use all built-in function and a function for filtering the dictionaries, then use it within a list comprehension or filter function in order to get the expected dictionaries.
from operator import itemgetter
option1, option2, option3 = itemgetter(key1, key2, key3)(attributes)
def filter_func(temp_dict):
# key1, key2, key3 have been defined already (keys in attributes)
val1, val2, val3 = itemgetter(key1, key2, key3)(temp_dict)
return all(option1 == val1, option2 => val2, option3 => val3)
filtered_result = filter(filter_func, list_of_dictionaries)
Also note that if it's possible that the dictionaries with your list don't have all the specified keys, the itemgetter might raise an KeyError for getting ride of that you can use dict.get attribute by passing a default value to it (based on your need).
For example for val3 you can use temp_dict.get(key3, 0).

dictionary: Unique relative values where values are of list type

I am getting the output of word2vec_basic.py in the following format
Nearest to key1 : node1, node2, node3 ..
Nearest to key2 : node2, node4, node5 ..
This implies that node2 is comparatively closer to key2 over key1 (Please correct me if I am wrong, as I am newbie here)
It would be great if I get the output in the following format
Nearest to key1 : node1, node3 , node6..
Nearest to key2 : node2, node4, node5 ..
That is, consider only the closest neighbor for clustering.
Suggestions for the same?
I am maintaining a python dictionary for the same of the following format:
{
key1: [node1,node2,node3],
key2: [node2,node4,node5]
}
But I required,
{
key1: [node1,node3,node6],
key2: [node2,node4,node5]
}
And for the above dictionary, I will be needing
Nearest to key1 : node1, node3 , node6..
Nearest to key2 : node2, node4, node5 ..
Could we do this in tensorflow itself, or should I define a function which takes dictionary as input and give me the required output?
For eg:
If we have a python dictionary of the following format:
{
a: ["abc","bcd","def"],
b: ["def","xyz"]
}
Here the values are list. I am looking for the following format from the above input:
{
a: ["abc","bcd"],
b: ["def","xyz"]
}
Suggestions are welcome on how I could achieve it.
Also, are there any python in built functions which could help me to reach the above output format?
dicts are unordered so which dupe gets removed is not guaranteed but you can keep a set of elements seen so far as you iterate over the items, updating/removing elements from the list/value if it has already been seen:
d = {
"a": ["abc","bcd","def"],
"b": ["def","xyz"]
}
seen = set()
for k,v in d.items():
d[k] = [seen.add(ele) or ele for ele in v if ele not in seen]
print(d)
This could output:
{'b': ['def', 'xyz'], 'a': ['abc', 'bcd']}
Or:
d = { "a": ["abc","bcd","def"], "b": ["xyz"]}
It completely depends on which key you hit first.
As you can see from this top answer with 436 upvotes, the removal logic is efficient and it maintains the order if required. To also to avoid the set.add lookup each time as in the link, you can set seen_add = seen.add and use seen._add(ele) in place of seen.add.
Since dictionaries entries in Python are unordered, you need to first build a separate dictionary keyed by node recording each list (or sequence) it's in as well as its index in that list so relative distances in each list can be compared to one another. After that's done, it can be referenced to determine whether each node should stay in each list it is in or not by making a second pass through the dictionary's contents.
d = {
"a": ["abc", "bcd", "def"],
"b": ["def", "xyz"]
}
def check_usage(k, elem_usage):
if len(elem_usage) == 1: # unique?
return True
else:
index = elem_usage[k] # within this elem's seq
for key,value in elem_usage.items():
if key != k:
if value < index:
return False
else:
return True
usage = {}
for key in d: # build usage dictionary
for index, item in enumerate(d[key]):
usage.setdefault(item, {})[key] = index
for k,seq in d.items():: # remove nodes that are closer in other lists
d[k] = [elem for elem in seq if check_usage(k, usage[elem])]
# display results
print('{')
for k in sorted(d):
print(' {!r}: {},'.format(k, d[k]))
print('}')
Output:
{
'a': ['abc', 'bcd'],
'b': ['def', 'xyz'],
}

how to convert all the values in a list into keys of a Dictionary in Py2.7

I've below lists,
lists=[ ['arya','egg','milk','butter','bread'],
['Jon','butter','pastrie','yogurt','beer'],
['bran','beer','milk','banana','apples'],]
Each list has values in which the first value is the name of a person and rest of all are some food items. I've a task where I've to create a dictionary with these food items as keys and the person as a value as shown below
dict = { 'egg' : set(['arya']),
'milk': set(['arya','bran']),
'butter' : set(['arya','jon']),
'bread' : set(['arya']),
'pastrie' : set(['jon']),
'milk' : set(['bran'])
} # few keys omitted
This is what I did and stopped, dont know how to proceed further,
food,person = [],[]
for i in lists:
food.append(i[1:])
person.append(i[0])
I was able to seperate the first value of each list and append it to a list
and same with food.
Dont know how to proceed further.
started learning python, Any input is highly helpful. kindly share one or two lines of explanation to enlighten this newbie !
Thank you so much.
Using dictionary method setdefault is helpful here.
You of course don't nee to set the slices to a variable, but it makes it easier to read.
d = {}
for l in lists:
name = l[0]
items = l[1:]
for item in items:
d.setdefault(item, set()).add(name)
Use a collections.defaultdict:
lists = [['arya', 'egg', 'milk', 'butter', 'bread'],
['Jon', 'butter', 'pastrie', 'yogurt', 'beer'],
['bran', 'beer', 'milk', 'banana', 'apples']]
from collections import defaultdict
d = defaultdict(set)
for sub in lists:
for v in sub[1:]:
d[v].add(sub[0])
print(d)
Output:
defaultdict(<class 'set'>,
{'bread': {'arya'}, 'yogurt': {'Jon'}, 'beer': {'Jon', 'bran'},
'banana': {'bran'}, 'butter': {'Jon', 'arya'}, 'milk': {'arya',
'bran'}, 'pastrie': {'Jon'}, 'egg': {'arya'}, 'apples': {'bran'}})
For python3 the syntax is a little nicer:
from collections import defaultdict
d = defaultdict(set)
for name, *rest in lists:
for v in rest:
d[v].add(name)

Merging python dictionaries differently

I've python dictionaries within a list as follows
[{"item1": {"item2": "300", "item3" : "10"}},
{"item2": { "item4": "90", "item5": "400" }},
{"item5": {"item6": "16"}},
{"item3": {"item8": "ava", "item1" : "xxx","item5": "400"}}]
And I want to construct a dictionary as follows
{
"item1" : {
"item2": "300",
"item4": "90",
"item5": "400",
"item6": "16",
"item3" : "10",
"item8": "ava"
}
Traversing method:
1) Starting with item1 => is a dict with two keys. Add it to the new dict.
2) Then take the first key in the first dict and check if there is any dictionary with this key. item2 is again a dict with two keys and hence add those keys to the new dict.
3) Repeat the same for the keys in item2 until nothing is there to traverse down. While traversing if there is already traversed dict found skip that. For eg, in the last dict item3, we have item5 which is already traversed and added to new dict. So we can skip traversing this key.
4) Repeat step 2, for second key in item1 (Has to be repeated depends upon the number of keys in first dict.)
I know this is more complex. Is there any possibility to achieve this?