我是新的顶级python,希望您的支持进行以下操作。我有如下列表:
item = [[8, 28], [8, 33], [8, 38], [8, 43], [13, 18], [13, 23],
[13, 28], [13, 33], [13, 38], [13, 43], [18, 23], [18, 28],
[18, 33], [18, 38], [18, 43], [23, 28], [23, 33], [23, 38],
[23, 43], [28, 33], [28, 38], [28, 43], [33, 38], [33, 43],
[38, 43], [4, 9], [4, 14], [4, 19], [4, 24], [4, 29], [4, 34],
[4, 39], [4, 44], [9, 14], [9, 19], [9, 24], [9, 29], [25, 30],
[25, 35], [25, 40], [25, 45], [30, 35], [30, 40], [30, 45]]
我想获取同时出现的每个元组的计数,我尝试应用以下代码:
collection.Counter(item)
发生以下错误:
count_elements(self, iterable)
TypeError: unhashable type: 'list'
答案 0 :(得分:0)
如果您要列出一个元组列表(如您的问题所示),则可以将所有内括号都转换为括号,如下所示:
utils.parse_config
这是因为ImportErrorTraceback (most recent call last) <ipython-input-6-77f4a3369184> in <module>()
----> 1 from models import *
2 from utils import *
3
4 import os, sys, time, datetime, random
5 import torch
/content/models.py in <module>()
9 from PIL import Image
10
---> 11 from utils.parse_config import *
12 from utils.utils import build_targets
13 from collections import defaultdict
ImportError: No module named utils.parse_config
--------------------------------------------------------------------------- NOTE: If your import is failing due to a missing package, you can manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the "Open Examples" button below.
---------------------------------------------------------------------------
表示列表,而items = [(8, 28), (8, 33), (8, 38), (8, 43), (13, 18), (13, 23), (13, 28), (13, 33), (13, 38), (13, 43), (18, 23), (18, 28), (18, 33), (18, 38), (18, 43), (23, 28), (23, 33), (23, 38), (23, 43), (28, 33), (28, 38), (28, 43), (33, 38), (33, 43), (38, 43), (4, 9), (4, 14), (4, 19), (4, 24), (4, 29), (4, 34), (4, 39), (4, 44), (9, 14), (9, 19), (9, 24), (9, 29), (25, 30), (25, 35), (25, 40), (25, 45), (30, 35), (30, 40), (30, 45), (35, 40), (35, 45), (40, 45), (1, 6), (1, 11), (1, 16), (1, 21), (1, 26), (1, 31), (1, 36), (1, 41), (1, 46), (6, 11), (6, 16), (6, 21), (6, 26), (6, 31), (6, 36), (6, 41), (6, 46), (11, 16), (11, 21), (11, 26), (11, 31), (11, 36), (11, 41), (11, 46), (16, 21), (16, 26), (16, 31), (16, 36), (16, 41), (16, 46), (21, 26), (21, 31), (21, 36), (21, 41), (21, 46), (26, 31), (26, 36), (26, 41), (26, 46), (31, 36), (31, 41), (31, 46), (36, 41), (36, 46), (41, 46), (2, 7), (2, 12), (2, 17), (2, 22), (2, 27), (2, 32), (2, 37), (2, 42), (2, 47), (7, 12), (7, 17), (7, 22), (7, 27), (7, 32), (7, 37), (7, 42), (7, 47), (12, 17), (12, 22), (12, 27), (12, 32), (12, 37), (12, 42), (12, 47), (17, 22), (17, 27), (17, 32), (17, 37), (17, 42), (17, 47), (22, 27), (22, 32), (22, 37), (22, 42),(22, 47),(27, 32)]
表示python中的元组。如果您这样做,那么Counter肯定会开始工作。
但是,如果您想要的是列表列表并进行比较,则Counter可能无法按您想要的方式工作。您可以使用以下程序通过编程将每个元素转换为元组:
[]
,然后在计数器中使用该元组列表。
答案 1 :(得分:0)
您的子列表(数字对)不能用作dict
键,因为它们是不可散列的(因为列表是可变的)。
要解决此问题,您可以将列表更改为元组
item_list = [
(8, 28), (8, 33), (8, 38), (8, 43), (13, 18), (13, 23),
(13, 28), (13, 33), (13, 38), (13, 43), (18, 23), (18, 28),
(18, 33), (18, 38), (18, 43), (23, 28), (23, 33), (23, 38),
(23, 43), (28, 33), (28, 38), (28, 43), (33, 38), (33, 43),
(38, 43), (4, 9), (4, 14), (4, 19), (4, 24), (4, 29), (4, 34),
(4, 39), (4, 44), (9, 14), (9, 19), (9, 24), (9, 29), (25, 30),
(25, 35), (25, 40), (25, 45), (30, 35), (30, 40), (30, 45),
]
c = collections.Counter(item_list)
答案 2 :(得分:0)
您有一个列表列表,可以将列表转换为元组,然后id
将起作用:
collection.Counter(item)
输出:
from collections import Counter
item_lists = [[8, 28], [8, 33], [8, 38], [8, 43], [13, 18], [13, 23], [13, 28],
[13, 33], [13, 38], [13, 43], [18, 23], [18, 28], [18, 33],
[18, 38], [18, 43], [23, 28], [23, 33], [23, 38], [23, 43],
[28, 33], [28, 38], [28, 43], [33, 38], [33, 43], [38, 43],
[4, 9], [4, 14], [4, 19], [4, 24], [4, 29], [4, 34], [4, 39],
[4, 44], [9, 14], [9, 19], [9, 24], [9, 29], [25, 30], [25, 35],
[25, 40], [25, 45], [30, 35], [30, 40], [30, 45]]
item_tuples = [tuple(l) for l in item_lists]
print(Counter(item_tuples))
注意:目前看来,所有项目在原始列表中仅存在一次。
答案 3 :(得分:0)
首先,方括号表示列表,括号表示元组。因此,item
是一个列表列表,而不是元组列表。元组列表看起来更像[[(8,28),(8,33),(8,38),(8,43)...]将item
转换为元组将map
与tuple
结合使用:
fixed_item = map(tuple, item)
这就是说:如果您只想计算item
中的元组数,那只是列表的长度,因此您可以使用len()
:
len(fixed_item)
如果您要计算的是item
中每个元组的重复次数,那么Counter()
可能会有用。
from collections import Counter
new_counter = Counter(fixed_item)
new_counter
将包含一个字典,其中item
中的每个元组都是一个键,并且相应的值是该特定元组出现在item
中的次数。