簡體   English   中英

從多個列表中獲取指定列表的笛卡爾積

[英]Getting cartesian product of specified lists from multiple lists

請不要介意我的新手編寫代碼的風格......我只是在初學者階段:: 歡迎更有效的更正。

第 1 步:我有多個這樣的列表:

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]

第 2 步:從這些列表中生成隨機字母和數字

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

ISSUE #Step 3:僅獲取元素以randgen中每個字符開頭的列表的笛卡爾積(上面的步驟2)#這是我嘗試過的

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

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

但它只打印元素,也打印在單獨的行上

我想得到 newlist 的笛卡爾積

提前致謝 :)

randgen=['f', 'q', '8', 'i', 's', '3', 'a', 'k', 'o', '1']
您可以使用列表追加

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

newlist

輸出: ['qrst', 'ijkl', '3456', 'abcd', '1234']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM