我有一个脚本,我试图创建.html文件。 在这个.html文件中有3行,左行,右行,中心行。 每个要生成的行都有两个参数,一个是标题,另一个是描述。
作为示例,我们将左行和右行元素存储在两个列表中,一个用于标题,另一个用于desc:
rightRowTitle = ['title2','title2']
rightRowDesc = ['Desc','Desc2']
leftRowTitle = ['l1','l2']
leftRowDesc = ['l1','l2']
以下是我所做的一个例子:
rightRowTitle = ['Tytul','Tytul2']
rightRowDesc = ['Desc','Desc2']
leftRowTitle = ['l1','l2']
leftRowDesc = ['l1','l2']
for title, desc in zip(rightRowTitle, rightRowDesc):
print(title,' - ', desc)
for title, desc in zip(leftRowTitle, leftRowDesc):
print(title,' + ', desc)
结果:
Tytul - Desc
Tytul2 - Desc2
l1 + l1
l2 + l2
现在,一旦我使用函数生成行,我希望得到这样的结果:
rightRowTitle
rightRowDesc
leftRowTitle
leftRowDesc
rightRowTitle2
rightRowDesc2
我希望它能像现在一样混合在一起。
答案 0 :(得分:0)
也许您可以将所有标题存储在list
中,将所有描述存储在另一个list
中。将它们混洗并逐一制作。
from random import shuffle
titles = ['Tytul','Tytul2', 'l1','l2']
descriptions = ['Desc','Desc2', 'l1','l2']
shuffle(titles)
shuffle(descriptions)
for t, d in zip(titles, descriptions):
print(title + ' - ' + desc)
很抱歉,如果这不是你想要的,但这是我从你的问题中理解的。
你会得到这样的东西:
// shuffled lists
titles = ['l2', 'Tytul2','Tytul', 'l1']
descriptions = ['Desc', 'l2','l1', 'Desc2']
// After the print
l2 - Desc
Tytul2 - l2
Tytul - l1
l1 - Desc2
@nestor
这是我的条件:
if len(rightRowTitle) > len(leftRowTitle):
for title, desc in zip(rightRowTitle, rightRowDesc):
right_row(title, desc)
for title, desc in zip(leftRowTitle, leftRowDesc):
left_row(title, desc)
问题是,这不会混合,它只是基本显示我的第一个左行然后右行结果。我想将它们混合在一起。
答案 1 :(得分:0)
您可以两次发送zip
。
rightRowTitle = ['Tytul','Tytul2']
rightRowDesc = ['Desc','Desc2']
leftRowTitle = ['l1','l2']
leftRowDesc = ['l1','l2']
[print(a) for a in zip(zip(rightRowTitle, rightRowDesc), zip(leftRowTitle, leftRowDesc))]
你会得到:
(('Tytul', 'Desc'), ('l1', 'l1'))
(('Tytul2', 'Desc2'), ('l2', 'l2'))
[None, None]
看一个更大的例子:
rightRowTitle = ['Title1Right','Title2Right', 'Title3Right', 'Title4Right', 'Title5Right']
rightRowDesc = ['Desc1Right','Desc2Right', 'Desc3Right', 'Desc4Right', 'Desc5Right']
leftRowTitle = ['Title1Left','Title2Left', 'Title3Left', 'Title4Left', 'Title5Left']
leftRowDesc = ['Desc1Left','Desc2Left', 'Desc3Left', 'Desc4Left', 'Desc5Left']
[print(a) for a in zip(zip(rightRowTitle, rightRowDesc), zip(leftRowTitle, leftRowDesc))]
你会得到:
(('Title1Right', 'Desc1Right'), ('Title1Left', 'Desc1Left'))
(('Title2Right', 'Desc2Right'), ('Title2Left', 'Desc2Left'))
(('Title3Right', 'Desc3Right'), ('Title3Left', 'Desc3Left'))
(('Title4Right', 'Desc4Right'), ('Title4Left', 'Desc4Left'))
(('Title5Right', 'Desc5Right'), ('Title5Left', 'Desc5Left'))
[None, None, None, None, None]
现在你只需要遍历结果就能得到你想要的东西!要调用您的函数,您可以执行以下操作:
def myFun(title, description):
print("Title: " + title + ". Desc: " + description)
def myFun2(title, description):
print("TITLE: " + title + ". DESC: " + description)
pairs = [a for a in zip(zip(rightRowTitle, rightRowDesc), zip(leftRowTitle, leftRowDesc))]
pairs2 = [pair for pair in pairs]
for p in pairs2:
myFun(p[0][0], p[0][1])
myFun2(p[1][0], p[1][1])
你会得到这样的东西:
Title: Title1Right. Desc: Desc1Right
TITLE: Title1Left. DESC: Desc1Left
Title: Title2Right. Desc: Desc2Right
TITLE: Title2Left. DESC: Desc2Left
Title: Title3Right. Desc: Desc3Right
TITLE: Title3Left. DESC: Desc3Left
Title: Title4Right. Desc: Desc4Right
TITLE: Title4Left. DESC: Desc4Left
Title: Title5Right. Desc: Desc5Right
TITLE: Title5Left. DESC: Desc5Left
您只需要使用您的功能更改myFun
和myFun2
。