简体   繁体   中英

Getting cartesian product of specified lists from multiple lists

Please don't mind my newbie style of writing the code.... Am just at beginner stage:: a more efficient correction is welcome.

Step 1: I have multiple list like this:

aaa = "abcd"
bbb = "efgh"
ccc = "ijkl"
ddd = "mnop"
eee = "qrst"
no1 = "1234"
no2 = "3456"
no3 = "7890"

listpack = [aaa, bbb, ccc, ddd, eee, no1, no2, no3]

Step 2: Generate random alphabets and numbers from those lists

all_list = (aaa+bbb+ccc+ddd+eee+no1+no2+no3) 
randgen = random.sample(all_list, k=10) #print(randgen)#: aem31kgcs9

ISSUE #Step 3: Get the cartesan product of only the list whose element startswith each character in randgen(step 2 above) #This is what i've tried

newlist=[]
for i in origin:
    for x in diclist:
        if x.startswith(i):
            newlist.append([x]) 

for xx in itertools.product(newlist):
    print(xx)

but it prints only the elements and also on separate line

I want to get the cartesian product of newlist

Thanks in advance :)

randgen=['f', 'q', '8', 'i', 's', '3', 'a', 'k', 'o', '1']
You can use list for appending

newlist=[]
for i in randgen:
    for x in listpack:
        if x.startswith(i):
            newlist.append(x)

newlist

Output: ['qrst', 'ijkl', '3456', 'abcd', '1234']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM