简体   繁体   English

Python-列表未转换为元组以进行排序

[英]Python - List not converting to Tuple inorder to Sort

def mkEntry(file1):
    for line in file1:
        lst = (line.rstrip().split(","))
        print("Old", lst)
        print(type(lst))
        tuple(lst)
        print(type(lst)) #still showing type='list'
        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()

TextFile: 文本文件:

Emma,F,20791
Tom,M,1658
Anthony,M,985
Lisa,F,88976
Ben,M,6989
Shelly,F,8975

and I get this output: 我得到以下输出:

IndexError: string index out of range

I am trying to convert the lst to Tuple from List. 我正在尝试从列表将lst转换为Tuple。 So I will able to order the F to M and Smallest Number to Largest Numbers. 因此,我将能够订购F到M以及最小数字到最大数字。 In around line 7, it's still printing type list instead of type tuple. 在第7行附近,它仍在打印类型列表,而不是类型元组。 I don't know why it's doing that. 我不知道为什么要这么做。

    print(type(lst))
    tuple(lst)
    print(type(lst)) #still showing type='list'

You're not changing what lst refers to. 您没有更改lst所指的内容。 You create a new tuple with tuple(lst) and immediately throw it away because you don't assign it to anything. 您使用tuple(lst)创建一个新的tuple ,并立即将其丢弃,因为您没有将其分配给任何东西。 You can do: 你可以做:

lst = tuple(lst)

Note that this will not fix your program. 请注意,这不会修复您的程序。 Notice that your sort operation is happening once per line of your file, which is not what you want. 请注意,您的文件每行仅发生一次排序操作,这不是您想要的。 Try collecting each line into one sequence of tuples and then doing the sort. 尝试将每一行收集到一个元组序列中, 然后进行排序。

Firstly, you are not saving the tuple you created anywhere: 首先,您不保存在任何地方创建的元组:

tup = tuple(lst)

Secondly, there is no point in making it a tuple before sorting it - in fact, a list could be sorted in place as it's mutable, while a tuple would need another copy (although that's fairly cheap, the items it contains aren't copied). 其次,没有必要在对其进行排序之前将其设为一个元组-实际上,由于可变性,列表可以在适当的位置进行排序,而一个元组则需要另一个副本(尽管相当便宜,但是其中包含的项目不会被复制) )。

Thirdly, the IndexError has nothing to do with whether it's a list or tuple, nor whether it is sorted. 第三,IndexError与它是列表还是元组无关,也不与它是否排序有关。 It most likely comes from the itemgetter, because there's a list item that doesn't have three entries in turn - for instance, the strings "F" or "M". 它最有可能来自itemgetter,因为有一个列表项依次没有三个条目,例如,字符串“ F”或“ M”。

Fourthly, the sort you're doing, but not saving anywhere, is done on each individual line, not the table of data. 第四,您正在执行但不在任何地方保存的排序是在每一行而不是数据表上进行的。 Considering this means you're comparing a name, a number, and a gender, I rather doubt it's what you intended. 考虑到这意味着您正在比较名称,数字和性别,我相当怀疑这是您想要的。

It's completely unclear why you're trying to convert data types, and the code doesn't match the structure of the data. 现在还不清楚为什么要尝试转换数据类型,并且代码与数据的结构不匹配。 How about moving back to the overview plan and sorting out what you want done? 回到总览计划并整理您想做什么呢? It could well be something like Python's csv module could help considerably. 很有可能像Python的csv模块可以提供很大帮助。

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

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