简体   繁体   English

如何将数组中的嵌套字符串转换为分离的单词?

[英]How to transform nested strings in array to separated words?

I'm trying to do a simple array & string transformation with Python yet I'm stuck. 我正在尝试使用Python做一个简单的数组和字符串转换,但是我被困了。 I have this array : 我有这个数组:

data = ['one, two, three',  'apple, pineapple',  'frog, rabbit, dog, cat, horse'] 

And I would like to arrive to this result : 我想得出这个结果:

new_data = ['one', 'two', 'three', 'apple', 'pineapple', 'frog', 'rabbit', 'dog', 'cat', 'horse']

This is what I'm doing but whenever I use 这就是我在做的,但是每当我使用时

data_to_string = ''.join(data) 
new_data = re.findall(r"[\w']+", data_to_string)

it gives me this : 它给了我这个:

['one', 'two', 'threeapple', 'pineapplefrog', 'rabbit', 'dog', 'cat', 'horse']

As you can see "threeapple" and "pineapplefrog" aren't separated, how can I avoid this issue ? 如您所见,“ threeapple”和“ pineapplefrog”没有分开,如何避免这个问题?

How about some simple list comprehension and string methods? 一些简单的列表理解和字符串方法怎么样? re is overkill for this. 对于这个来说, re实在是太过分了。

>>> data = ['one, two, three',  'apple, pineapple',  'frog, rabbit, dog, cat, horse']
>>> [word.strip() for string in data for word in string.split(',')]
['one', 'two', 'three', 'apple', 'pineapple', 'frog', 'rabbit', 'dog', 'cat', 'horse']

Look into list comprehensions, they're great. 调查列表理解,它们很棒。

Here's your answer: 这是您的答案:

[word for string in data for word in string.split(", ")]

use join and split 使用连接和拆分

','.join(data).split(',')

results in 结果是

['one',
 ' two',
 ' three',
 'apple',
 ' pineapple',
 'frog',
 ' rabbit',
 ' dog',
 ' cat',
 ' horse']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python将字符串数组转换为矩阵 - How to transform array of strings into matrix with python 如何将字符串空格分隔的键,唯一字的值对转换为字典 - How to transform string of space-separated key,value pairs of unique words into a dict 计算大熊猫中每组以逗号分隔的字符串中的所有单词 - Count all words in comma separated strings per group in pandas 用嵌套数组值转换字典 - transform dict with nested array values 如果我有一个字符串列表,这些字符串是用逗号分隔的单词,如何将其转换为带有单词配对和频率的DataFrame? - If I have a list of character strings which are words separated by commas, how do I convert that into a DataFrame with word pairings and frequencies? 将以字符串分隔的数据导入numpy数组 - Import data separated by strings to numpy array 如何在以“;”分隔的文件行中拆分单词 - How to split words in line of a file separated by “;” 单词用“|”(PSV)分隔时如何读取文件? - How to read file when the words are separated by “|” (PSV)? 如何将字符串列表(每个条目是由空格分隔的三个数字)转换为数字数组? - How can I convert a list of strings (each entry is three numbers separated by spaces) to an array of numbers? 如何将几个单词列表转换为pandas数据框? - How to transform several lists of words to a pandas dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM