Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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_Python 2.7_Counter - Fatal编程技术网

Python 计算循环列表的频率--在列表列表中

Python 计算循环列表的频率--在列表列表中,python,list,python-2.7,counter,Python,List,Python 2.7,Counter,我有一个python列表,我需要找出每个子列表出现的次数。这是一个样本 from collections import Counter list1 = [[ 1., 4., 2.5], [ 1., 2.66666667, 1.33333333], [ 1., 2., 2.], [ 1., 2.66666667, 1.33333333], [ 1., 4., 2.5], [ 1., 2.66666667, 1.33333333]] c = Counter

我有一个python列表,我需要找出每个子列表出现的次数。这是一个样本

from collections import Counter
list1 = [[ 1., 4., 2.5], [ 1., 2.66666667, 1.33333333], 
         [ 1., 2., 2.], [ 1., 2.66666667, 1.33333333], [ 1., 4., 2.5],
         [ 1., 2.66666667, 1.33333333]]   
c = Counter(x for x in iter(list1))
print c
如果列表中的元素是可散列的(比如int),那么上面的代码可以工作,但在本例中,它们是列表,我得到一个错误

TypeError: unhashable type: 'list'
我如何计算这些列表,以便得到

[ 1., 2.66666667, 1.33333333], 3
[ 1., 4., 2.5], 2
[ 1., 2., 2.], 1

只需将列表转换为元组:

>>> c = Counter(tuple(x) for x in iter(list1))
>>> c
Counter({(1.0, 2.66666667, 1.33333333): 3, (1.0, 4.0, 2.5): 2, (1.0, 2.0, 2.0): 1})
请记住对查找执行相同的操作:

>>> c[tuple(list1[0])]
2
试试这个

list1 = [[ 1., 4., 2.5], [ 1., 2.66666667, 1.33333333], 
         [ 1., 2., 2.], [ 1., 2.66666667, 1.33333333], [ 1., 4., 2.5],
         [ 1., 2.66666667, 1.33333333]]

counter = {}
for el in list1:
    el = str(el)     #This sorts your hashable part or use tuple(el)
    if el in counter:
        counter[el]+=1
    else:
        counter[el]=1

print(counter)
应该输出

{'[1.0, 2.0, 2.0]': 1, '[1.0, 2.66666667, 1.33333333]': 3, '[1.0, 4.0, 2.5]': 2}

计数器返回一个类似字典的对象,它的键必须是可散列的。由于列表不可散列,您可以使用
map
函数将其转换为
tuple

>>> Counter(map(tuple, list1))
Counter({(1.0, 2.66666667, 1.33333333): 3, (1.0, 4.0, 2.5): 2, (1.0, 2.0, 2.0): 1})
请注意,使用
map
将比生成器表达式的性能稍好一些,因为通过将生成器表达式传递给
Counter()
python将自己从生成器函数获取值,因为使用内置函数
map
在执行时间方面具有更高的性能1

发件人:

生成器表达式的语义相当于创建匿名生成器函数并调用它。例如:

g = (x**2 for x in range(10))
print g.next()
相当于:

def __gen(exp):
    for x in exp:
        yield x**2
g = __gen(iter(range(10)))
print g.next()

请注意,由于生成器表达式在内存使用方面更好,如果您处理的是大数据,最好使用生成器表达式而不是映射函数。

将它们转换为元组-它们是可散列的。哇,这非常简单。只是为了确保计数器总是按从最高频率到最低频率的顺序返回值?@WanderingMind不知道你的意思。如果您希望项目按降序排列,请使用
c.most_common()
。您显示的结果的频率从高到低。我想知道这是否是预期的结果。看起来,您需要使用
最常见的()
来获得这种行为。@WanderingMind
计数器有一个字典作为后盾,所以当您只是打印或迭代计数器时,它们的打印顺序是随机的。如果要按特定顺序获取它们,必须使用
most_common
@tobias_k,是否存在以打印生成器对象,以便它不会附加“,'指向它所指向的元组中的项目?有类似的python查找方式吗?@WanderingMind你所说的查找是什么意思?如何知道特定子列表的频率,比如说
list1[5]
只需将
map(tuple,list1)
的结果放入一个新列表中,然后执行
Counter\u object[new\u list[5]
谢谢你的帮助!!
def __gen(exp):
    for x in exp:
        yield x**2
g = __gen(iter(range(10)))
print g.next()