Python组合字符串并多次打印

Python组合字符串并多次打印,python,string,printing,combinations,Python,String,Printing,Combinations,组合字符串并多次打印 假设我有两个列表,我想打印第一个列表的每个元素,然后打印第二个列表的每个元素。为了好玩,我可以在这两个元素之间加一个词吗,比如“和” 例如: firstList = (“cats”, “hats”, “rats”) secondList = (“dogs”, “frogs”, “logs”) 我想要的是: cats and dogs cats and frogs cats and logs hats and dogs hats and frogs hats and log

组合字符串并多次打印

假设我有两个列表,我想打印第一个列表的每个元素,然后打印第二个列表的每个元素。为了好玩,我可以在这两个元素之间加一个词吗,比如“和”

例如:

firstList = (“cats”, “hats”, “rats”)
secondList = (“dogs”, “frogs”, “logs”)
我想要的是:

cats and dogs
cats and frogs
cats and logs
hats and dogs
hats and frogs
hats and logs
rats and dogs
etc...

如果我明白你的意思,这应该很容易

for item1 in firstlist:
    for item2 in secondlist:
        print(item1+ " and "+item2)

如果我明白你的意思,这应该很容易

for item1 in firstlist:
    for item2 in secondlist:
        print(item1+ " and "+item2)

您可以将其作为嵌套列表执行

items = ['%s and %s' % (a,b) for b in secondList for a in firstList]
如果只想打印值,可以插入
print
语句

ignore = [print('%s and %s' % (a,b)) for b in secondList for a in firstList]
或者如果你愿意的话


您可以将其作为嵌套列表执行

items = ['%s and %s' % (a,b) for b in secondList for a in firstList]
如果只想打印值,可以插入
print
语句

ignore = [print('%s and %s' % (a,b)) for b in secondList for a in firstList]
或者如果你愿意的话


对于s,您可以使用带有两个
的列表理解:

>>> words = [x + " and " + y for x in firstList for y in secondList]
>>> print(*words, sep="\n")
cats and dogs
cats and frogs
cats and logs
hats and dogs
hats and frogs
hats and logs
rats and dogs
rats and frogs
rats and logs
如果要枚举该列表,可以使用如下方式使用
enumerate

>>> words = ["{}: {} and {}".format(i, x, y) for i, (x, y) in enumerate([(x, y) for x in firstList for y in secondList])]
>>> print(*words)
0: cats and dogs
1: cats and frogs
2: cats and logs
3: hats and dogs
4: hats and frogs
5: hats and logs
6: rats and dogs
7: rats and frogs
8: rats and logs

要使编号从1开始,请将
“{}:{}和{}”.format(i,x,y)
更改为
“{}:{}和{}”。format(i+1,x,y)

您可以使用两个
的列表理解:

>>> words = [x + " and " + y for x in firstList for y in secondList]
>>> print(*words, sep="\n")
cats and dogs
cats and frogs
cats and logs
hats and dogs
hats and frogs
hats and logs
rats and dogs
rats and frogs
rats and logs
如果要枚举该列表,可以使用如下方式使用
enumerate

>>> words = ["{}: {} and {}".format(i, x, y) for i, (x, y) in enumerate([(x, y) for x in firstList for y in secondList])]
>>> print(*words)
0: cats and dogs
1: cats and frogs
2: cats and logs
3: hats and dogs
4: hats and frogs
5: hats and logs
6: rats and dogs
7: rats and frogs
8: rats and logs

要使编号从1开始,将
“{}:{}和{}”.format(i,x,y)
更改为
“{}:{}和{}”.format(i+1,x,y)

除了其他答案之外,另一种方法是使用
itertools.product

import itertools

firstList = (“cats”, “hats”, “rats”)
secondList = (“dogs”, “frogs”, “logs”)

for item in itertools.product(firstList, secondList):
  print(item[0] + " and " + item[1])

除了其他答案外,另一种方法是使用
itertools.product

import itertools

firstList = (“cats”, “hats”, “rats”)
secondList = (“dogs”, “frogs”, “logs”)

for item in itertools.product(firstList, secondList):
  print(item[0] + " and " + item[1])

你是怎么做的?即使是一个简单的for循环也是一个很好的开始。你自己做过什么?即使是一个简单的for循环也会是一个很好的开始谢谢你!如果我想给名单编号呢?非常感谢!如果我想给名单编号呢?