请不要介意我的新手写代码.我只是在初级阶段::更有效的纠正是受欢迎的。
步骤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
问题#第3步:只获得其元素以随机元开头的列表的cartesan乘积(上面的步骤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)
但它只打印元素,也打印在单独的行上。
我想得到新列表的笛卡儿积
(预先谢谢:)
发布于 2022-06-25 10:34:33
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']
https://stackoverflow.com/questions/72752910
复制相似问题