[英]How to split a compound word split by hyphen into two individual words
I have the following list我有以下列表
list1= ['Dodd-Frank', 'insurance', 'regulation']
I used the following to remove the hyphen我使用以下内容删除连字符
new1 =[j.replace('-', ' ') for j in list1]
The result I got我得到的结果
new1= ['Dodd Frank', 'insurance', 'regulation']
The result that Ideally want is Ideally 想要的结果是
new1= ['Dodd', 'Frank', 'insurance', 'regulation']
How can I accomplish this in the most pythonic (efficient way)我怎样才能以最pythonic(有效的方式)完成这个
list1 = ['Dodd-Frank', 'insurance', 'regulation']
new1 = '-'.join(list1).split('-')
print(new1)
Prints:印刷:
['Dodd', 'Frank', 'insurance', 'regulation']
Beautiful is better than ugly.美丽总比丑陋好。
But it's usually rewarding to move from ugly code to beautiful code.但从丑陋的代码转向优美的代码通常是有益的。 So, we'll first attack this problem using loops , and then we'll massage the loop-y solution into a one-liner in a number of steps.
因此,我们将首先使用loops解决这个问题,然后我们将通过多个步骤将loop-y解决方案按摩到一个单行中。
res = []
for item in list1:
sublist = item.split("-")
for subitem in sublist:
res.append(subitem)
We can do better by splitting sublist
in the header of the inner for
loop, in order to avoid the assignment just before the loop.我们可以通过在内部
for
循环的头部拆分sublist
来做得更好,以避免在循环之前赋值。
res = []
for item in list1:
for subitem in item.split("-"):
res.append(subitem)
Now that we have our loop in iterate-then-append form, we can conveniently massage it into a one-liner.既然我们的循环是先迭代后追加的形式,我们就可以方便地将它按摩成一行。
res = [subitem for item in list1 for subitem in item.split("-")]
list2 = []
[list2.extend(i.split("-")) for i in list1]
list2:清单 2:
['Dodd', 'Frank', 'insurance', 'regulation']
This can be done with a single list comprehension, without needing you to construct any intermediate data structures:这可以通过单个列表理解来完成,不需要您构建任何中间数据结构:
my_list = ['Dodd-Frank', 'insurance', 'regulation']
def split_hyphens(lst):
return [i for word in lst for i in word.split("-")]
print(split_hyphens(my_list))
with output带输出
['Dodd', 'Frank', 'insurance', 'regulation']
Variation using yield from
使用
yield from
变化
list1= ['Dodd-Frank', 'insurance', 'regulation']
def hyphen_split(list1):
for sublst in list1:
yield from sublst.split("-")
print(list(hyphen_split(list1)))
Output输出
['Dodd', 'Frank', 'insurance', 'regulation']
[Program finished]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.