从字典创建列表或只是简单地对其进行排序

时间:2017-03-11 07:29:06

标签: python python-3.x dictionary

我有以下代码:

import os
import pprint

file_path = input("Please, enter the path to the file: ")
if os.path.exists(file_path):
    worker_dict = {}
    k = 1
    for line in open(file_path,'r'):
        split_line = line.split()
        worker = 'worker{}'.format(k)
        worker_name = '{}_{}'.format(worker, 'name')
        worker_yob = '{}_{}'.format(worker, 'yob')
        worker_job = '{}_{}'.format(worker, 'job')
        worker_salary = '{}_{}'.format(worker, 'salary')
        worker_dict[worker_name] = ' '.join(split_line[0:2])
        worker_dict[worker_yob] = ' '.join(split_line[2:3])
        worker_dict[worker_job] = ' '.join(split_line[3:4])
        worker_dict[worker_salary] = ' '.join(split_line[4:5])
        k += 1
else:
    print('Error: Invalid file path')

文件:

John Snow 1967 CEO 3400$ 
Adam Brown 1954 engineer 1200$

来自worker_dict的输出:

{
 'worker1_job': 'CEO',
 'worker1_name': 'John Snow',
 'worker1_salary': '3400$',
 'worker1_yob': '1967',
 'worker2_job': 'engineer',
 'worker2_name': 'Adam Brown',
 'worker2_salary': '1200$',
 'worker2_yob': '1954',
}

我希望按工人名称对数据进行排序,然后按工资对数据进行排序。所以我的想法是创建一个单独的列表,其中包含薪水和工人姓名以进行排序。但我填写它有问题,也许有更优雅的方法来解决我的问题?

3 个答案:

答案 0 :(得分:0)

import os
import pprint

file_path = input("Please, enter the path to the file: ")
if os.path.exists(file_path):
    worker_dict = {}
    k = 1
    with open(file_path,'r') as file:
        content=file.read().splitlines()
    res=[]
    for i in content:
        val = i.split()
        name = [" ".join([val[0],val[1]]),]#concatenate first name and last name
        i=name+val[2:] #prepend name
        res.append(i) #append modified value to new list
    res.sort(key=lambda x: x[3])#sort by salary
    print res
    res.sort(key=lambda x: x[0])#sort by name
    print res

输出:

[['Adam Brown', '1954', 'engineer', '1200$'], ['John Snow', '1967', 'CEO', '3400$']]
[['Adam Brown', '1954', 'engineer', '1200$'], ['John Snow', '1967', 'CEO', '3400$']]

答案 1 :(得分:0)

d = {
 'worker1_job': 'CEO',
 'worker1_name': 'John Snow',
 'worker1_salary': '3400$',
 'worker1_yob': '1967',
 'worker2_job': 'engineer',
 'worker2_name': 'Adam Brown',
 'worker2_salary': '1200$',
 'worker2_yob': '1954',
}

from itertools import zip_longest

#re-group: 
def grouper(iterable, n, fillvalue=None):
      "Collect data into fixed-length chunks or blocks"
       # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
       args = [iter(iterable)] * n
       return zip_longest(*args, fillvalue=fillvalue)

#re-order: 
res = []
for group in list(grouper(d.values(), 4)):
          reorder = [1,2,0,3]
          res.append([ group[i] for i in reorder])

#sort:
res.sort(key=lambda x: (x[1], x[2]))

输出:

[['Adam Brown', '1200$', 'engineer', '1954'],
 ['John Snow', '3400$', 'CEO', '1967']]

Grouper在itertools中定义和解释。我已根据每个工作人员的记录对您的字典进行分组,并将其作为重新排序的列表列表返回。作为列表,我按名称和工资对它们进行排序。这是解决方案是模块化的:它明确地分组,重新排序和排序。

答案 2 :(得分:0)

我建议以不同的格式存储工作人员,例如.csv,然后您可以使用csv.DictReader并将其放入字典列表中(这也可以让您使用作业,名称等等更多的单词,如"古墓丽影")。

请注意,您必须将出生年份和工资转换为整数或浮点数才能正确排序,否则它们会按字典顺序排序,因为它们是字符串,例如:

>>> sorted(['100', '11', '1001'])
['100', '1001', '11']

要对词典列表进行排序,您可以使用operator.itemgetter作为sorted的关键参数,而不是lambda函数,只需将所需的键传递给itemgetter即可。

k变量没用,因为它只是列表的len

.csv文件:

"name","year of birth","job","salary"
John Snow,1967,CEO,3400$
Adam Brown,1954,engineer,1200$
Lara Croft,1984,tomb raider,5600$

.py文件:

import os
import csv
from operator import itemgetter
from pprint import pprint


file_path = input('Please, enter the path to the file: ')
if os.path.exists(file_path):
    with open(file_path, 'r', newline='') as f:
        worker_list = list(csv.DictReader(f))
        for worker in worker_list:
            worker['salary'] = int(worker['salary'].strip('$'))
            worker['year of birth'] = int(worker['year of birth'])

    pprint(worker_list)
    pprint(sorted(worker_list, key=itemgetter('name')))
    pprint(sorted(worker_list, key=itemgetter('salary')))
    pprint(sorted(worker_list, key=itemgetter('year of birth')))

如果int转换失败,或者只是让程序崩溃,你仍然需要一些错误处理。