如何在 python 中按字母顺序打印两列
How to print two columns alphabetically in python
我有一个列表,需要根据两列按字母顺序排序。我当前的代码确实是按字母顺序对它进行排序,但上次它也没有对人员进行排序。在我下面的代码中。我使用 itemgetter(3) 来获取此人的名字。但是我该怎么做才能得到 2 项 getter,例如项 getter(3,4)。我希望我的问题是有道理的。谢谢。
注意:是否可以将名字和姓氏连接成一个字符串?然后使用该字符串作为项目 getter?
我的代码。
def sortAlpha():
newFile = open("ScriptTesting3.txt","w")
def csv_itemgetter(index, delimiter=' ', default=''):
def composite(row):
try:
return row.split(delimiter)[index]
except IndexError:
return default
return composite
with open("ScriptTesting2.txt", "r") as file:
for eachline in sorted(file, key=csv_itemgetter(3)):
print(eachline.strip("\n") , file = newFile)
ScriptTesting2.txt
2312 filand 4 Joe Alex
4541 portlant 4 Alex Gray
5551 highlands 4 Alex Martin
我的输出
5551 highlands 4 Alex Martin
4541 portlant 4 Alex Gray
2312 filand 4 Joe Alex
输出应该是
5551 highlands 4 Alex Gray
4541 portlant 4 Alex Martin
2312 filand 4 Joe Alex
你可以试试:
from operator import itemgetter
...
with open("ScriptTesting2.txt", "r") as file:
lines = file.readlines()
linesSplit = [l.split() for l in lines]
linesSplitSorted = sorted(linesSplit, key=itemgetter(3, 4))
for l in linesSplitSorted: print(' '.join(l))
另请参阅:sorting how-to
(其实我觉得这个确实是对的:
利用内置列表的比较。)
然后你可以尝试:
def itemGetterRest(idx):
def helper(seq):
return seq[idx:]
return helper
with open("Input.txt", "r") as file:
lines = file.readlines()
linesSplit = [l.split() for l in lines]
linesSplitSorted = sorted(linesSplit, key=itemGetterRest(3))
for l in linesSplitSorted: print(' '.join(l))
如果您将数据放在 excel 而不是文本中,然后通过 Panda 库读取 excel,那么您可以很容易地按多列排序。
假设excel中的数据是这样的:
然后你可以读取熊猫数据框中的数据并排序:
import pandas as pd
data = pd.read_excel('ScriptTesting3.xlsx')
data = data.sort_values(by=['C','D'], ascending=True)
print(data)
我选择了按C列和D列排序,你可以选择任何你想要的列。
我有一个列表,需要根据两列按字母顺序排序。我当前的代码确实是按字母顺序对它进行排序,但上次它也没有对人员进行排序。在我下面的代码中。我使用 itemgetter(3) 来获取此人的名字。但是我该怎么做才能得到 2 项 getter,例如项 getter(3,4)。我希望我的问题是有道理的。谢谢。
注意:是否可以将名字和姓氏连接成一个字符串?然后使用该字符串作为项目 getter?
我的代码。
def sortAlpha():
newFile = open("ScriptTesting3.txt","w")
def csv_itemgetter(index, delimiter=' ', default=''):
def composite(row):
try:
return row.split(delimiter)[index]
except IndexError:
return default
return composite
with open("ScriptTesting2.txt", "r") as file:
for eachline in sorted(file, key=csv_itemgetter(3)):
print(eachline.strip("\n") , file = newFile)
ScriptTesting2.txt
2312 filand 4 Joe Alex
4541 portlant 4 Alex Gray
5551 highlands 4 Alex Martin
我的输出
5551 highlands 4 Alex Martin
4541 portlant 4 Alex Gray
2312 filand 4 Joe Alex
输出应该是
5551 highlands 4 Alex Gray
4541 portlant 4 Alex Martin
2312 filand 4 Joe Alex
你可以试试:
from operator import itemgetter
...
with open("ScriptTesting2.txt", "r") as file:
lines = file.readlines()
linesSplit = [l.split() for l in lines]
linesSplitSorted = sorted(linesSplit, key=itemgetter(3, 4))
for l in linesSplitSorted: print(' '.join(l))
另请参阅:sorting how-to
(其实我觉得这个确实是对的:
利用内置列表的比较。)
然后你可以尝试:
def itemGetterRest(idx):
def helper(seq):
return seq[idx:]
return helper
with open("Input.txt", "r") as file:
lines = file.readlines()
linesSplit = [l.split() for l in lines]
linesSplitSorted = sorted(linesSplit, key=itemGetterRest(3))
for l in linesSplitSorted: print(' '.join(l))
如果您将数据放在 excel 而不是文本中,然后通过 Panda 库读取 excel,那么您可以很容易地按多列排序。
假设excel中的数据是这样的:
然后你可以读取熊猫数据框中的数据并排序:
import pandas as pd
data = pd.read_excel('ScriptTesting3.xlsx')
data = data.sort_values(by=['C','D'], ascending=True)
print(data)
我选择了按C列和D列排序,你可以选择任何你想要的列。