Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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和基于最小int值的数据排序_Python - Fatal编程技术网

Python和基于最小int值的数据排序

Python和基于最小int值的数据排序,python,Python,我有大量重复的数据(元组列表)。我试图只识别得分较低的员工 例如,“雇主1”有“约翰·史密斯”和“艾伦·史密斯”。这些是管理者,int值表示他们的权重/分数。我只想为每个雇主选择值较低的行。如您所见,“雇主3”只有一个条目的权重为1。因此,无需进行选择,因为它是该雇主的下限值 dataset = [ ('Employer 1', 'Video Editor', '2018-05-08 10:22:31', 'Dataset1', 'John Smith', '2'), (

我有大量重复的数据(元组列表)。我试图只识别得分较低的员工

例如,“雇主1”有“约翰·史密斯”和“艾伦·史密斯”。这些是管理者,int值表示他们的权重/分数。我只想为每个雇主选择值较低的行。如您所见,“雇主3”只有一个条目的权重为1。因此,无需进行选择,因为它是该雇主的下限值

    dataset = [
    ('Employer 1', 'Video Editor', '2018-05-08 10:22:31', 'Dataset1', 'John Smith', '2'),
    ('Employer 1', 'Video Editor', '2018-05-08 10:22:31', 'Dataset2', 'Allan Smith', '1'),
    ('Employer 1', 'Video Editor', '2018-05-08 10:22:31', 'Dataset3', 'John Smith', '2'),
    ('Employer 1', 'Video Editor', '2018-05-08 10:22:31', 'Dataset2', 'Allan Smith', '1'),
    ('Employer 1', 'Video Editor', '2018-05-08 10:22:31', 'Dataset1', 'John Smith', '2'),
    ('Employer 1', 'Video Editor', '2018-05-08 10:22:31',  'Dataset3', 'Allan Smith', '1'),
    ('Employer 2', 'Cook', '2018-05-08 10:22:31',  'Dataset1', 'james bond', '3'),
    ('Employer 2', 'Cook', '2018-05-08 10:22:31',  'Dataset1', 'james bond', '3'),
    ('Employer 2', 'Cook', '2018-05-08 10:22:31',  'Dataset1', 'james brown', '1'),
    ('Employer 2', 'Cook', '2018-05-08 10:22:31',  'Dataset1', 'james brown', '1'),
    ('Employer 3', 'Cook', '2018-05-08 10:22:31',  'Dataset1', 'james brown', '1'),
    ('Employer 3', 'Cook', '2018-05-08 10:22:31',  'Dataset1', 'james brown', '1')]

您可以将
collections.defaultdict
min
一起使用:

import collections
d = collections.defaultdict(list)
for a, *b in dataset:
  d[a].append(b)

results = {a:min(b, key=lambda x:int(x[-1])) for a, b in d.items()}
输出:

{'Employer 1': ['Video Editor', '2018-05-08 10:22:31', 'Dataset2', 'Allan Smith', '1'], 'Employer 2': ['Cook', '2018-05-08 10:22:31', 'Dataset1', 'james brown', '1'], 'Employer 3': ['Cook', '2018-05-08 10:22:31', 'Dataset1', 'james brown', '1']}
请注意,上述语法(解包)仅适用于Python3。然而,下面是一个非常类似于Python2的解决方案:

import collections
d = collections.defaultdict(list)
for i in dataset:
  d[i[0]].append(i[1:])

results = {a:min(b, key=lambda x:int(x[-1])) for a, b in d.items()}

如果我理解正确,您正在寻找:

  • 各组的min
  • 具有该最小值的组中每个员工的列表
  • 此groupby循环执行以下操作:

    from itertools import groupby
    
    for k,g in groupby(sorted(dataset), key=lambda t: t[0]):
        g=list(g)
        low=min(g, key=lambda t: int(t[-1]))[-1]
        print k, list({t for t in g if t[-1]==low}) 
    
    印刷品:

    Employer 1 [('Employer 1', 'Video Editor', '2018-05-08 10:22:31', 'Dataset2', 'Allan Smith', '1'), ('Employer 1', 'Video Editor', '2018-05-08 10:22:31', 'Dataset3', 'Allan Smith', '1')]
    Employer 2 [('Employer 2', 'Cook', '2018-05-08 10:22:31', 'Dataset1', 'james brown', '1')]
    Employer 3 [('Employer 3', 'Cook', '2018-05-08 10:22:31', 'Dataset1', 'james brown', '1')]
    

    只有在组中确实有dup元组时才需要集合理解…

    到目前为止您做了什么?查看
    groupby
    ,然后查看
    sorted
    ,然后查看第一个元素。当你说“较低的值”时,你是指每个员工的最低值吗?你有一个星号分配,仅限Python 3。这和@dawg的答案解决了我面临的问题,请你把答案分解一下,解释一下发生了什么。是的,这是正确的,我希望雇主的每一次迭代都有最低的权重分数。这和上面的答案都是正确的。你能把for循环分解一下,解释一下它在做什么吗。