使用 for 循环对列表进行排序

时间:2021-03-07 08:54:12

标签: python list

我想对我的列表进行排序,并根据它们各自的类别单独分配它们。我仍然不熟悉列表索引及其工作原理。有人可以帮我解决这个问题吗?

输入

2

100 10

200 20

期望输出

[100, 200]
[10, 20]

代码:

if __name__ == '__main__':
    number_of_items = int(input())
    the_items = []

    for i in range(number_of_items):
        the_items.append(input().split(' '))

    for x in the_items:
        cost, disc = [the_items[0][0]], [the_items[0][1]]
        print(cost, disc)

2 个答案:

答案 0 :(得分:1)

Python 具有内置的 zip 函数,可以帮助您:

a = [100, 20]
b = [200, 10]

sorted_ls = [sorted(each) for each in zip(a, b)]
# -> [[100, 200], [10, 20]]

答案 1 :(得分:0)

所以基于你想要的,有点困惑。答案如下:

t = int(input())
_t1 = []
_t2 = []
for i in range(t):
    _a = list(map(int,input().split()))
    x = _a[0]
    y = _a[1]
    _t1.append(x)
    _t2.append(y)
print(sorted(_t1))
print(sorted(_t2))