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条经验 获得超7个赞
您可以使用ast.literal_eval这对您很有用:
import ast
list2 = ast.literal_eval(list2)
result2 = [''.join(x) for x in List2]
添加回答
举报