在某个索引处按元素排序列表/元组

时间:2016-05-01 17:19:40

标签: python python-2.7 sorting

我如何开始对此列表进行排序,以便按顺序显示按顺序显示的国家/地区名称(由文本文件中的数据读取)?我希望它只显示国家的名称,而不是汇率。另外,我是一个新手,所以更简单的答案越有帮助,我就越能理解/学习。我不确定我是否将其称为列表或字典。

America,Dollar,1
Argentina,Peso,8.257
Australia,Dollar,1.432
Austria,Euro,0.82

我对如何开始有一个大致的想法,但我不知道该怎么做。这是我到目前为止所做的:

fhand = open('Exchange.txt')
for line in fhand:
    line = line.split(',')
print line[0]

输出应该是这样的:

Austria
Australia
America
Argentina

4 个答案:

答案 0 :(得分:3)

这样的事情应该有效:

all_data = []

fhand = open('/tmp/data')

for line in fhand:
    data_parts = line.strip().split(',')
    # Convert the data to a tuple of country and the exchange rate as
    # a number (rather than a string).
    data_item = (data_parts[0], float(data_parts[2]))
    all_data.append(data_item)
fhand.close()

# Sort that data by the 2nd part of the tuple: the exchange rate
all_data.sort(key = lambda x: x[1])
# print out the sorted list but only print the first part: the country
for di in all_data:
    print(di[0])

答案 1 :(得分:1)

对费率列表进行排序。

data = """America,Dollar,1
Argentina,Peso,8.257
Australia,Dollar,1.432
Austria,Euro,0.82"""

rates = [line.split(",") for line in data.split("\n")]

print rates
sorted_rates = sorted(rates, key=lambda x: float(x[2]))
print sorted_rates

输出:

[['America', 'Dollar', '1'], ['Argentina', 'Peso', '8.257'], ['Australia', 'Dollar', '1.432'], ['Austria', 'Euro', '0.82']]
[['Austria', 'Euro', '0.82'], ['America', 'Dollar', '1'], ['Australia', 'Dollar', '1.432'], ['Argentina', 'Peso', '8.257']]

答案 2 :(得分:1)

这对你来说应该是最容易理解的。

# Read the file content, as a single string into "file_content"
file_content = open('Exchange.txt').read()
print(file_content)

# Prints:
# America,Dollar,1
# Argentina,Peso,8.257
# Australia,Dollar,1.432
# Austria,Euro,0.82


# Split the file contents to lines, which is a list of strings,
# each element of list being a single line of the file
lines = file_content.splitlines()
print(lines)

# Prints:
# ['America,Dollar,1', 'Argentina,Peso,8.257', 'Australia,Dollar,1.432', 'Austria,Euro,0.82']


# Split each line into 3 parts, separated by comma
split_lines = [line.split(',') for line in lines]
print(split_lines)

# Prints:
# [['America', 'Dollar', '1'], ['Argentina', 'Peso', '8.257'], ['Australia', 'Dollar', '1.432'], ['Austria', 'Euro', '0.82']]


# Sort the complete list using the third element of each sublist as the key to sort
lines_sorted = sorted(split_lines, key=lambda x: x[2])

for line in lines_sorted:
    print(line[0])

最终循环打印:

Austria
America
Australia
Argentina

注意:如果按照汇率排序,问题中给出的预期输出有点不对。亲自检查一下。

有用的链接,特别是对于OP ;-)

答案 3 :(得分:1)

这可能是低效的,我也不会在阅读之后关闭文件......但是,嘿,一行! :d

print("\n".join((z[1] for z in sorted(((float(x[2]), x[0]) for x in (line.split(",") for line in open('Exchange.txt')))))))

更合理的解决方案......

with open("Exchange.txt", "r") as f:
    for li in sorted((float(x[2]), x[0]) for x in (line.split(",") for line in f)):
        print li[1]

看,马',没有循环!

with open("Exchange.txt", "r") as f:
    data = map(lambda x: x.split(","), f.readlines())
    data = map(lambda x: (float(x[2]), x[0]), data)
    print "\n".join(map(lambda x: x[1], sorted(data)))

这个出去@zondo;)

with open("Exchange.txt", "r") as f:
    data = []
    for line in f:
        data.append(line.split(","))
    data = sorted(data, key=lambda x: float(x[2]))
    for line in data:
        print line[0]