I am trying to combine two list of strings into one, where the first list of strings are applied to all items of list 2. The list are always same length, however the sublist of list 2 can contain more than 1 list.
我试图将两个字符串列表合并为一个,其中第一个字符串列表应用于列表2的所有项目。列表总是相同的长度,但列表2的子列表可以包含多个列表。
I've tried combining using zip()
, like this:
我尝试使用zip()进行组合,如下所示:
[str(x)+'_'+str(y) for x,y in zip(list1,list2)]
But that does not work when the sublist does not match in length.
但是当子列表的长度不匹配时,这不起作用。
Here is an example of what i want to do:
这是我想要做的一个例子:
list1 = ['H1','H2','H3','H4']
list2 = ['item1',['item2','item3'],['item4','item5','item6'],['item7','item8']]
And the output should be:
输出应该是:
list = ['H1_item1',['H2_item2','H2_item3'],['H3_item4','H3_item5','H3_item6'],['H4_item7','H4_item8']]
2 个解决方案
#1
4
You can use the following approach:
您可以使用以下方法:
['%s_%s'%(x,y) if isinstance(y,str) else ['%s_%s'%(x,yi) for yi in y]
for x,y in zip(list1,list2)]
This generates:
这会产生:
>>> ['%s_%s'%(x,y) if isinstance(y,str) else ['%s_%s'%(x,yi) for yi in y]
... for x,y in zip(list1,list2)]
['H1_item1', ['H2_item2', 'H2_item3'], ['H3_item4', 'H3_item5', 'H3_item6'], ['H4_item7', 'H4_item8']]
The code works as follows: we use - like you've done yourself - zip(list1,list2)
to produce pairs of elements of the two lists. Now for each such pair, we evaluate the ternary operator as is written in the first line.
代码的工作原理如下:我们使用 - 就像你自己做的那样 - zip(list1,list2)来生成两个列表的元素对。现在对于每个这样的对,我们评估第三行中写入的三元运算符。
The ternary operator will first inspect whether isinstance(y,str)
holds (we check if y
is a string). If so, we simply add %s_%s%(x,y)
to the list. If not, we evaluate a list comprehension ['%s_%s'%(x,yi) for yi in y]
. Here we iterate over every element yi
of y
, and we then add %s_%s%(x,yi)
to the sublist.
三元运算符将首先检查isinstance(y,str)是否成立(我们检查y是否为字符串)。如果是这样,我们只需将%s_%s%(x,y)添加到列表中。如果没有,我们在y中评估列表理解['%s_%s'%(x,yi)为yi。这里我们迭代y的每个元素yi,然后我们将%s_%s%(x,yi)添加到子列表中。
#2
3
You can try the following approch, where you check whether the element in list2
is a list or not:
您可以尝试以下approch,在其中检查list2中的元素是否为列表:
[[str(x)+'_'+str(el) for el in y] if isinstance(y, list)
else str(x)+"_"+str(y) for x,y in zip(list1,list2)]
Which gives the expected output:
这给出了预期的输出:
['H1_item1',
['H2_item2', 'H2_item3'],
['H3_item4', 'H3_item5', 'H3_item6'],
['H4_item7', 'H4_item8']]
#1
4
You can use the following approach:
您可以使用以下方法:
['%s_%s'%(x,y) if isinstance(y,str) else ['%s_%s'%(x,yi) for yi in y]
for x,y in zip(list1,list2)]
This generates:
这会产生:
>>> ['%s_%s'%(x,y) if isinstance(y,str) else ['%s_%s'%(x,yi) for yi in y]
... for x,y in zip(list1,list2)]
['H1_item1', ['H2_item2', 'H2_item3'], ['H3_item4', 'H3_item5', 'H3_item6'], ['H4_item7', 'H4_item8']]
The code works as follows: we use - like you've done yourself - zip(list1,list2)
to produce pairs of elements of the two lists. Now for each such pair, we evaluate the ternary operator as is written in the first line.
代码的工作原理如下:我们使用 - 就像你自己做的那样 - zip(list1,list2)来生成两个列表的元素对。现在对于每个这样的对,我们评估第三行中写入的三元运算符。
The ternary operator will first inspect whether isinstance(y,str)
holds (we check if y
is a string). If so, we simply add %s_%s%(x,y)
to the list. If not, we evaluate a list comprehension ['%s_%s'%(x,yi) for yi in y]
. Here we iterate over every element yi
of y
, and we then add %s_%s%(x,yi)
to the sublist.
三元运算符将首先检查isinstance(y,str)是否成立(我们检查y是否为字符串)。如果是这样,我们只需将%s_%s%(x,y)添加到列表中。如果没有,我们在y中评估列表理解['%s_%s'%(x,yi)为yi。这里我们迭代y的每个元素yi,然后我们将%s_%s%(x,yi)添加到子列表中。
#2
3
You can try the following approch, where you check whether the element in list2
is a list or not:
您可以尝试以下approch,在其中检查list2中的元素是否为列表:
[[str(x)+'_'+str(el) for el in y] if isinstance(y, list)
else str(x)+"_"+str(y) for x,y in zip(list1,list2)]
Which gives the expected output:
这给出了预期的输出:
['H1_item1',
['H2_item2', 'H2_item3'],
['H3_item4', 'H3_item5', 'H3_item6'],
['H4_item7', 'H4_item8']]