簡體   English   中英

將字符串數組轉換為 python 中的數組數組

[英]Convert string array to array of array in python

我有這樣的輸入

logs = ['88 99 200','88 99 300','99 32 100','12 12 15']

如何像這樣將它轉換為 Python 中的數組數組?

logs = [[88 99 200],[88 99 300],[99 32 100],[12 12 15]]

str.splitmap一起使用:

list(map(str.split, logs))


Out[36]:
[['88', '99', '200'],
 ['88', '99', '300'],
 ['99', '32', '100'],
 ['12', '12', '15']]

如果要將每個項目轉換為int

list(map(lambda x: [int(i) for i in x], map(str.split, logs)))


Out[37]: [[88, 99, 200], [88, 99, 300], [99, 32, 100], [12, 12, 15]]

這是轉換的方法。

logs = ['88 99 200','88 99 300','99 32 100','12 12 15']
logs = [[i] for i in logs]

另一種方法是

logs2 = []
for i in logs:
    logs2.append([i])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM