Skip to main content
 首页 » 编程设计

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

2025年05月04日16lvdongjie

这是我的代码:

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等