在Python的我怎麼轉換:如何轉換的字符串列表,列出清單
list a = [ 'i am going to America', 'i will come by tomorrow evening' ]
into
list b = [ [i am going to America], [i will come by tomorrow evening] ]
在Python的我怎麼轉換:如何轉換的字符串列表,列出清單
list a = [ 'i am going to America', 'i will come by tomorrow evening' ]
into
list b = [ [i am going to America], [i will come by tomorrow evening] ]
使用列表理解。
>>> a = [ 'i am going to America', 'i will come by tomorrow evening' ]
>>> [[x] for x in a]
[['i am going to America'], ['i will come by tomorrow evening']]
您可以使用列表綜合併產生另一個列表
list_a = [ 'i am going to America', 'i will come by tomorrow evening' ]
print(list_a) # ['i am going to America', 'i will come by tomorrow evening']
list_b = [[x] for x in list_a]
print(list_b) # [['i am going to America'], ['i will come by tomorrow evening']]
需要聲明的每個條目的列表,然後將其追加到main_list
list = ['i am going']
main_list.append(list)
試試這個:
list = ['iam g', 'here']
main_list = []
main_list.append(list)
print main_list
output: [['iam g', 'here']]
main_list.append(list)
print main_list
output: [['iam g', 'here'], ['iam g', 'here']]