def mkEntry(file1):
for line in file1:
lst = (line.rstrip().split(","))
print("Old", lst)
print(type(lst))
lst = tuple(lst)
print(type(lst)) #still showing type='list'
print("Old2", lst)
sorted(lst, key=operator.itemgetter(1,2))
def main():
openFile = 'yob' + input("Enter the year <Do NOT include 'yob' or .'txt' : ") + '.txt'
file1 = open(openFile)
mkEntry(file1)
main()
我收到的错误:
Traceback (most recent call last):
File "/Users/ranbir/Desktop/names/top101.py", line 20, in <module>
main()
File "/Users/ranbir/Desktop/names/top101.py", line 18, in main
mkEntry(file1)
File "/Users/ranbir/Desktop/names/top101.py", line 12, in mkEntry
sorted(lst, key=operator.itemgetter(1,2))
IndexError: string index out of range
文件:
Tom,M,1658
Anthony,M,985
Lisa,F,88976
Ben,M,6989
Shelly,F,8975
我需要帮助排序从最小数字到最大数字F
的顺序。M
输出应如下所示:
Shelly,F,8975
Lisa,F,8976
Ben,M,698
Anthony,M,985
Tom,M,1658
所以基本上,女性首先是最小到最大的数字,然后是男性,然后是最小到最大的数字(M=male
, f=female
)。