时间:2020-12-27 16:23:57 | 栏目:Python代码 | 点击:次
前段时间前在网上看到一段面试题,要求如下:
employee文件中记录了工号和姓名
100 Jason Smith
200 John Doe
300 Sanjay Gupta
400 Ashok Sharma
100 $5,000
200 $500
300 $3,000
400 $1,250
100 jason smith $5,000
200 john doe $500
300 sanjay gupta $3,000
这个是要求用shell来写的,但我的shell功底不怎么样,就用python来实现了
注意,按题目的意思,在输出文件中还需要按照姓名首字母来排序的,实现代码
fp02 = open("employee.txt", "r")
fc02 = sorted(fp02, key = lambda x:x.split()[1])
for line02 in fc02:
i = 0
while line02.split()[0]!=a[i].split()[0]:
i += 1
print "%s %s %s %s" % (line02.split()[0], line02.split()[1], line02.split()[2], a[i].split()[1])
fp01.close()
fp02.close()