我在串列中有串列,對于每個子串列中的第三列,我想將其值從 String 更改為 Int,而不創建其他新串列(如果我宣告新串列,我會知道如何執行此操作list2)但要更改它立即在其中(如果可能)。
list1=[[1,"a","100"],[2,"b","200"],[3,"c","300"]]
[int(item[2]) for item in list1]
for t in list1:
print(type(t[2]))
<class 'str'>
<class 'str'>
<class 'str'>
這列印str。目標是有整數。另外我必須使用 python 2.7(不能升級到 python 3)
目標是列出所有其他列:
list1=[[1,"a",100],[2,"b",200],[3,"c",300]]
uj5u.com熱心網友回復:
list1=[[1,"a","100"],[2,"b","200"],[3,"c","300"]]
for x in list1:
x[2] = int(x[2])
list1 # outputs [[1, 'a', 100], [2, 'b', 200], [3, 'c', 300]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/485369.html
標籤:Python python-2.7
