gpt4 book ai didi

python - 从列表序列中提取数据以放入变量中

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:22:49 24 4
gpt4 key购买 nike

我正在尝试找到一种方法来将所有玩家的所有数字存储在 allPlayers 中

有人建议我使用有效的 for 循环,但是当您全局使用它时,它只显示最后一个数字,而不是所有数字。最好我希望它全部在一行中,将它放入一个变量中,但最简单的是什么。

player1 = ['David.G', '1204', '4th Catogory']
player2 = ['John.D', '1000', 'Unranked']
player3 = ['Barry.M', '1932', '1st Catogory']
player4 = ['Steven.H', '1844', '1st Catogory']
allPlayers = [player1, player2, player3, player4]

for player in allPlayers:
fideRankings = player[1]
global fideRankings

print("")
print(fideRankings)

结果是:120410001932年1844年

1844

我一直在寻找它来在 for 循环之外打印相同的四个数字。

最佳答案

player1 = ['David.G','1204','4th Catogory']
player2 = ['John.D','1000','Unranked']
player3 = ['Barry.M','1932','1st Catogory']
player4 = ['Steven.H','1844','1st Catogory']
allPlayers = [player1,player2,player3,player4]

for i in allPlayers:
print(i[1],end=" ")

如果你只想在 for 循环之外打印它:

player1 = ['David.G','1204','4th Catogory']
player2 = ['John.D','1000','Unranked']
player3 = ['Barry.M','1932','1st Catogory']
player4 = ['Steven.H','1844','1st Catogory']
allPlayers = [player1,player2,player3,player4]

string = ""
for i in allPlayers:
string = string + " " + str(i[1])

print(string)

如果您将来需要使用这些数字,您始终可以使用 string.split("") 来获取列表中的这些数字。

关于python - 从列表序列中提取数据以放入变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com