简体   繁体   English

我有一个包含字母和数字列表的txt文件,我需要根据数字对它进行排序,我该怎么做?

[英]I have a txt file with a list of letters and numbers, and I need to sort it based on the numbers, how do I go about doing this?

What I have so far is a program that reads a text file, and puts it into a list. 到目前为止,我有一个程序可以读取文本文件,并将其放入列表中。 The txt file looks like: txt文件如下所示:

asdf:15.3 eazzz:31.12 qw:1.65 ...

I am kind of new to Python so I'm still trying to figure out how to do this properly. 我是Python的新手,因此我仍在尝试找出如何正确执行此操作。 I know this will separate the numbers from the letters, but I don't know where to go from here. 我知道这会将数字和字母分开,但是我不知道从这里去哪里。

def get_num(str):
    items = str.split(':')
    return float(items[1])

The desired output will have the numbers sorted out in descending order with their respective letters. 所需的输出将具有按降序排列的数字及其相应的字母。 I know this can probably be done with "sort" but if possible I'd also like to see it done with for-loops. 我知道可以使用“ sort”完成此操作,但如果可能的话,我也希望使用for循环完成此操作。

Don't use str as the input name. 不要使用str作为输入名称。 To sort a list containing tuples (name, value) just do the following: 要对包含元组(name, value)的列表进行排序(name, value)只需执行以下操作:

sorted(list_name, key = lambda x : x[1], reverse = True)

key = lambda x : x[1] indicates that you want to sort the list by value while reverse = True indicates that you want the sort to be in descending order. key = lambda x : x[1]表示要按value对列表进行排序,而reverse = True表示要按降序对列表进行排序。

data  = '''asdf:15.3
eazzz:31.12
qw:1.65
adasf:7.3'''

# convert string to list
lst = data.split('\n')

#sort list of data by two things. first the string part second number part
print '\n'.join(sorted(lst, key=lambda item:(item.split(':')[0],float(item.split(':')[1])), reverse = True))

output: 输出:

qw:1.65
eazzz:31.12
asdf:15.3
adasf:7.3

暂无
暂无

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

相关问题 如何根据最后两位数字对数字列表进行排序? - How do I sort a list of numbers based on the last two digits? 如何将数字转换为字母的python列表? - How do I convert a python list which is in numbers into letters? 我需要知道如何对这个列表进行排序并删除数字 - I need to know how to sort this list and remove the numbers JSON 中的数据有我不需要的字母和数字,如何在 Python 中获取我需要的数据 - Data inside a JSON has letters and numbers I do not need, how to get data I need in Python 我有一个包含数百万个序列的 fasta 文件。 我只想提取那些与 a.txt 文件中的名称匹配的内容,我该如何执行此操作 go? - I have a fasta file with millions of sequences. I want to only extract those that's names match within a .txt file, how can I go about doing this? 如何保存我已经列出的号码列表? - How do I keep a list of numbers I have already enountered? 如何添加已填写列表的随机数? - How do I add the random numbers I have filled a list with? 如何根据数字对该文件进行排序并显示给读者? - How do I sort this file according to the numbers and display it to the readers? 如何使用字符串和数字对列表进行排序(外部文本文件) - How do I sort my list with string and numbers (external text file) Python:如何在不使用 median() 调用的情况下读取数字的 .txt 文件以查找所述数字的中位数? - Python: How do I read a .txt file of numbers to find the median of said numbers without using median() call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM