I have a python dictionary and I'd like to concatenate the values of value[0] and a list in value [1]
so
dict={ 'Key1':['MASTER',['_A','_B','_C']]}
and expected output is of calling Key1 is
[['MASTER_A','MASTER_B','MASTER_C']]
Use a nested comprehension:
d = {'Key1':['MASTER',['_A','_B','_C']]}
result_dict = {k: [v[0] + l for l in v[1]] for k,v in d.items()}
For example:
>>> result_dict = {k: [v[0] + l for l in v[1]] for k,v in d.items()}
>>> result_dict
{'Key1': ['MASTER_A', 'MASTER_B', 'MASTER_C']}
Try this :
d = {'Key1':['MASTER',['_A','_B','_C']]}
out = [d['Key1'][0]+i for i in d['Key1'][1]]
Output :
['MASTER_A', 'MASTER_B', 'MASTER_C']
To assign it to the key, do :
d['Key1'] = out
Related
I have two dictionaries as follows, I need to extract which strings in the tuple values are in one dictionary but not in other:
dict_a = {"s": ("mmmm", "iiiii", "p11"), "yyzz": ("oo", "i9")}
dict_b = {"s": ("mmmm",), "h": ("pp",), "g": ("rr",)}
The desired output:
{"s": ("iiiii", "p11"), "yyzz": ("oo", "i9")}
The order of the strings in the output doesn't matter.
One way that I tried to solve, but it doesn't produce the expected result:
>>> [item for item in dict_a.values() if item not in dict_b.values()]
[('mmmm', 'iiiii', 'p11'), ('oo', 'i9')]
If order doesn't matter, convert your dictionary values to sets, and subtract these:
{k: set(v) - set(dict_b.get(k, ())) for k, v in dict_a.items()}
The above takes all key-value pairs from dict_a, and for each such pair, outputs a new dictionary with those keys and a new value that's the set difference between the original value and the corresponding value from dict_b, if there is one:
>>> dict_a = {"s": ("mmmm", "iiiii", "p11"), "yyzz": ("oo", "i9")}
>>> dict_b = {"s": ("mmmm",), "h": ("pp",), "g": ("rr",)}
>>> {k: set(v) - set(dict_b.get(k, ())) for k, v in dict_a.items()}
{'s': {'p11', 'iiiii'}, 'yyzz': {'oo', 'i9'}}
The output will have sets, but these can be converted back to tuples if necessary:
{k: tuple(set(v) - set(dict_b.get(k, ()))) for k, v in dict_a.items()}
The dict_b.get(k, ()) call ensures there is always a tuple to give to set().
If you use the set.difference() method you don't even need to turn the dict_b value to a set:
{k: tuple(set(v).difference(dict_b.get(k, ()))) for k, v in dict_a.items()}
Demo of the latter two options:
>>> {k: tuple(set(v) - set(dict_b.get(k, ()))) for k, v in dict_a.items()}
{'s': ('p11', 'iiiii'), 'yyzz': ('oo', 'i9')}
>>> {k: tuple(set(v).difference(dict_b.get(k, ()))) for k, v in dict_a.items()}
{'s': ('p11', 'iiiii'), 'yyzz': ('oo', 'i9')}
{k: [v for v in vs if v not in dict_b.get(k, [])] for k,vs in dict_a.items()}
if you want to use tuples (or sets - just replace the cast)
{k: tuple(v for v in vs if v not in dict_b.get(k, [])) for k,vs in dict_a.items()}
Try this (see comments for explanations):
>>> out = {} # Initialise output dictionary
>>> for k, v in dict_a.items(): # Iterate through items of dict_a
... if k not in dict_b: # Check if the key is not in dict_b
... out[k] = v # If it isn't, add to out
... else: # Otherwise
... out[k] = tuple(set(v) - set(dict_b[k])) # Subtract sets to find the difference
...
>>> out
{'s': ('iiiii', 'p11'), 'yyzz': ('oo', 'i9')}
This can then be simplified using a dictionary comprehension:
>>> out = {k: tuple(set(v) - set(dict_b.get(k, ()))) for k, v in dict_a.items()}
>>> out
{'s': ('iiiii', 'p11'), 'yyzz': ('oo', 'i9')}
See this solution :
First iterate through all keys in dict_a
Check if the key is present or not in dict_b
Now, if present: take the tuples and iterate through dict_a's tuple(as per your question). now check weather that element is present or not in dict_b's tuple. If it is present just leave it. If it is not just add that element in tup_res.
Now after the for loop, add that key and tup value in the dict_res.
if the key is not present in dict_b simply add it in dict_res.
dict_a = {"s":("mmmm","iiiii","p11"), "yyzz":("oo","i9")}
dict_b = {"s":("mmmm"),"h":("pp",),"g":("rr",)}
dict_res = {}
for key in dict_a:
if key in dict_b:
tup_a = dict_a[key]
tup_b = dict_b[key]
tup_res = ()
for tup_ele in tup_a:
if tup_ele in tup_b:
pass
else:
tup_res = tup_res + (tup_ele,)
dict_res[key] = tup_res;
else:
dict_res[key] = dict_a[key];
print(dict_res)
It is giving correct output :)
I have a dictionary as follows:
d= {'a':['the','the','an','an'],'b':['hello','hello','or']}
I want to convert this dictionary into a nested dictionary with values of a key and their count as follows:
d = {'a':{'the':2,'an':2},'b':{'hello':2,'or':1}}
I can count the values for a dictionary as follows but unable to turn the values to another dictionary with their counts.
length_dict = {key: len(value) for key, value in d.items()}
You can use collections.Counter instead:
from collections import Counter
{k: dict(Counter(v)) for k, v in d.items()}
This returns:
{'a': {'the': 2, 'an': 2}, 'b': {'hello': 2, 'or': 1}}
d= {'a':['the','the','an','an'],'b':['hello','hello','or']}
I think thig ough to do it :
from collections import Counter
new_dict = {}
for k in d.keys():
aux_counter = Counter(d[k])
new_dict [k] = {}
for c, v in zip(aux_counter.keys(), aux_counter.values()):
new_dict[k][c] = v
A dictionary comprehension using Counter
from collections import Counter
{k:{p:q for p,q in Counter(v).items()} for k,v in d.items()}
Without using Counter
def count_values(v):
d={}
for i in v:
d[i]=d.get(i,0)+1
return d
{k:{p:q for p,q in count_values(v).items()} for k,v in d.items()}
Giving you more options using Pandas(not required) here but still
from pandas import Series
df = pd.DataFrame(dict([ (k,Series(v)) for k,v in d.items() ]))
{c:df[c].value_counts().to_dict() for c in df.columns}
I am new to Python and need help with converting a list "lis" into a dictionary "dic". The list is generated by reading lines from stdin as follows-
lis = sys.stdin.readlines()
['San Jose\t200.82\n', 'California\t115.15\n', 'San Jose\t20.20\n', 'Texas\t300.10\n', 'San Jose\t100.50\n']
I have done the following to convert an item in list into key,value but dont know how to store this in a dictionary.
for item in lis:
k,v1 = item.split('\t')
v = float(v1)
print k,v
I would like to store this in a dictionary as follows
{'San Jose':200.82, 'California':115.15 .....etc}
Could you please show me how to do this efficiently? Later on I would like to load this dictionary into Pandas DataFrame.
You're almost there:
dic = {}
for item in lis:
k,v1 = item.split('\t')
v = float(v1)
print k,v
dic[k] = v
Of course, you could simplify this with a comprehension:
dic = {item.split('\t')[0]:float(item.split('\t')[1]) for item in lis}
It seems that you want multiple values associated with each key. For that, use a defaultdict:
from collections import defaultdict
dic = defaultdict(list)
for item in lis:
k,v1 = item.split('\t')
v = float(v1)
print k,v
dic[k].append(v)
Of course, you could just do the heavy-lifting yourself:
dic = {}
for item in lis:
k,v1 = item.split('\t')
v = float(v1)
print k,v
if k not in dic:
dic[k] = []
dic[k].append(v)
This should work -
dic = {}
for k,v in [e.strip().split("\t") for e in lis]:
dic[k] = v
#{'San Jose': '100.50', 'California': '115.15', 'Texas': '300.10'}
Or more succinctly -
dic = {k:v for k,v in [e.strip().split("\t") for e in lis]}
If you don't want to overwrite previous values -
dic = {}
for k,v in [e.strip().split("\t") for e in lis]:
if k not in dic:
dic[k] = v
If you want to have multiple values for the same key.
dic = {}
for k,v in [e.strip().split("\t") for e in lis]:
if k not in dic:
dic[k] = [v]
else:
dic[k].append(v)
'''
Output of the Dictionary.
{
'San Jose': ['200.82', '20.20', '100.50'],
'California': ['115.15'],
'Texas': ['300.10']
}
'''
I have two inputs
list1 = [1,2,3,4,5,6]
dict1={'a':[1,11],'b':[2,234],'c':[34,6]}
I need the results to be displayed as
list1 = [3,4,5]
Algorithm : Display the values of list1 which is not in dict1 values
Well I know this can be achieved by the below code,
tmp=0
for x in list1:
for k,v in dict1.items():
if x in v:
tmp=1
break;
if tmp:
list1.remove(x)
tmp=''
print list1
Is there anyway I can try this using filter function or any single line function?
Collect all list values in the dictionary into a set:
all_values = {v for lst in dict1.itervalues() for v in lst}
then filter your list on that:
list1 = [v for v in list1 if v not in all_values]
Demo:
>>> list1 = [1,2,3,4,5,6]
>>> dict1={'a':[1,11],'b':[2,234],'c':[34,6]}
>>> all_values = {v for lst in dict1.itervalues() for v in lst}
>>> [v for v in list1 if v not in all_values]
[3, 4, 5]
list1 = [1,2,3,4,5,6]
dict1 = {'a':[1,11],'b':[2,234],'c':[34,6]}
set1 = set(list1)
set2 = set(t for _, v in dict1.items() for t in v)
print list(set1 - set2)
Output
[3, 4, 5]
Edit: As per bruno's suggestion
list1 = [1,2,3,4,5,6]
dict1 = {'a':[1,11],'b':[2,234],'c':[34,6]}
from itertools import chain
set1 = set(list1)
set2 = set(v for v in chain.from_iterable(dict1.values()))
print list(set1 - set2)
Your solution wouldn't work, you're iterating through dict1.values(), when what I think you intend is dict1.items(). That line should actually be:
for v in dict1.values():
or
for k, v in dict1.items():
Though you don't really need k, so I'd go with the first option.
What I would do would probably be more like:
removeMe = []
for v in dict1.values():
removeMe.extend(v)
return [i for i in list1 if i not in removeMe]
Say I have a dictionary with whatever number of values.
And then I create a list.
If any of the values of the list are found in the dictionary, regardless of whether or not it is a key or an index how do I delete the full value?
E.g:
dictionary = {1:3,4:5}
list = [1]
...
dictionary = {4:5}
How do I do this without creating a new dictionary?
for key, value in list(dic.items()):
if key in lst or value in lst:
del dic[key]
No need to create a separate list or dictionary.
I interpreted "whether or not it is a key or an index" to mean "whether or not it is a key or a value [in the dictionary]"
it's a bit complicated because of your "values" requirement:
>>> dic = {1: 3, 4: 5}
>>> ls = set([1])
>>> dels = []
>>> for k, v in dic.items():
if k in ls or v in ls:
dels.append(k)
>>> for i in dels:
del dic[i]
>>> dic
{4: 5}
A one liner to do this would be :
[dictionary.pop(x) for x in list if x in dictionary.keys()]
dictionary = {1:3,4:5}
list = [1]
for key in list:
if key in dictionary:
del dictionary[key]
>>> dictionary = {1:3,4:5}
>>> list = [1]
>>> for x in list:
... if x in dictionary:
... del(dictionary[x])
...
>>> dictionary
{4: 5}
def remKeys(dictionary, list):
for i in list:
if i in dictionary.keys():
dictionary.pop(i)
return dictionary
I would do something like:
for i in list:
if dictionary.has_key(i):
del dictionary[i]
But I am sure there are better ways.
A few more testcases to define how I interpret your question:
#!/usr/bin/env python
def test(beforedic,afterdic,removelist):
d = beforedic
l = removelist
for i in l:
for (k,v) in list(d.items()):
if k == i or v == i:
del d[k]
assert d == afterdic,"d is "+str(d)
test({1:3,4:5},{4:5},[1])
test({1:3,4:5},{4:5},[3])
test({1:3,4:5},{1:3,4:5},[9])
test({1:3,4:5},{4:5},[1,3])
If the dictionary is small enough, it's easier to just make a new one. Removing all items whose key is in the set s from the dictionary d:
d = dict((k, v) for (k, v) in d.items() if not k in s)
Removing all items whose key or value is in the set s from the dictionary d:
d = dict((k, v) for (k, v) in d.items() if not k in s and not v in s)