我正在嘗試做一個簡單的數組& 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