2 回答

TA貢獻1846條經驗 獲得超7個贊
你list2是一個列表列表,string而你list1是一個字符串列表。因此,您需要展平您list2的以獲得list1如下結果。
import ast
# Convert to list
list2 = ast.literal_eval(list2)
# Flatten nested list/list of list into list
flat_list2 = [y for x in list2 for y in x]
# Then you can use this
result = [''.join(x) for x in List2]
或者,您可以將它們組合起來:
import ast
list2 = ast.literal_eval(list2)
result [''.join(y) for x in list2 for y in x]
當然,您需要確保您的嵌套列表字符串必須遵循正確的 Python 語法。下面是運行的代碼IPython
In [1]: list2 = """[[\'7am run ??\\u200d♂? done ? @kristianevofit @ottowallin @trboxing @btsport @ringtv @frank_warren_official @mtkglobal @marbella.co.uk\'],
...: [\'I have succummed to the #bottlecapchallenge ????? What do you think? #bluesteel #scrubs\']]"""
In [2]: import ast
In [3]: list2 = ast.literal_eval(list2)
In [4]: result = [''.join(y) for x in list2 for y in x]
In [5]: result
Out[5]:
['7am run ??\u200d♂? done ? @kristianevofit @ottowallin @trboxing @btsport @ringtv @frank_warren_official @mtkglobal @marbella.co.uk',
'I have succummed to the #bottlecapchallenge ????? What do you think? #bluesteel #scrubs']

TA貢獻1794條經驗 獲得超8個贊
您可以使用ast.literal_eval這對您很有用:
import ast
list2 = ast.literal_eval(list2)
result2 = [''.join(x) for x in List2]
添加回答
舉報