如何获取列表列表中的重复项数量?

时间:2018-10-28 15:08:08

标签: python python-3.x

a=[[2, 5, 21,],
  [2, 9, 14,],
  [2, 22, 32],
  [3, 10, 13],
  [3, 10, 13]
  [3, 10, 13]]

for i in range(len(a)):
  cnt=1                  #count
for j in range(i, len(a)):

    if (i==j):
        continue
    elif (len(set(a[i])&set(a[j]))==6):
        cnt+=1
        print('\t{:2} {:2} {:2} {:2} {:2} {:2}, number {:2} '.format(*a[i],cnt))
    else:
        pass

我要创建的代码在下面

  

[3,10,13],数字3

如何计算列表列表?

1 个答案:

答案 0 :(得分:1)

如果将内部列表转换为元组,则可以使用collections.Counter query = new SqlCommand ("Select UserType from singleTable where UserName = @userName", conn); query.Parameters.AddWithValue("@userName", userName.Text); dr = query.ExecuteReader (); if (dr.Read ()) { MessageBox.Show ("Login is successful. Welcome '"+ userName.Text +"'"); if(Convert.ToInt32(dr["UserType"]) == 1) { adminPanel form = new adminPanel (); } else if (Convert.ToInt32(dr["UserType"]) == 2) { teacherPanel form = new teacherPanel (); } else if (Convert.ToInt32(dr["UserType"])== 3) { studentPanel form = new studentPanel (); } } 是不可哈希的-list需要哈希密钥-fe dict):

tuples

输出:

from collections import Counter

a=[[2, 5, 21,],
  [2, 9, 14,],
  [2, 22, 32],
  [3, 10, 13],
  [3, 10, 13],
  [3, 10, 13]]

c = Counter( map(tuple,a) )   # shorter notation for: ( tuple(item) for item in a) )

# extract all (key,value) tuples with values > 1
for what, how_much in  (x for x in c.most_common() if x[1] > 1):  

    # 3.6 string interpol, use  "{} num {}".format(list(what),how_much) else
    print(f"{list(what)} num {how_much}") 

您还可以使用itertools.groupby(),但必须先对列表进行排序:

[3, 10, 13] num 3

相同的结果。 this answer误导了itertools的用法,以便在寻找此操作的重复项时“如何从列表列表中删除重复项”