Python 如何从文本文件中打印排序的2d列表中的3种不同类型的值?
我有一个文本文件,上面写着一个人的名字,num.pressups和num.pullup。这是文本文件。 我需要对学生分别能做的俯卧撑和俯卧撑的数量进行分类。例如,杰克可以做最多的俯卧撑和拉升动作,因此他在这两个类别中都会名列前茅 为了解决这个问题,我想我应该制作一个2D列表,但是我不确定如何排序以按排序方式附加值。这是迄今为止我编写的代码Python 如何从文本文件中打印排序的2d列表中的3种不同类型的值?,python,python-3.x,Python,Python 3.x,我有一个文本文件,上面写着一个人的名字,num.pressups和num.pullup。这是文本文件。 我需要对学生分别能做的俯卧撑和俯卧撑的数量进行分类。例如,杰克可以做最多的俯卧撑和拉升动作,因此他在这两个类别中都会名列前茅 为了解决这个问题,我想我应该制作一个2D列表,但是我不确定如何排序以按排序方式附加值。这是迄今为止我编写的代码 opener = open("gymclub.txt", "r") reader = opener.readline
opener = open("gymclub.txt", "r")
reader = opener.readline()
listObjects = []
listNames = []
listPressups = []
listPullups = []
while reader!="":
splitting=reader.split(",")
name = splitting[0]
press_ups = splitting[1]
pull_ups = splitting[2]
reader = opener.readline()
print(name)
print(press_ups)
print(pull_ups)
listNames.append(name)
listPressups.append(press_ups)
listPullups.append(pull_ups)
listObjects.append(name)
listObjects.append(press_ups)
listObjects.append(pull_ups)
print(listObjects)
splitting2 = str(listObjects).split("\n")
# print(splitting[1])
# print(splitting[2])
opener.close()
我只想知道一种方法,可以帮助我解决这个问题。用熊猫做这个比较容易
import pandas as pd
df = pd.read_csv("your.csv", columns=["name","pressups", "pullups"])
df.sort(["pressups", "pullups"], ascending=[True, False])
试试这个
import csv
with open('file.txt', "r") as f:
sorted(csv.DictReader(f),
# replace with actual column names
key=lambda x: (int(x["pressups"]), int(x["pullups"])),
reverse=True)
你已经结束了你的问题,我无法回答 如果您想进行登录验证,那么您可以使用
csv
比其他文件更容易地进行登录验证
您必须创建users.csv
文件
username,password
player123,11223344
player456,55667788
然后您必须编写一个返回bool
值的函数
import pandas as pd
def authorize(username, password):
df = pd.read_csv('users.csv')
for user_id in range(len(df.values)):
u_name = str(df.values[user_id][0])
p_word = str(df.values[user_id][1])
if u_name == username and p_word == password:
return True
return False
if __name__ == "__main__":
username = input("Enter your username: ")
password = input("Enter your password: ")
if authorize(username, password):
print("user authorized")
else:
print("user is not defined")
我认为这与登录验证无关。你打算把你的回复发布到另一个线程上吗?我知道。这是迷你游戏的基本验证示例,现在还不清楚。你的回答如何与OP中的问题保持一致。问题是按两列对数据进行排序。您的响应是检查特定用户。无法连接提问和回答之间的点。