我需要生成一个文件,其中每行是一个句子(模板)和四个变量的连接。 这四个变量(分别是它们的值)之前已经收集在单独的列表中。 到目前为止,我曾尝试:Python - 多个列表中的字符串连接?
num = [1,2,3]
fruit = [grape ,banana,pea]
fruits = [grapes, bananas, peas]
with open('result', 'w') as r:
for n,f,g in num,fruit,fruits:
r.writelines("This is string number %d. This is a %s. And I like to eat %s" % (num[n],fruit[f],fruits[g]))
...这种方法有:
TypeError: list indices must be integers, not tuple
。
为了减少“复杂性”,我还没有将它与不同的列表一起“混杂”,即首先将第一个和第二个变量+字符串与中间列表连接起来以减少“复杂性”。
有人可以给我一个暗示我失败的地方吗?
感谢您的编辑! –
感谢大家的快速和现场帮助!我显然没有完全掌握迭代/极限的概念。 –