Home > Mobile >  Concatenate each element of a list with all the elements of its list and put these strings inside a
Concatenate each element of a list with all the elements of its list and put these strings inside a

Time:01-16

#Load the input lists

load_names_list = ['Katherine', 'María Jose', 'Steve']
load_surnames_list = ['Taylor', 'Johnson', 'White', 'Clark']


#Shuffle the data lists: ['element_AA element_AA' , 'element_AA element_AB' , ... ]

names_with_names_list = []
surnames_with_surnames_list = []
names_with_surnames_list = []

print(names_with_names_list)
print(surnames_with_surnames_list)
print(names_with_surnames_list)
  • names_with_names_list matches each name with each of the names within the list load_names_list, for example: name_A name_A , name_A name_B, always leaving a space in between

  • surnames_with_surnames_list It is the same process as with the elements of list names_with_names_list, but using the list load_surnames_list

  • names_with_surnames_list this case is somewhat different since you must test all possible orders between the elements of the 2 lists ( load_names_list and load_surnames_list )

The concatenation that is giving me the most problems is the one required to obtain the list names_with_surnames_list

This would be the output that should be returned when printing each of these 3 lists of strings.

#for names_with_names_list
['Katherine Katherine', 'Katherine María Jose', 'Katherine Steve', 'María Jose Katherine', 'María Jose María Jose', 'María Jose Steve', 'Steve Katherine', 'Steve María Jose', 'Steve Steve']

#for surnames_with_surnames_list
['Taylor Taylor', 'Taylor Johnson', 'Taylor White', 'Taylor Clark', 'Johnson Taylor', 'Johnson Johnson', 'Johnson White', 'Johnson Clark', 'White Taylor', 'White Johnson', 'White White', 'White Clark', 'Clark Taylor', 'Clark Johnson', 'Clark White', 'Clark Clark']

#for names_with_surnames_list
['Katherine Taylor', 'Katherine Johnson', 'Katherine White', 'Katherine Clark', 'María Jose Taylor', 'María Jose Johnson', 'María Jose White', 'María Jose Clark', 'Steve Taylor', 'Steve Johnson', 'Steve White', 'Steve Clark', 'Taylor Katherine', 'Taylor María Jose', 'Taylor Steve', 'Johnson Katherine', 'Johnson María Jose', 'Johnson Steve', 'White Katherine', 'White María Jose', 'White Steve', 'Clark Katherine', 'Clark María Jose', 'Clark Steve']

What should I do to get these 3 lists from the 2 input lists? (note that there is always a whitespace between the elements)

CodePudding user response:

You can use itertools.product (also chain for the case with two different lists):

from itertools import product, chain

load_names_list = ['Katherine', 'María Jose', 'Steve']
load_surnames_list = ['Taylor', 'Johnson', 'White', 'Clark']

names_with_names_list = [f"{a} {b}" for a,b in product(load_names_list, repeat=2)]
surnames_with_surnames_list = [f"{a} {b}" for a,b in chain(product(load_names_list, load_surnames_list),
                                                           product(load_surnames_list, load_names_list))]
names_with_surnames_list = [f"{a} {b}" for a,b in product(load_surnames_list, repeat=2)]

CodePudding user response:

You can use list comprehenssion and sum:

names_with_surnames_list = sum(([n2   ' '   n1 for n1 in load_surnames_list]
                               for n2 in load_names_list), [])

This generates a list for each first name concatenated with every last name, and then sum concatenated them into one list.

You can similarly do this for the rest of the cases

  • Related