如何计算列表列表中的不同元素? [英] How to count different elements in list of lists?

查看:77
本文介绍了如何计算列表列表中的不同元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算列表列表中唯一列表的数量. 例如,

I would like to count the number of unique lists, inside a list of lists. For instance,

>>>list1 = [[1,2,3],[1,2,3],[1,2,2],[1,2,2]]

>>>how_many_different_lists(list1)

>>>2 #They are [1,2,3] and [1,2,2]

如何制作how_many_different_lists函数?

How can I make the how_many_different_lists function?

推荐答案

这里的工作代码:

from copy import deepcopy

def how_much_dif_l(arg):
    arg_list=deepcopy(arg)
    i=0
    length=len(arg_list)
    while i<length:
        a = arg_list[i]
        if arg_list.count(a)>1:
            length-=1
            arg_list.remove(a)
        else: 
            i+=1


    return len(arg_list)

list1= [[1,2,3],[1,2,3],[1,2,2],[1,2,2]]
print(how_much_dif_l(list1))

这篇关于如何计算列表列表中的不同元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆