Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较列表列表的Python_Python_List - Fatal编程技术网

比较列表列表的Python

比较列表列表的Python,python,list,Python,List,我有以下格式的列表: [,source1,version1],,,source2,version2]…] 我需要比较每个列表,并构建一个包含唯一源值的新列表。当存在重复的源值时,我需要选择具有最高版本值的列表 另外,这是我应该使用的正确数据结构吗 您可以使用以下功能: >>lst=[['foo',1,2],'asdf',2,5],'bar',1,3]] >>>按原样导入itertools >>>从运算符导入itemgetter >>>[max(items,key=itemgetter(2)) g

我有以下格式的列表:

[,source1,version1],,,source2,version2]…]

我需要比较每个列表,并构建一个包含唯一源值的新列表。当存在重复的源值时,我需要选择具有最高版本值的列表

另外,这是我应该使用的正确数据结构吗

您可以使用以下功能:

>>lst=[['foo',1,2],'asdf',2,5],'bar',1,3]]
>>>按原样导入itertools
>>>从运算符导入itemgetter
>>>[max(items,key=itemgetter(2))
groupby(排序(lst,key=itemgetter(1)),key=itemgetter(1))]
[bar',1,3],[asdf',2,5]]
您可以使用以下功能:

>>lst=[['foo',1,2],'asdf',2,5],'bar',1,3]]
>>>按原样导入itertools
>>>从运算符导入itemgetter
>>>[max(items,key=itemgetter(2))
groupby(排序(lst,key=itemgetter(1)),key=itemgetter(1))]
[bar',1,3],[asdf',2,5]]

假设您的所有子列表都具有相同的三项结构,这似乎是一种相当合理的数据结构,因为您始终可以使用索引[0]、[1]和[2]访问图像对象、源和版本

这段代码使源代码成为字典的键,子列表列出这些键的值

bigList = [['foo', 1, 2], ['asdf', 2, 5], ['bar', 1, 3]]
uniqueSources = {}
for sublist in bigList:
    currentSource = sublist[1]
    if currentSource in uniqueSources:
        if sublist[2] > uniqueSources[currentSource][2]:
            uniqueSources[currentSource] = sublist
    else: uniqueSources[currentSource] = sublist
dupesRemoved = list(uniqueSources.values())
print(dupesRemoved)

假设您的所有子列表都具有相同的三项结构,那么使用该结构似乎是一种相当合理的数据结构,因为您始终可以使用索引[0]、[1]和[2]访问图像对象、源和版本

这段代码使源代码成为字典的键,子列表列出这些键的值

bigList = [['foo', 1, 2], ['asdf', 2, 5], ['bar', 1, 3]]
uniqueSources = {}
for sublist in bigList:
    currentSource = sublist[1]
    if currentSource in uniqueSources:
        if sublist[2] > uniqueSources[currentSource][2]:
            uniqueSources[currentSource] = sublist
    else: uniqueSources[currentSource] = sublist
dupesRemoved = list(uniqueSources.values())
print(dupesRemoved)