基于一列python对两列列表进行排序

基于一列python对两列列表进行排序,python,list,sorting,Python,List,Sorting,如果我有一个文件file.txt或file.dat,有两列,比如x和y: x y 1467153 12309 1466231 21300 . . . . 1478821 10230 我想用x作为键值按升序对每个x,y进行排序。如何在python中实现这一点 Python具有内置函数,您可以使用该函数对列表进行排序 data = """1467153 12309 1466231 21300 1478821

如果我有一个文件file.txt或file.dat,有两列,比如x和y:

   x      y  
1467153  12309  
1466231  21300  
  .        .  
  .        .  
1478821  10230 

我想用x作为键值按升序对每个x,y进行排序。如何在python中实现这一点

Python具有内置函数,您可以使用该函数对列表进行排序

data = """1467153  12309  
1466231  21300  
1478821  10230
"""
l = sorted([list(map(int, line.split())) # convert each pair to integers
            for line                     # iterate over lines in input
            in data.split("\n")          # split on linebreaks
            if line],                    # ignore empty lines
    key=lambda x: x[0])                  # sort by firt element of pair
print(l)
输出:

[[1466231, 21300], [1467153, 12309], [1478821, 10230]]
[(1466231, 21300), (1467153, 12309), (1478821, 10230)]
编辑:如果您的输入是两个整数列表,请执行以下操作:

xs = [1467153, 1466231, 1478821]
ys = [12309, 21300, 10230]
l = sorted(zip(xs, ys), key=lambda x: x[0])
print(l)
输出:

[[1466231, 21300], [1467153, 12309], [1478821, 10230]]
[(1466231, 21300), (1467153, 12309), (1478821, 10230)]

到目前为止,您尝试了什么?到目前为止,我在python中得到了两个列表中的两列。如果您有两个列表,那么您可以尝试这个..,python非常漂亮。