[英]Dictionary keys with list value
I wanted to append to respective keys in a dictionary in the list of string我想附加到字符串列表中字典中的各个键
myDictionary = {'johny': [], 'Eli': [], 'Johny': [], 'Jane': [], 'john': [], 'Ally': []}
votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli']
outPut={'johny': ['johny'], 'Eli': ['Eli','Eli'], 'Johny': ['Johny'], 'Jane': ['Jane'], 'john': ['john'], 'Ally': ['Ally']}
I tried to do like this but appends the whole list in each key我试图这样做,但在每个键中附加整个列表
votes_dictionary={}
votes_dictionary=votes_dictionary.fromkeys(votes,[])
for i in votes:
print(i.lower())
votes_dictionary[i].append(i)
print(votes_dictionary)
You can use defaultdict with list
as default value and then iterate through votes and append it:您可以使用defaultdict和
list
作为默认值,然后遍历投票并附加它:
from collections import defaultdict
votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli']
votes_dictionary = defaultdict(list)
for vote in votes:
votes_dictionary[vote].append(vote)
# votes_dictionary will be an instance of defaultdict
# to convert it to dict, just call dict
print(dict(votes_dictionary))
# outpout
{'johny': ['johny'], 'Eli': ['Eli', 'Eli', 'Eli'], 'Jane': ['Jane'], 'Ally': ['Ally'], 'Johny': ['Johny'], 'john': ['john']}
I see there are three Eli
s, so typically it would look like this:我看到有三个
Eli
,所以通常它看起来像这样:
output = {}
for name in votes:
output.setdefault(name, [])
output[name].append(name)
print(output)
Output:输出:
{'johny': ['johny'],
'Eli': ['Eli', 'Eli', 'Eli'],
'Jane': ['Jane'],
'Ally': ['Ally'],
'Johny': ['Johny'],
'john': ['john']}
Or,或者,
import copy
output = copy.deepcopy(mydictionary)
for name in votes:
output[name].append(name)
print(output):
output:输出:
{'johny': ['johny'],
'Eli': ['Eli', 'Eli', 'Eli'],
'Johny': ['Johny'],
'Jane': ['Jane'],
'john': ['john'],
'Ally': ['Ally']}
Now, if you want to limit to two even if there are three elements:现在,如果您想限制为两个,即使有三个元素:
output = {}
for name in votes:
# to maintain the order, you can iterate over `mydictionary`
output[name] = [name]*min(2, votes.count(name))
print(output)
output:输出:
{'johny': ['johny'],
'Eli': ['Eli', 'Eli'],
'Jane': ['Jane'],
'Ally': ['Ally'],
'Johny': ['Johny'],
'john': ['john']}
Another fun way to achieve two occurrences of Eli
would be, itertools.groupby
:实现两次
Eli
另一种有趣方式是itertools.groupby
:
>>> from itertools import groupby
>>> {key: [*group] for key, group in groupby(reversed(votes))}
{'Eli': ['Eli', 'Eli'],
'john': ['john'],
'Johny': ['Johny'],
'Ally': ['Ally'],
'Jane': ['Jane'],
'johny': ['johny']}
votes_dictionary={}
for i in votes:
try:
votes_dictionary[i].append(i)
except KeyError:
votes_dictionary[i] = [i]
print(votes_dictionary)
myDictionary = {'johny': [], 'Eli': [], 'Johny': [], 'Jane': [], 'john': [], 'Ally': []}
votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli']
for x in votes:
myDictionary[x].append(x)
votes_dictionary = {}
votes_dictionary = votes_dictionary.fromkeys(votes)
for i in votes:
if not votes_dictionary[i]:
votes_dictionary[i] = []
votes_dictionary[i].append(i)
print(votes_dictionary)
A lot of answers have been posted, if you really want to use the fromkeys() method, you could do something like this已经发布了很多答案,如果你真的想使用 fromkeys() 方法,你可以做这样的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.