我有一個清單['101,122,133,145,150,222,200']
,我想列出如下
['101','122','133','145','150','222','200']或[101,122,133,145,150,222,200]
uj5u.com熱心網友回復:
您可以使用.split()所需的輸出。
rlist = ['101,122,133,145,150,222,200']
reqlist = []
for _ in rlist:
nlist = _.split(',')
reqlist.append(nlist)
print(reqlist)
這列印
[['101', '122', '133', '145', '150', '222', '200']]
uj5u.com熱心網友回復:
添加此代碼
array = ['101,122,133,145,150,222,200'][0].split(',')
print(array)
uj5u.com熱心網友回復:
l = ['101,122,133,145,150,222,200']
new_list =l[0].split(',')
>>> ['101', '122', '133', '145', '150', '222', '200']
uj5u.com熱心網友回復:
要獲取字串格式的元素,您可以使用str.split來定義分隔符并根據其周圍的子字串生成串列:
list = ['101,122,133,145,150,222,200']
list[0].split(",")
要獲取 int 格式的數字,您可以強制轉換它們。這可以通過迭代前一個串列、創建一個新的 int 物件并將其添加到新串列中來實作。
int_list = []
for x in list[0].split(","):
number = int(x)
int_list.append(number)
或者更簡潔地使用串列理解:
list = ['101,122,133,145,150,222,200']
int_list = [int(x) for x in list[0].split(",")]
uj5u.com熱心網友回復:
對于整數串列,您可以這樣做:
a = ['101,122,133,145,150,222,200']
b = list(map(int, a[0].split(',')))
print(b)
輸出:
[101, 122, 133, 145, 150, 222, 200]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/493443.html
上一篇:字典回圈操作
