为特定数字赋值通过文件

huangapple go评论68阅读模式
英文:

Atributting values to specific numbers trough a file

问题

我有一个问题,是否有办法将第一列的数字与第二列的数字关联起来?这样我就可以阅读第二列的数字,但它们与第一列的数字以某种方式相连,以便我可以像在sorted_resistances列表中那样对它们进行排序,但在对它们进行排序之后,我将它们替换为分配给每个值的第一列中的值。
关于代码的信息是从文件中打开列表,这就是为什么它以这种方式编程的原因。

1 30000
2 30511
3 30052
4 30033
5 30077
6 30055
7 30086
8 30044
9 30088
10 30019
11 30310
12 30121
13 30132

with open("file.txt") as file_in:
    list_of_resistances = []
    for line in file_in:
        list_of_resistances.append(int(line.split()[1]))
    sorted_resistances = sorted(list_of_resistances)
英文:

My question is if there's any way to attribute the numbers in the first column to the ones in the second column. So that I can read the numbers in the second column but have them connected to the ones in the first column in some way, so that I can sort them as I do in the sorted_resistances list but after sorting them I replace them with the values in the first column that we're assigned to each of the values.
For information in the code it's opening up from a file the list that's why it's programed like that

1 30000
2 30511
3 30052
4 30033
5 30077
6 30055
7 30086
8 30044
9 30088
10 30019
11 30310
12 30121
13 30132


with open("file.txt") as file_in:

    list_of_resistances = []
    for line in file_in:
        list_of_resistances.append(int(line.split()[1]))
        sorted_resistances = sorted(list_of_resistances)

答案1

得分: 0

以下是翻译好的内容:

如果您希望保持两列中值之间的相关性,您可以将每行的所有值保存在一个元组(或列表)中,然后使用特定的片段通过将 lambda 函数传递给 sorted() 函数的 key 参数对元组列表进行排序,告诉它使用每个元组的第二个部分作为排序值。

在这个示例中,我使用了 pprint.pprint 来使列表的输出更容易阅读。

from pprint import pprint

with open("file.txt") as file_in:

    list_of_resistances = []
    for line in file_in:
        list_of_resistances.append(tuple(line.strip().split(' ')))
    
    print("未排序的值:")
    pprint(list_of_resistances)
    
    sorted_resistances = sorted(list_of_resistances, key=lambda x: x[1])
    print("\n已排序的值:")
    pprint(sorted_resistances)

    print("\n从第一列排序的键:")
    pprint([x[0] for x in sorted_resistances])

输出:

未排序的值:
[('1', '30000'),
 ('2', '30511'),
 ('3', '30052'),
 ('4', '30033'),
 ('5', '30077'),
 ('6', '30055'),
 ('7', '30086'),
 ('8', '30044'),
 ('9', '30088'),
 ('10', '30019'),
 ('11', '30310'),
 ('12', '30121'),
 ('13', '30132')]

已排序的值:
[('1', '30000'),
 ('10', '30019'),
 ('4', '30033'),
 ('8', '30044'),
 ('3', '30052'),
 ('6', '30055'),
 ('5', '30077'),
 ('7', '30086'),
 ('9', '30088'),
 ('12', '30121'),
 ('13', '30132'),
 ('11', '30310'),
 ('2', '30511')]

从第一列排序的键:
['1', '10', '4', '8', '3', '6', '5', '7', '9', '12', '13', '11', '2']
英文:

If you want to keep the correlation between the values in the two columns, you can keep all of the values from each line in a tuple (or list), and then sort the list of tuples using a specific piece by passing a lambda function to the key parameter of the sorted() function that tells it to use the second piece of each tuple as the sort value.

In this example, I used pprint.pprint to make the output the of the lists easier to read.

from pprint import pprint

with open("file.txt") as file_in:

    list_of_resistances = []
    for line in file_in:
        list_of_resistances.append(tuple(line.strip().split(' ')))
    
    print("Unsorted values:")
    pprint(list_of_resistances)
    
    sorted_resistances = sorted(list_of_resistances, key=lambda x: x[1])
    print("\nSorted values:")
    pprint(sorted_resistances)

    print("\nSorted keys from column 1:")
    pprint([x[0] for x in sorted_resistances])

Output:

Unsorted values:
[('1', '30000'),
 ('2', '30511'),
 ('3', '30052'),
 ('4', '30033'),
 ('5', '30077'),
 ('6', '30055'),
 ('7', '30086'),
 ('8', '30044'),
 ('9', '30088'),
 ('10', '30019'),
 ('11', '30310'),
 ('12', '30121'),
 ('13', '30132')]

Sorted values:
[('1', '30000'),
 ('10', '30019'),
 ('4', '30033'),
 ('8', '30044'),
 ('3', '30052'),
 ('6', '30055'),
 ('5', '30077'),
 ('7', '30086'),
 ('9', '30088'),
 ('12', '30121'),
 ('13', '30132'),
 ('11', '30310'),
 ('2', '30511')]

Sorted keys from column 1:
['1', '10', '4', '8', '3', '6', '5', '7', '9', '12', '13', '11', '2']

huangapple
  • 本文由 发表于 2023年2月10日 02:59:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定