gpt4 book ai didi

python - 如何使列表中的变量可识别为数字?

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:36 25 4
gpt4 key购买 nike

这是我的代码:

list1 =  "SP   " , "Total Sales   " , "Total Commission"
print(" ".join(list1))
listA = ["103 " , "500"]
listB = "104 " , "25000"
listC = "201 " , "51000"
listD = "319 " , "100000"
listE = "367 " , "12599"
listF = "388 " , "12600"
listG = "435 " , "292000"
print(" ".join(listA))
print(" ".join(listB))
print(" ".join(listC))
print(" ".join(listD))
print(" ".join(listE))
print(" ".join(listF))
print(" ".join(listG))
if 0 < (listA[1]) < 50999:
print ("yes")

我收到此错误:TypeError: unorderable types: int() < str()

我需要能够检查 (listA[1]) 中的数字查看它是否大于 0 且小于 50999,但我不知道如何让它工作。

最佳答案

如果你有

listA = ["103  " , "500"]

你可以简单地使用 list comprehension将其更改为整数列表:

listA = [int(item) for item in listA]

然后可以将其与其他整数进行比较。

但是,您不应该使用七个(或更多)单独的列表。最好做类似的事情:

lists = [["103  " , "500"],
["104 " , "25000"],
["201 " , "51000"],
["319 " , "100000"],
["367 " , "12599"],
["388 " , "12600"],
["435 " , "292000"],
]

然后访问子列表为 lists[0]而不是 listA , lists[1]而不是 listB

关于python - 如何使列表中的变量可识别为数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33706119/

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