Get a string list from a word - python

I'm trying to invert items of a dictionary containing strings as key and string list as value:
dico = {'key1': [],
'key2': [],
'key3': ['value1', 'value1'],
'key4': ['value2', 'value2'],
'key5': ['value3'],
'key6': ['value1', 'value2', 'value3']}
new_dict = {}
for key, values in dico.items():
if values:
for value in values:
try:
if key not in new_dict[value]:
new_dict[value].append(key)
except KeyError:
new_dict[values[0]] = list(key)
else:
print('ERROR')
Here's the result expected:
#Expected
new_dict = {'value1': ['key3', 'key6'],
'value2': ['key4', 'key6'],
'value3': ['key5', 'key6']}
#Reality
new_dict = {'value1': ["k", "e", "y", "3", "k", "e", "y", "6"],
'value2': ["k", "e", "y", "4", "k", "e", "y", "6"],
'value3': ["k", "e", "y", "5", "k", "e", "y", "6"]}
I noticed if I change that:
new_dict[values[0]] = list(key)
by that:
new_dict[values[0]] = []
new_dict[values[0]].append(key)
It actually works but is there another way to do it in one line ?

You are turning your keys to lists:
new_dict[values[0]] = list(key)
That'll produce a list with individual characters. Use a list literal instead:
new_dict[values[0]] = [key]
You can use the dict.setdefault() method to handle missing keys in new_dict to simplify your code. It looks like you want to produce sets instead; sets track unique values and saves you having to do explicit tests for duplicates.
for key, values in dico.items():
for value in values:
new_dict.setdefault(value, set()).add(key)
You can always turn those sets back to lists afterwards:
new_dict = {key: list(values) for key, values in new_dict.items()}
Demo:
>>> dico = {'key1': [],
... 'key2': [],
... 'key3': ['value1', 'value1'],
... 'key4': ['value2', 'value2'],
... 'key5': ['value3'],
... 'key6': ['value1', 'value2', 'value3']}
>>> new_dict = {}
>>> for key, values in dico.items():
... for value in values:
... new_dict.setdefault(value, set()).add(key)
...
>>> new_dict
{'value3': set(['key6', 'key5']), 'value2': set(['key6', 'key4']), 'value1': set(['key3', 'key6'])}
>>> {key: list(values) for key, values in new_dict.items()}
{'value3': ['key6', 'key5'], 'value2': ['key6', 'key4'], 'value1': ['key3', 'key6']}

Iterate every item from main dico dictionary.
Check if value is present or not.
Iterate every item from value.
Use set method to remove duplicate values.
Add to new_dict dictionary where value is key and key is list value.
code:
dico = {'key1': [],
'key2': [],
'key3': ['value1', 'value1'],
'key4': ['value2', 'value2'],
'key5': ['value3'],
'key6': ['value1', 'value2', 'value3']}
new_dict = {}
for key, values in dico.items():
if values:
for value in set(values):
try:
new_dict[value].append(key)
except:
new_dict[value] = [key]
import pprint
pprint.pprint(new_dict)
Output:
$ python test.py
{'value1': ['key3', 'key6'],
'value2': ['key6', 'key4'],
'value3': ['key6', 'key5']}

Related

Creating nested dictionary from two lists one of which contains dictionaries

Very similar to this question but with an added caveat.
I have two lists identical in length. One contains my keys, and the other contains a list of dictionaries belonging to said keys. Example below (yes the Nones are intentional)
keys = ['key1', 'key2, 'key3']
vals = [[{'subkey1': 'val1', 'subkey2': 'val2'}], [{'subkey1: 'val2'},None],[{'subkey1':'val3'}, {'subkey3':'val1}, None, None]]
What I'd like to do is match each dictionary in each list to a key to create a nested dict.
So something like below:
final = {'key1':{'subkey1': 'val1', 'subkey2': 'val2'}, 'key2':{'subkey1': 'val2'}, 'key3':{'subkey1':'val3'}, {'subkey3':'val1'}}
I know we can use dictionary comprehension to do this. I created a dictionary of empty dicts.
s = {}
for i in keys:
for v in vals:
s[i] = {}
break
Then I wrote the following to iterate through the list of dicts I'd like to pair up accounting for the non dictionary values in my list.
x = 0
while x < len(keys):
for i in keys:
for element in vals[x]:
if isinstance(element, dict) == True:
s[str(i)].append(element)
else:
print('no')
x=x+1
However when I run this, I get AttributeError: 'NoneType' object has no attribute 'append'
How can I append to a dictionary using this for loop?
For the less readable dictionary comprehension solution
keys = ['key1', 'key2', 'key3']
vals = [
[{'subkey1': 'val1', 'subkey2': 'val2'}],
[{'subkey1': 'val2'}, None],
[{'subkey1': 'val3'}, {'subkey3':'val1'}, None, None]
]
s = {
k: {
sk: sv for d in (x for x in v if x is not None) for sk, sv in d.items()
} for k, v in zip(keys, vals)
}
Gives
{'key1': {'subkey1': 'val1', 'subkey2': 'val2'},
'key2': {'subkey1': 'val2'},
'key3': {'subkey1': 'val3', 'subkey3': 'val1'}}
This can be done fairly easily using a defaultdict:
from collections import defaultdict
keys = ['key1', 'key2', 'key3']
vals = [
[{'subkey1': 'val1', 'subkey2': 'val2'}],
[{'subkey1': 'val2'},None],
[{'subkey1':'val3'}, {'subkey3':'val1'}, None, None]
]
final = defaultdict(dict)
for idx, key in enumerate(keys):
for d in vals[idx]:
if d is not None:
final[key].update(d)
Output:
defaultdict(<class 'dict'>, {
'key1': {'subkey1': 'val1', 'subkey2': 'val2'},
'key2': {'subkey1': 'val2'},
'key3': {'subkey1': 'val3', 'subkey3': 'val1'}
})
can't use append on a dict
try this maybe?
keys = ['key1', 'key2', 'key3']
vals = [[{'subkey1': 'val1', 'subkey2': 'val2'}], [{'subkey1': 'val2'},None],[{'subkey1':'val3'}, {'subkey3':'val1'}, None, None]]
s = {}
for i in keys:
s[i] = {}
for i, key in enumerate(keys):
for element in vals[i]:
if isinstance(element, dict) == True:
s[str(key)].update(element)
print(s)
Output:
{'key1': {'subkey1': 'val1', 'subkey2': 'val2'}, 'key2': {'subkey1': 'val2'}, 'key3': {'subkey1': 'val3', 'subkey3': 'val1'}}

removing all the keys with the same value in dictionary

I want to get a new dictionary by removing all the keys with the same value (keys are differents, values are the same)
For example: Input:
dct = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
expected output:
{'key2': [1, 2, 6]}
key1 and key3 was deleted because they shared same values.
I have no idea about it?
You can do this by creating a dictionary based on the values. In this case the values are lists which are not hashable so convert to tuple. The values in the new dictionary are lists to which we append any matching key from the original dictionary. Finally, work through the new dictionary looking for any values where the list length is greater than 1 - i.e., is a duplicate. Then we can remove those keys from the original dictionary.
d = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
control = {}
for k, v in d.items():
control.setdefault(tuple(v), []).append(k)
for v in control.values():
if len(v) > 1:
for k in v:
del d[k]
print(d)
Output:
{'key2': [1, 2, 6]}
I created a list of counts which holds information about how many times an item is in the dictionary. Then I copy only items which are there once.
a = {"key1": [1,2,3], "key2": [1,2,6], "key3": [1,2,3]}
# find how many of each item are there
counts = list(map(lambda x: list(a.values()).count(x), a.values()))
result = {}
#copy only items which are in the list once
for i,item in enumerate(a):
if counts[i] == 1:
result[item] = a[item]
print(result)
As given by the OP, the simplest solution to the problem:
dct = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
print({k:v for k, v in dct.items() if list(dct.values()).count(v) == 1})
Output:
{'key2': [1, 2, 6]}
One loop solution:
dict_ = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
key_lookup = {}
result = {}
for key, value in dict_.items():
v = tuple(value)
if v not in key_lookup:
key_lookup[v] = key
result[key] = value
else:
if key_lookup[v] is not None:
del result[key_lookup[v]]
key_lookup[v] = None
print(result)
Output:
{'key2': [1, 2, 6]}
dct = {'key1' : [1,2,3], 'key2': [1,2,6], 'key3': [1,2,3]}
temp = list(dct.values())
result = {}
for key, value in dct.items():
for t in temp:
if temp.count(t) > 1:
while temp.count(t) > 0:
temp.remove(t)
else:
if t == value:
result[key] = value
print(result)
Output:
{'key2': [1, 2, 6]}

How to remove duplicate from list of values

I have list of dictionary
dictio =[{'key1':'value1'}, {'key1':'value2'}, {'key1':'value1'}, {'key2':'value4'}, {'key2':'value5'}]
from collections import defaultdict
result = defaultdict(list)
for subd in dictio:
for k, v in subd.items():
result[k].append(v)
result
My output
While appending 'value1' appending to 'key1' which is not required
defaultdict(list,
{'key1': ['value1', 'value2', 'value1'],
'key2': ['value4', 'value5']})
My Expected
`defaultdict(list,
{'key1': ['value1', 'value2'],
'key2': ['value4', 'value5']})`
You can turn your list of dict into a set and then in a list again, a set would remove all duplicates.
dictio = list(set([{'key1':'value1'}, {'key1':'value2'}, {'key1':'value1'}, {'key2':'value4'}, {'key2':'value5'}]))
You can achieve that very simply, just check if the element is already in:
dictio =[{'key1':'value1'}, {'key1':'value2'}, {'key1':'value1'}, {'key2':'value4'}, {'key2':'value5'}]
from collections import defaultdict
result = defaultdict(list)
for subd in dictio:
for k, v in subd.items():
if v not in result[k]:
result[k].append(v)
Instead of list use set
Ex:
dictio =[{'key1':'value1'}, {'key1':'value2'}, {'key1':'value1'}, {'key2':'value4'}, {'key2':'value5'}]
from collections import defaultdict
result = defaultdict(set)
for subd in dictio:
for k, v in subd.items():
result[k].add(v)
result
# --> defaultdict(<class 'set'>, {'key1': {'value2', 'value1'}, 'key2': {'value4', 'value5'}})
Try this example from https://www.w3schools.com/python/python_howto_remove_duplicates.asp
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)

Replace first dictionary value with value from second if blank

I would like dict3 to match dict2 after running it through this loop. It doesn't have to be a loop if there is an easier way.
dict1={'key1' : 'val1', 'key2' : ''}
dict2={'key1' : 'val1', 'key2' : 'val2'}
dict3=dict1
#pseudocode
for key in dict1.keys():
if value is not None:
#no need to do anything
else:
dict3[value] = dict2[value]
What I would like is for dict3 to contain keys and values matching dict2.
I believe you need a dict comprehension with .copy
Ex:
dict1 = {'key1' : 'val1', 'key2' : ''}
dict2 = {'key1' : 'val1', 'key2' : 'val2'}
dict3 = {k: v if v else dict2.get(k, v) for k, v in dict1.items() }
print(dict3) #--> {'key1': 'val1', 'key2': 'val2'}

Remove duplicate value from dictionary without removing key

I'm new to Python and am having to learn by trial and error, but I haven't been able to find a solution to the problem I'm having.
I have a dictionary that looks something like this:
myDict = {'key1': ['item1', 'item2', 'item3'], 'key2': ['item4', 'item5', 'item6'],
'key3': 'item7', 'key4': 'item8', 'key5': ['item1', 'item2', 'item3'], 'key6': 'item7'}
I need to remove duplicate values from the dictionary and replace them with an empty value (""). Found a couple solution on here but they are working as intended
for key, value in myDict.items():
if values not in key newDict.values():
myDict[key] = value
else:
myDict[key] = ""
print newDict
This is removing all the values and is outputting
# newDict:{key1: '', key2: '', key3: '', key4: '', key5: '', key6: '')
I'm looking for the output to be
# newDict = {'key1': '', 'key2':['item4', 'item5', 'item6'], 'key3': '', 'key4':
'item8', key5: ['item1', 'item2', 'item3'], 'key6': 'item7'}
You have the right overall idea, but there are three problems with your code:
You're storing values back into myDict instead of into newDict.
On line 2, you're checking values instead of value.
Also on line 2, key shouldn't be there, and throws a SyntaxError.
Here is the correct code:
newDict = {}
for key, value in myDict.iteritems():
if value not in newDict.values():
newDict[key] = value
else:
newDict[key] = ""
print newDict
If you're not in the anti-ternary operator camp, you could also shorten it to this:
newDict = {}
for key, value in myDict.iteritems():
newDict[key] = value if value not in newDict.values() else ""
print newDict
Or, if you would rather just remove the values from the original dict (myDict) instead of building a new one (newDict), you could do this:
foundValues = []
for key, value in myDict.iteritems():
if value not in foundValues:
foundValues.append(myDict[key])
else:
myDict[key] = ""
print myDict
If you need duplicate values removed in a specific order, check out OrderedDicts.
Update:
In light of the updated requirements -- that values be removed from the original dict, starting from the beginning -- if you're able to simply initialize myDict with an OrderedDict instead of a dict, all you need to do is replace this:
myDict = {'key1': ['item1', 'item2', 'item3'], 'key2': ['item4', 'item5', 'item6'], 'key3': 'item7', 'key4': 'item8', 'key5': ['item1', 'item2', 'item3'], 'key6': 'item7'}
with this:
from collections import OrderedDict
…
myDict = OrderedDict([('key1', ['item1', 'item2', 'item3']), ('key2', ['item4', 'item5', 'item6']), ('key3', 'item7'), ('key4', 'item8'), ('key5', ['item1', 'item2', 'item3']), ('key6', 'item7')])
and then use the same code provided above.
This does it:
myDict_values = myDict.values() # better than calling this numerous times
for key in myDict.keys():
if myDict_values.count(myDict[key]) > 1: myDict[key] = ""
This won't guarantee that key5 will be blank instead of key1, because dictionaries are not ordered.

Categories