Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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中,如何比较列表中的重复元素并给出同一列表中另一个元素的IF条件_Python - Fatal编程技术网

在python中,如何比较列表中的重复元素并给出同一列表中另一个元素的IF条件

在python中,如何比较列表中的重复元素并给出同一列表中另一个元素的IF条件,python,Python,朋友 我陷入了问题清单中。 我有以下类型的列表包含['Score'、'title'、'ID'] [[0.6428571428571429, 'Software QA', 2089], [0.7428571428571429, 'Software Developer', 2086], [0.75, 'Software Tester', 2004], [0.7428571428571429, 'Software Developer', 2004], [0.7428571428571429,

朋友

我陷入了问题清单中。 我有以下类型的列表包含['Score'、'title'、'ID']

[[0.6428571428571429, 'Software QA', 2089],
 [0.7428571428571429, 'Software Developer', 2086],
 [0.75, 'Software Tester', 2004],
 [0.7428571428571429, 'Software Developer', 2004],
 [0.7428571428571429, 'Software Developer', 1958],
 [0.7428571428571429, 'Software Developer', 1954],
 [0.7428571428571429, 'Software Developer', 2133],
 [0.7428571428571429, 'Software Developer', 2153],
 [0.7428571428571429, 'Software Developer', 2152],
 [0.9411764705882353, 'Software Engineer', 1107],
 [0.7058823529411765, 'Software Devloper', 2133],
 [0.7058823529411765, 'Software Devloper', 2153],
 [0.7058823529411765, 'Software Devloper', 2152],]
现在我想比较一下ID,如果他们相似的话,那么需要给出分数更高的条件

我试过的一个元素代码如下:

if cleanlist[2][2] == cleanlist[i][2]:
        if cleanlist[2][0] > cleanlist[i][0]:
            print (cleanlist[2])
        else:
            print(cleanlist[i])
[[0.6428571428571429, 'Software QA', 2089],
 [0.7428571428571429, 'Software Developer', 2086],
 [0.75, 'Software Tester', 2004],
 [0.7428571428571429, 'Software Developer', 1958],
 [0.7428571428571429, 'Software Developer', 1954],
 [0.7428571428571429, 'Software Developer', 2133],
 [0.7428571428571429, 'Software Developer', 2153],
 [0.7428571428571429, 'Software Developer', 2152],
 [0.9411764705882353, 'Software Engineer', 1107],]
我如何在循环中完成并获得具有分数的唯一ID

期望输出如下:

if cleanlist[2][2] == cleanlist[i][2]:
        if cleanlist[2][0] > cleanlist[i][0]:
            print (cleanlist[2])
        else:
            print(cleanlist[i])
[[0.6428571428571429, 'Software QA', 2089],
 [0.7428571428571429, 'Software Developer', 2086],
 [0.75, 'Software Tester', 2004],
 [0.7428571428571429, 'Software Developer', 1958],
 [0.7428571428571429, 'Software Developer', 1954],
 [0.7428571428571429, 'Software Developer', 2133],
 [0.7428571428571429, 'Software Developer', 2153],
 [0.7428571428571429, 'Software Developer', 2152],
 [0.9411764705882353, 'Software Engineer', 1107],]
输出:

[[0.6428571428571429, 'Software QA', 2089], [0.7428571428571429, 'Software Developer', 2086], [0.75, 'Software Tester', 2004], [0.7428571428571429, 'Software Developer', 2004], [0.7428571428571429, 'Software Developer', 1958], [0.7428571428571429, 'Software Developer', 1954], [0.7428571428571429, 'Software Developer', 2133], [0.7428571428571429, 'Software Developer', 2153], [0.7428571428571429, 'Software Developer', 2152], [0.9411764705882353, 'Software Engineer', 1107], [0.7058823529411765, 'Software Devloper', 2133], [0.7058823529411765, 'Software Devloper', 2153], [0.7058823529411765, 'Software Devloper', 2152]]
使用字典可能是一个比把所有的东西都放回清单要好得多的方法

cleanlist = [[0.6428571428571429, 'Software QA', 2089],
 [0.7428571428571429, 'Software Developer', 2086],
 [0.75, 'Software Tester', 2004],
 [0.7428571428571429, 'Software Developer', 2004],
 [0.7428571428571429, 'Software Developer', 1958],
 [0.7428571428571429, 'Software Developer', 1954],
 [0.7428571428571429, 'Software Developer', 2133],
 [0.7428571428571429, 'Software Developer', 2153],
 [0.7428571428571429, 'Software Developer', 2152],
 [0.9411764705882353, 'Software Engineer', 1107],
 [0.7058823529411765, 'Software Devloper', 2133],
 [0.7058823529411765, 'Software Devloper', 2153],
 [0.7058823529411765, 'Software Devloper', 2152]]

donelist = set()

for item in cleanlist:
    max = item[2]
    output = item
    if(item[2] not in donelist):
        for item1 in cleanlist[cleanlist.index(item):]:
            if(item[2] == item1[2] and item1[0]>max):
                output = item1
        donelist.add(item[2])
        print(output)

看看这是否能帮你得到它。

如果你不介意输出的顺序,你可以使用它

from collections import defaultdict

def sort_it(list_):
    res = defaultdict(list)
    for i, j, k in sorted(list_):
        res[k] = [i, j]
    return [[value[0], value[1], key] for key, value in res.items()]
如果您希望按照ID进行排序,那么只需重新排序即可

sorted(sort_it(test_list), key=lambda x: x[-1])
工作:首先根据分数对输入列表进行排序,然后将值输入一个dict,ID作为键,rest in list作为其值,ID值的任何重复出现都将替换为大于先前值的新值,然后将其重新转换为列表。

尝试以下操作:

from collections import OrderedDict

def best_scores(scores):
    best_scores = OrderedDict()
    for line in scores:
        score, _, my_id = line
        if my_id not in best_scores or score > best_scores[my_id][0]:
            best_scores[my_id] = line
    return list(best_scores.values())

NB:如果你不需要保存行的顺序,你可以使用一个标准的
dict
而不是
orderedict

不要使用dict.keys()中的
项进行查找,它是O(N),使用dict中的
项,它是O(1)(高度优化)。我不知道。谢谢你的提醒。更改答案不要使用
range
和索引来迭代列表,而是对列表中的项使用
我需要范围,因为我没有重复整个列表两次,正如您从代码中看到的那样。您仍然不需要范围。哦,是的,您应该使用
集合
来进行
donelist
(O(1)优化查找而不是O(N)查找)。感谢您的注释,我们将根据建议调查并更改答案。@Brunodesshuilliers这就是您的意思(答案已编辑)?非常感谢您。我在我的逻辑中遗漏了一部分,你现在把它放在这里,它的工作原理,你能解释一下它是如何工作的,这样它在将来会很有帮助吗?非常感谢,我现在就知道了
from collections import OrderedDict

def best_scores(scores):
    best_scores = OrderedDict()
    for line in scores:
        score, _, my_id = line
        if my_id not in best_scores or score > best_scores[my_id][0]:
            best_scores[my_id] = line
    return list(best_scores.values())