将元组列表混合并迭代到文本模板中

时间:2018-11-14 20:46:26

标签: python arrays django python-3.x

我有一个列表和一个元组列表,每个元组都包含字符串:

names = ['joseph', 'parker', 'john']
interests = [('apples', 'design'),('bananas','economy'),('pears','medicine')]

text_template = 'hi my name is {name}, i like {interests}, im studying {interests} everytime / '

我正在努力实现这一目标:

hi my name is joseph, i like apples, im studying design everytime / 
hi my name is joseph, i like bananas, im studying economy everytime / 
hi my name is joseph, i like pears, im studying medicine everytime 


hi my name is parker, i like apples, im studying design everytime / 
hi my name is parker, i like bananas, im studying economy everytime / 
hi my name is parker, i like pears, im studying medicine everytime 

hi my name is john, i like apples, im studying design everytime / 
hi my name is john, i like bananas, im studying economy everytime / 
hi my name is john, i like pears, im studying medicine everytime 

谢谢大家!

2 个答案:

答案 0 :(得分:1)

我建议您对模板进行一些更改,以使字段名称不重复。

text_template = 'hi my name is {name}, i like {interest}, im studying {degree} everytime / '

现在您可以这样做:

>>> from itertools import product
>>> for name, (interest, degree) in product(names, interests):
...:    print(text_template.format(name=name, interest=interest, degree=degree))
...:    
hi my name is joseph, i like apples, im studying design everytime / 
hi my name is joseph, i like bananas, im studying economy everytime / 
hi my name is joseph, i like pears, im studying medicine everytime / 
hi my name is parker, i like apples, im studying design everytime / 
hi my name is parker, i like bananas, im studying economy everytime / 
hi my name is parker, i like pears, im studying medicine everytime / 
hi my name is john, i like apples, im studying design everytime / 
hi my name is john, i like bananas, im studying economy everytime / 
hi my name is john, i like pears, im studying medicine everytime / 

...,如果空白行很重要:

>>> for i, (name, (interest, degree)) in enumerate(product(names, interests), 1):
...:    print(text_template.format(name=name, interest=interest, degree=degree))
...:    if not i%len(names):
...:        print()
...:        
hi my name is joseph, i like apples, im studying design everytime / 
hi my name is joseph, i like bananas, im studying economy everytime / 
hi my name is joseph, i like pears, im studying medicine everytime / 

hi my name is parker, i like apples, im studying design everytime / 
hi my name is parker, i like bananas, im studying economy everytime / 
hi my name is parker, i like pears, im studying medicine everytime / 

hi my name is john, i like apples, im studying design everytime / 
hi my name is john, i like bananas, im studying economy everytime / 
hi my name is john, i like pears, im studying medicine everytime / 

答案 1 :(得分:0)

names = ['joseph', 'parker', 'john']
interests = [('apples', 'design'),('bananas','economy'),('pears','medicine')]
for i in names:
    for j in interests:
        text_template = 'hi my name is '+i + ', i like '+ j[0]+', im studying '+j[1]+' everytime / '
        print (text_template)

这应该有效。