Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当行以字符串开头时,如何按整数对文本文件进行排序?_Python_Python 2.7 - Fatal编程技术网

Python 当行以字符串开头时,如何按整数对文本文件进行排序?

Python 当行以字符串开头时,如何按整数对文本文件进行排序?,python,python-2.7,Python,Python 2.7,当行以字符串开头时,如何按整数对文本文件进行排序 我想列这个清单 Adams 3.7 Alexander 36.1 Bond 6.5 Boone 2.6 Brown 19.1 Bureau 0.8 Calhoun 0.3 Carroll 1.1 Cass 4.4 Champaign 12.8 像 然后我还计划删除所有值大于1的行,然后删除所有整数 # open file with open("my_file.txt") as infile:

当行以字符串开头时,如何按整数对文本文件进行排序

我想列这个清单

Adams   3.7
Alexander   36.1
Bond    6.5
Boone   2.6
Brown   19.1
Bureau  0.8
Calhoun     0.3
Carroll     1.1
Cass    4.4
Champaign   12.8

然后我还计划删除所有值大于1的行,然后删除所有整数

# open file
with open("my_file.txt") as infile:
    # make a list with each line
    # split lines by whitespace, so that the name is element 0 and value is element 1
    file_lines = [line.split() for line in infile]

# sort lines, with the sort key being the value (element 1)
# we need to cast it to a float first, so that numeric comparison behaves correctly
sorted_lines = sorted(file_lines, key=lambda x:float(x[1]))
print(sorted_lines)
# [['Calhoun', '0.3'], ['Bureau', '0.8'], ['Carroll', '1.1'], ['Boone', '2.6'], ['Adams', '3.7'], ['Cass', '4.4'], ['Bond', '6.5'], ['Champaign', '12.8'], ['Brown', '19.1'], ['Alexander', '36.1']]

# export back to file in the same format
outfile_lines = ["\t".join(line) for line in sorted_lines]
with open("my_file_sorted.txt", "w") as outfile:
    outfile.writelines(outfile_lines)
您以后可以进一步过滤
已排序的\u行。例如:

filtered_lines = [line for line in file_lines
                     if float(line[1]) <= 1  # "remove all lines with value greater than 1"
                     and float(line[1]) != float(int(line[1]))  # "remove all of the integers"
                 ]
filtered_line=[文件行中的行对行\u行
如果浮动(第[1]行)
您可以在以后进一步筛选
已排序的\u行。例如:

filtered_lines = [line for line in file_lines
                     if float(line[1]) <= 1  # "remove all lines with value greater than 1"
                     and float(line[1]) != float(int(line[1]))  # "remove all of the integers"
                 ]
filtered_line=[文件行中的行对行\u行
如果浮动(行[1])这将起作用

fp = open('file')

pairs = [line.split() for line in fp]

fp.close()

# pair = [['Adams', '3.7'], ['Alexander', '36.1'], ['Bond', '6.5'], ['Boone', '2.6'], ['Brown', '19.1'],
#         ['Bureau', '0.8'], ['Calhoun', '0.3'], ['Carroll', '1.1'], ['Cass', '4.4'], ['Champaign', '12.8']]

pairs.sort(key=lambda item: float(item[1]))
print(pairs)
# pairs = [['Calhoun', '0.3'], ['Bureau', '0.8'], ['Carroll', '1.1'], ['Boone', '2.6'], ['Adams', '3.7'], ['Cass', '4.4'],
#          ['Bond', '6.5'], ['Champaign', '12.8'], ['Brown', '19.1'], ['Alexander', '36.1']]

fp = open('result', 'w')

for pair in pairs:
    string = str(pair[0]) + '   ' + str(pair[1]) + '\n'
    fp.write(string)

fp.close()
这会有用的

fp = open('file')

pairs = [line.split() for line in fp]

fp.close()

# pair = [['Adams', '3.7'], ['Alexander', '36.1'], ['Bond', '6.5'], ['Boone', '2.6'], ['Brown', '19.1'],
#         ['Bureau', '0.8'], ['Calhoun', '0.3'], ['Carroll', '1.1'], ['Cass', '4.4'], ['Champaign', '12.8']]

pairs.sort(key=lambda item: float(item[1]))
print(pairs)
# pairs = [['Calhoun', '0.3'], ['Bureau', '0.8'], ['Carroll', '1.1'], ['Boone', '2.6'], ['Adams', '3.7'], ['Cass', '4.4'],
#          ['Bond', '6.5'], ['Champaign', '12.8'], ['Brown', '19.1'], ['Alexander', '36.1']]

fp = open('result', 'w')

for pair in pairs:
    string = str(pair[0]) + '   ' + str(pair[1]) + '\n'
    fp.write(string)

fp.close()

为了得到更好的答案,您应该显示您尝试了什么。您可以创建对列表(字符串,整数),然后按整数值对列表排序为了得到更好的答案,您应该显示您尝试了什么。您可以创建对列表(字符串,整数),然后按整数值对该列表进行排序I'm Get ValueError:无法将字符串转换为float at sorted_lines=sorted(file_lines,key=lambda x:float(x[1]),它适用于您在问题中给出的数据。如果您遇到“name”是多个单词的情况(例如“Mary Anne”),这些标记将被解析为单独的标记,因此尝试将
“Anne”
转换为浮点将产生该错误。您可以通过将
[1]
更改为
[-1]
(如果数字始终是行中的最后一项)来修复此问题,但它仍可能输出错误的文件。更好的方法是更改您读取信息的方式,使十进制数始终位于其自身标记中,并且始终位于列表的同一索引中(您可以在键中使用该索引)。我正在尝试筛选的_行代码,得到
ValueError:invalid literal for int()以10:0.9为基数,在
和float(第[-1行])!=float(int(第[-1行])
的情况下,我得到了一个值错误:无法将字符串转换为float,在sorted\u行=sorted(file\u行,key=lambda x:float(x[1])的情况下,它可以处理您在问题中给出的数据。如果您有“name”是多个单词的情况(例如,“Mary Anne”),这些标记将被解析为单独的标记,因此尝试将
“Anne”
转换为浮点将产生该错误。您可以通过将
[1]
更改为
[-1]
(如果数字始终是行中的最后一项)来修复此问题,但它仍可能输出错误的文件。更好的方法是更改您读取信息的方式,使十进制数始终位于其自身标记中,并且始终位于列表的同一索引中(您可以在键中使用该索引)。我正在尝试筛选的_行代码,得到
ValueError:invalid literal for int()以10为基数:0.9
at
和float(第[-1行])!=float(int(第[-1行])
非常好。您现在能告诉我如何从结果中完全删除数字吗?您只需要名称列表吗?如果是,那么用
string=str(配对[0])替换
string
行+'\n'
非常好。您现在能告诉我如何从结果中完全删除数字吗?您只需要名称列表吗?如果是,请将
string
line替换为
string=str(pair[0])+'\n'