Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

Merging two list elements into one in python?

I have two lists say:

list1 = ['a','b','a','b']
l2 =['e','d']

how do I merg into a new list

 l3 =['a_e','b_e','a_d',b_d']
question from:https://stackoverflow.com/questions/65840836/merging-two-list-elements-into-one-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this way:

list1 = ['a','b','a','b']
l2 =['e','d']

print([el1+'_'+el2 for el2 in l2 for el1 in sorted(set(list1))])

Output:

['a_e', 'b_e', 'a_d', 'b_d']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...