Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python从列表中获取字典,列表条目作为键,列表中的项目数量作为值?_Python_List_Dictionary - Fatal编程技术网

如何使用python从列表中获取字典,列表条目作为键,列表中的项目数量作为值?

如何使用python从列表中获取字典,列表条目作为键,列表中的项目数量作为值?,python,list,dictionary,Python,List,Dictionary,在特定情况下,我有以下列表: [12112,7676,11708,6045,4705,11143,11143,5895] 并希望得到以下输出: {"12112":"1","7676":"1","11708":"1","11143":"2","5895":"1","6045":"1","4705":"1"} 您可以从列表中设置,以确定唯一的元素。然后使用dict理解和count确定列表中每个唯一元素的实例数 >>> l = [12112,7676,11708,6045,470

在特定情况下,我有以下列表:

[12112,7676,11708,6045,4705,11143,11143,5895]
并希望得到以下输出:

{"12112":"1","7676":"1","11708":"1","11143":"2","5895":"1","6045":"1","4705":"1"}

您可以从列表中设置,以确定唯一的元素。然后使用dict理解和
count
确定列表中每个唯一元素的实例数

>>> l = [12112,7676,11708,6045,4705,11143,11143,5895]
>>> {i : l.count(i) for i in set(l)}
{12112: 1, 4705: 1, 5895: 1, 11708: 1, 11143: 2, 7676: 1, 6045: 1}
否则,您可以只使用集合。计数器

>>> from collections import Counter
>>> dict(Counter(l))
{12112: 1, 4705: 1, 5895: 1, 11708: 1, 11143: 2, 7676: 1, 6045: 1}

您可以使用
计数器

>>> from collections import Counter
>>> l = [12112,7676,11708,6045,4705,11143,11143,5895]
>>> Counter(l)
Counter({11143: 2, 4705: 1, 7676: 1, 5895: 1, 12112: 1, 11708: 1, 6045: 1})
如果你想要口述

>>> dict(Counter(l))
{12112: 1, 4705: 1, 11143: 2, 11708: 1, 5895: 1, 7676: 1, 6045: 1}
当你的问题使用字符串时,听写理解起作用:

>>> {str(i):str(j) for i,j in Counter(l).items()}
{'12112': '1', '11143': '2', '4705': '1', '11708': '1', '5895': '1', '6045': '1', '7676': '1'}
既然您想要一个带有
”的字符串:


您也可以使用
groupby

使用
map

m= map(str,l)
然后,
groupby
,每个列表的长度描述频率

m= map(str,l)
{key:str(len(list(group))) for key, group in groupby(m)}
你的代码看起来像

l = [12112,7676,11708,6045,4705,11143,11143,5895]
from itertools import groupby
print {key:str(len(list(group))) for key, group in groupby(map(str,l))}
输出:

{'12112': '1', '11143': '2', '4705': '1', '11708': '1', '5895': '1', '6045': '1', '7676': '1'}

这可能是低效的,因为list.count是O(n)Nice.ty。有没有一种简单的方法可以将所有键和值生成字符串?我特别需要“而不是”。我会使用str({str(I):str(j)代表I,j在计数器(l.items()})中)。替换(“,”)。或者有更优雅的方法吗?@Akku
'a'
“a”
代表相同的字符串:尝试
'a'==“a”
打印([(str(k),str(v))代表dict(计数器(a)).items())我知道。但我需要那个字符串通过tcp发送,而另一端似乎不能正确使用“a”。
{'12112': '1', '11143': '2', '4705': '1', '11708': '1', '5895': '1', '6045': '1', '7676': '1'}