起初,我道歉是因为我是新手...
我有一个3d元组列表:
list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
我想找到重复项并像这样显示,无论数字的位置如何,只具有相同的数字:
{ (0,1,6): 2,
(4,5,1): 2,
(3,2,1): 1 }
我想计算相似的元组。
有什么建议吗?
答案 0 :(得分:0)
使用Counter
中的collections
:
>>> from collections import Counter
>>> list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
>>> dict(Counter(map(lambda x: tuple(sorted(x)),list)))
{(0, 1, 6): 2, (1, 4, 5): 2, (1, 2, 3): 1}
>>>
或列表样式的理解:
>>> from collections import Counter
>>> list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
>>> dict(Counter([tuple(sorted(i)) for i in list]))
{(0, 1, 6): 2, (1, 4, 5): 2, (1, 2, 3): 1}
>>>
此外,list
是python关键字的副本,在创建了变量之后,您将无法再访问该关键字,因此最好重命名该变量,并在我的计算机中重命名该变量。代码。
编辑:
使用:
dict(Counter([tuple(sorted(x)) for i in list for x in i]))
答案 1 :(得分:0)
import pandas as pd
import numpy as np
# make a dataframe out of the list
df = pd.DataFrame(data=ll)
# sort columns and find duplicates
df_dup = df[pd.DataFrame(np.sort(df.values), columns=df.columns, index=df.index)
.duplicated(keep=False)]
# return duplicates as dictionary of tuples
result = df_dup.T.apply(tuple).to_dict()
# return number of duplicates found
n = len(result)
答案 2 :(得分:0)
list=[(0, 1, 6), (5,1,4), (1, 6, 0), (3, 2,1),(4,5,1)]
#first please make a list of sorted tuples to ignore the sequence of numbers
sorted_list = [sorted(x) for x in list]
#then count the tuple in sorted list and ignore count for duplicated items.
list = [ { x: sorted_list.count(sorted(x)) } for i, x in enumerate(list) if sorted(x) not in sorted_list[:i]]
print(list);
答案 3 :(得分:0)
您说您正在使用以下方法初始化列表:
mylist = [[[(x,y,z) for x in range(7)] for y in range(7)] for z in range(7)]
此时,您可以更改初始化方式:
from collections import Counter
# take off some square brackets
mylist = [(x,y,z) for x in range(7) for y in range(7) for z in range(7)]
dict(Counter(tuple(sorted(tup)) for tup in mylist))
输出:
{(0, 0, 0): 1, (0, 0, 1): 3, (0, 0, 2): 3, (0, 0, 3): 3, (0, 0, 4): 3, (0, 0, 5): 3, (0, 0, 6): 3, (0, 1, 1): 3, (0, 1, 2): 6, (0, 1, 3): 6, (0, 1, 4): 6, (0, 1, 5): 6, (0, 1, 6): 6, (0, 2, 2): 3, (0, 2, 3): 6, (0, 2, 4): 6, (0, 2, 5): 6, (0, 2, 6): 6, (0, 3, 3): 3, (0, 3, 4): 6, (0, 3, 5): 6, (0, 3, 6): 6, (0, 4, 4): 3, (0, 4, 5): 6, (0, 4, 6): 6, (0, 5, 5): 3, (0, 5, 6): 6, (0, 6, 6): 3, (1, 1, 1): 1, (1, 1, 2): 3, (1, 1, 3): 3, (1, 1, 4): 3, (1, 1, 5): 3, (1, 1, 6): 3, (1, 2, 2): 3, (1, 2, 3): 6, (1, 2, 4): 6, (1, 2, 5): 6, (1, 2, 6): 6, (1, 3, 3): 3, (1, 3, 4): 6, (1, 3, 5): 6, (1, 3, 6): 6, (1, 4, 4): 3, (1, 4, 5): 6, (1, 4, 6): 6, (1, 5, 5): 3, (1, 5, 6): 6, (1, 6, 6): 3, (2, 2, 2): 1, (2, 2, 3): 3, (2, 2, 4): 3, (2, 2, 5): 3, (2, 2, 6): 3, (2, 3, 3): 3, (2, 3, 4): 6, (2, 3, 5): 6, (2, 3, 6): 6, (2, 4, 4): 3, (2, 4, 5): 6, (2, 4, 6): 6, (2, 5, 5): 3, (2, 5, 6): 6, (2, 6, 6): 3, (3, 3, 3): 1, (3, 3, 4): 3, (3, 3, 5): 3, (3, 3, 6): 3, (3, 4, 4): 3, (3, 4, 5): 6, (3, 4, 6): 6, (3, 5, 5): 3, (3, 5, 6): 6, (3, 6, 6): 3, (4, 4, 4): 1, (4, 4, 5): 3, (4, 4, 6): 3, (4, 5, 5): 3, (4, 5, 6): 6, (4, 6, 6): 3, (5, 5, 5): 1, (5, 5, 6): 3, (5, 6, 6): 3, (6, 6, 6): 1}
如果您不想更改初始化方式:
def flatten(alist):
for item in alist:
if isinstance(item, list):
yield from flatten(item)
else:
yield item
dict(Counter(tuple(sorted(tup)) for tup in flatten(mylist)))
答案 4 :(得分:0)
建议,不要在python中使用关键字作为变量,这是python中的关键字:
import keyword
keyword_list = keyword.kwlist
对于您的问题,这是我的答案:
from collections import Counter
result = dict(Counter([tuple(sorted(item)) for item in your_list ]))