我正在尝试做一个简单的数组& Python的字符串转换,但我卡住了。我有这样的数组:如何将数组中的嵌套字符串转换为分隔的单词?
data = ['one, two, three', 'apple, pineapple', 'frog, rabbit, dog, cat, horse']
而且我想到达这个结果:
new_data = ['one', 'two', 'three', 'apple', 'pineapple', 'frog', 'rabbit', 'dog', 'cat', 'horse']
这是我在做什么,但每当我用
data_to_string = ''.join(data)
new_data = re.findall(r"[\w']+", data_to_string)
它给了我这个:
['one', 'two', 'threeapple', 'pineapplefrog', 'rabbit', 'dog', 'cat', 'horse']
正如你所看到的“threeapple”和“pineapplefrog”没有分开,我该如何避免这个问题?
我会检查列表推导然后:) – Lindow