Python 如何生成每个元素到其发生次数的映射?

Python 如何生成每个元素到其发生次数的映射?,python,python-2.7,Python,Python 2.7,我有一个定义为list=[“a”、“b”、“d”、“f”、“g”、“a”、“g”、“d”、“a”、“d”]的列表,我想要一个这样的dicc={a:2、b:1、d:3、f:1、g:2}。此字典中的值是列表元素在列表中重复的次数。我尝试了以下内容,但不知道在中放什么 在这种情况下,您不必使用len(list),因为python非常灵活,并且列表有count() 解决方案: lista = ["a","b","d","f","g","a","g","d","a","d"] dicc = dict(zi

我有一个定义为
list=[“a”、“b”、“d”、“f”、“g”、“a”、“g”、“d”、“a”、“d”]
的列表,我想要一个这样的
dicc={a:2、b:1、d:3、f:1、g:2}
。此字典中的值是列表元素在列表中重复的次数。我尝试了以下内容,但不知道在
中放什么

在这种情况下,您不必使用len(list),因为python非常灵活,并且列表有count()

解决方案:

lista = ["a","b","d","f","g","a","g","d","a","d"]
dicc = dict(zip(list,[list.count(x) for x in list]))
print dicc 
>>>dicc = {a:2, b:1, d:3, f:1, g:2}
或者您可以使用评论中提到的计数器。我将包括一个类似的例子。万一你要打电话

dicc = dict(Counter(list))
这是因为:

type(Counter(list)) -> collections.Counter
但是,如果您想要您所给出的代码,您可以使用下面的代码。但这不是最好的办法

dicc = dict(zip(list,[list.count(list[x]) for x in range(len(list))]))
两种解决方案都是相同的,第一种方案展示了python的美丽。希望您能得到答案。

您不必使用len(list),因为python非常灵活,而且列表有count()

解决方案:

lista = ["a","b","d","f","g","a","g","d","a","d"]
dicc = dict(zip(list,[list.count(x) for x in list]))
print dicc 
>>>dicc = {a:2, b:1, d:3, f:1, g:2}
或者您可以使用评论中提到的计数器。我将包括一个类似的例子。万一你要打电话

dicc = dict(Counter(list))
这是因为:

type(Counter(list)) -> collections.Counter
但是,如果您想要您所给出的代码,您可以使用下面的代码。但这不是最好的办法

dicc = dict(zip(list,[list.count(list[x]) for x in range(len(list))]))
两种解决方案都是相同的,第一种方案展示了python的美丽。希望您能得到答案。

您不必使用len(list),因为python非常灵活,而且列表有count()

解决方案:

lista = ["a","b","d","f","g","a","g","d","a","d"]
dicc = dict(zip(list,[list.count(x) for x in list]))
print dicc 
>>>dicc = {a:2, b:1, d:3, f:1, g:2}
或者您可以使用评论中提到的计数器。我将包括一个类似的例子。万一你要打电话

dicc = dict(Counter(list))
这是因为:

type(Counter(list)) -> collections.Counter
但是,如果您想要您所给出的代码,您可以使用下面的代码。但这不是最好的办法

dicc = dict(zip(list,[list.count(list[x]) for x in range(len(list))]))
两种解决方案都是相同的,第一种方案展示了python的美丽。希望您能得到答案。

您不必使用len(list),因为python非常灵活,而且列表有count()

解决方案:

lista = ["a","b","d","f","g","a","g","d","a","d"]
dicc = dict(zip(list,[list.count(x) for x in list]))
print dicc 
>>>dicc = {a:2, b:1, d:3, f:1, g:2}
或者您可以使用评论中提到的计数器。我将包括一个类似的例子。万一你要打电话

dicc = dict(Counter(list))
这是因为:

type(Counter(list)) -> collections.Counter
但是,如果您想要您所给出的代码,您可以使用下面的代码。但这不是最好的办法

dicc = dict(zip(list,[list.count(list[x]) for x in range(len(list))]))
两种解决方案都是相同的,第一种方案展示了python的美丽。希望你得到你的答案。

这正是我们班要做的

from collections import Counter
Counter(lista)
=> Counter({'d': 3, 'a': 3, 'g': 2, 'b': 1, 'f': 1})
Counter
dict
的子类,因此您可以将其用作dict。

这正是该类所做的

from collections import Counter
Counter(lista)
=> Counter({'d': 3, 'a': 3, 'g': 2, 'b': 1, 'f': 1})
Counter
dict
的子类,因此您可以将其用作dict。

这正是该类所做的

from collections import Counter
Counter(lista)
=> Counter({'d': 3, 'a': 3, 'g': 2, 'b': 1, 'f': 1})
Counter
dict
的子类,因此您可以将其用作dict。

这正是该类所做的

from collections import Counter
Counter(lista)
=> Counter({'d': 3, 'a': 3, 'g': 2, 'b': 1, 'f': 1})

<代码>计数器<代码>是代码> DICT<代码>的子类,因此您可以使用它作为DAT.< /P>。请考虑改进您的代码示例。我发现很难理解编写代码时的想法。还请注意,Python有一个<代码>计数器>代码>数据结构来解决这个问题:请考虑改进您的代码示例。我发现很难理解编写代码时的想法。还请注意,Python有一个<代码>计数器>代码>数据结构来解决这个问题:请考虑改进您的代码示例。我发现很难理解编写代码时的想法。还请注意,Python有一个<代码>计数器>代码>数据结构来解决这个问题:请考虑改进您的代码示例。我发现很难理解编写代码时的想法。还要注意,Python有一个

计数器
数据结构来解决这个问题: