简体   繁体   English

遍历python中的list并获得给定元素的最大值

[英]Iterate through list in python and get highest value for a given element

I have a CSV file made like so: 我有一个这样的CSV文件:

business unit; employee id; name; tax code;
1; 50; JOE BLOGGS; 123456789
1; 51; JOE BLOGGS; 123456789
1; 52; JOE BLOGGS; 123456789
3; 53; JOE BLOGGS; 123456789
5; 54; JOE BLOGGS; 123456789

The tax code is unique while the business units and id may vary. 税码是唯一的,而业务单位和ID可能会有所不同。 Since the employee I need is always the last one, it being his most recent and thus active working position, how can I loop through this file and append ONLY the last row to the array? 由于我需要的员工始终是最后一位,这是他最近的工作职位,因此我该如何循环浏览该文件并将仅最后一行追加到数组中? ( Edit: the last line or better the highest id referring to that particular person, there might be other employees in the file ) 编辑:最后一行或引用该特定人员的最高ID更好,文件中可能还有其他员工

my code is: 我的代码是:

for line in csv:
 l = [i.strip() for i in line.split(';')]
 if l[3] not in d:
  d.append(l[3])
  c.append(l[2])
  b.append(l[1])
  a.append(l[0])

this way I obtain as a result a list containing only the first record, how can I get the last one? 这样,我将获得仅包含第一条记录的列表,如何获得最后一条? Thank you! 谢谢!

If I understand well, you'd like to get the last record for each tax code . 据我了解,您想获取每个tax code的最后记录。 Try this, 尝试这个,

d = dict() # key by `tax code`
for line in csv:
    l = [i.strip() for i in line.split(';')]

    d[l[3]] = l # update

lists = d.values()

If your file is grouped by tax code , use itertools.groupby , 如果您的文件按tax code分组,请使用itertools.groupby

import csv
import operator
import itertools

with open(filename, 'r') as f:
    header = next(f, None)
    for key, group in itertools.groupby(f, key=operator.itemgetter(3)):
        last_line = list(group)[-1]

        print(last_line)
        5; 54; JOE BLOGGS; 123456789

Previous answer: read the last line from a csv file. 上一个答案:从csv文件中读取最后一行。

Way 1: Use csv.reader 方法1:使用csv.reader

skipinitialspace=True is used to get rid of whitespace following the delimiter ; skipinitialspace=True用于消除定界符后的空白; .

import csv

with open(filename, 'r') as f:
    headers = next(f, None) # the header
    lists = [row for row in csv.reader(f, delimiter=';', skipinitialspace=True)]

    print(lists[-1])
    # Output
    ['5', '54', 'JOE BLOGGS', '123456789']

Way 2: Use collections.deque 方法2:使用collections.deque

import csv
import collections

with open(filename, 'r') as f:
    last_line = collections.deque(csv.reader(f), 1)[0][0]

    print(last_line)
    # Output
    5; 54; JOE BLOGGS; 123456789

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM