#### Find how many lists in list have the same element

``````list=[  [['AA','A0'],['AB','A0']],
[['AA','B0'],['AB','A0']],
[['A0','00'],['00','A0'], [['00','BB'],['AB','A0'],['AA','A0']] ]
]
``````

``````for t in range(0,len(list)-1):
pattern=[]
flag=True
pattern.append(list[t])
count=1
rest=list[t+1:]
for p in pattern:
for j in p:
if flag==False:
break
pair= j
for i in rest:
for y in i:
if pair==y:
count=count+1
break
if brojac==len(list):
flag=False
break
``````
5 个回复

``````alist=[[['AA','A0'],['AB','A0']],[['AA','B0'],['AB','A0']],[['A0','00'],['00','A0'],[['00','BB'],['AB','A0'],['AA','A0']]]]

def count_in_list(val, arr):
val_is_list = isinstance(val, list)
ct = 0
for item in arr:
item_is_list = isinstance(item, list)
if item == val or (val_is_list and item_is_list and sorted(item) == sorted(val)):
ct += 1
if item_is_list :
ct += count_in_list(val, item)
return ct

print count_in_list(['AB', 'A0'], alist)
``````

``````from collections import defaultdict

d = defaultdict(int)

def counter(lst,  d):
it = iter(lst)
nxt = next(it)
while nxt:
if isinstance(nxt, list):
if nxt and isinstance(nxt[0], str):
d[tuple(nxt)] += 1
rev = tuple(reversed(nxt))
if rev in d:
d[rev] += 1
else:
lst += nxt
nxt = next(it,"")
return d

print((counter(lst, d)['AB', 'A0'])
3
``````

``````def counter(lst,  ele):
it = iter(lst)
nxt = next(it)
count = 0
while nxt:
if isinstance(nxt, list):
if ele in (nxt, nxt[::-1]):
count += 1
else:
lst += nxt
nxt = next(it, "")
return count

print(counter(lst, ['AB', 'A0']))
3
``````

1. 摆脱多个嵌套列表，
2. 计算内部列表中所有值对的出现，并
3. 从计数结果中提取最常出现的值对。

1。

“哟，我想你喜欢清单，所以我把清单放在清单里……”

``````def get_inner_lists(some_list):
inner_lists = []
for item in some_list:
if hasattr(item, '__iter__') and not isinstance(item, basestring):
if hasattr(item[0], '__iter__') and not isinstance(item[0], basestring):
inner_lists.extend(get_inner_lists(item))
else:
inner_lists.append(item)
return inner_lists
``````

``````>>> foo = [[['AA','A0'],['AB','A0']],[['AA','B0'],['AB','A0']],[['A0','00'],['00','A0'],[['00','BB'],['AB','A0'],['AA','A0']]]]
>>> print get_inner_lists(foo)

[['AA', 'A0'], ['AB', 'A0'], ['AA', 'B0'], ['AB', 'A0'], ['A0', '00'], ['00', 'A0'], ['00', 'BB'], ['AB', 'A0'], ['AA', 'A0']]
``````

2。

``````def count_list_values(some_list):
result = {}
for item in some_list:
str = item[0]+'-'+item[1]
if not str in result.keys():
result[str] = 1
else:
result[str] += 1
return result
``````

``````>>> print count_list_values(get_inner_lists(foo))

{'00-A0': 1, '00-BB': 1, 'A0-00': 1, 'AB-A0': 3, 'AA-A0': 2, 'AA-B0': 1}
``````

3。

``````def get_max_dict_value(some_dict):
all_keys = []
all_values = []
for key, val in some_dict.items():
all_keys.append(key)
all_values.append(val)
return all_keys[all_values.index(max(all_values))].split('-')
``````

``````>>> print get_max_dict_value(count_list_values(get_inner_lists(foo)))

['AB', 'A0']
``````

...您等到某个Guru出现并给您一个我从未见过的超快速，优雅的单行python命令时，它将执行相同的操作;-)

``````from collections import Counter

lst = [  [['AA','A0'],['AB','A0']],
[['AA','B0'],['AB','A0']],
[['A0','00'],['00','A0'], [['00','BB'],['AB','A0'],['AA','A0']] ]
]

def is_leaf(element):
return (isinstance(element, list) and
len(element) == 2 and
isinstance(element[0], basestring)
and isinstance(element[1], basestring))

def traverse(iterable):
for element in iterable:
if is_leaf(element):
yield tuple(sorted(element))
else:
for value in traverse(element):
yield value

value, count = Counter(traverse(lst)).most_common(1)[0]
print 'Value {!r} is present {} times'.format(value, count)
``````

`traverse()`生成产生一系列已排序的元组，它们表示列表中的每个项目。 `Counter`对象对每个对象的出现次数进行计数，其`.most_common(1)`方法返回最常见项目的值和计数。

``````def Compare(List):
#Assuming that the list input is a simple list like ["A1","B0"]
myList =[[['AA','A0'],['AB','A0']],[['AA','B0'],['AB','A0']],[['A0','00'],['00','A0'],[['00','BB'],['AB','A0'],['AA','A0']]]]

#Create a counter that will count if the elements are the same
myCounter = 0;
for innerList1 in myList:
for innerList2 in innerList1
for innerList3 in innerList2
for element in innerList3
for myListElements in myList
if (myListElements == element)
myCounter = myCounter + 1;

#I am putting the break here so that it counts how many lists have the
#same elements, not how many elements are the same in the lists
break;

return myCounter;
``````
1 如何计算一个列表中有多少个元素相同？

2013-05-24 08:56:41 4 76   java
2 方案列表中有多少个元素

2010-01-05 20:03:06 3 8417   scheme
3 Java检查两个列表中有多少个元素相同

2019-10-07 20:38:18 4 49   java
4 查找大量列表具有相同元素的列表

2019-07-12 15:35:59 2 48   list/ set
5 查找列表中有多少个嵌套字典键

2018-01-02 21:21:36 2 62   json
7 列表中有多少个单词

2017-10-28 00:21:54 1 56   python
10 查找边缘列表中有多少个重复连接[python]

© 2020-2021 STACKOOM.COM