from __future__ import print_function
# remove the above line if you're using Python 3.x
with open('color.txt') as f:
colors = ', '.join(f.read().splitlines()).split(', ')
with open('car.txt') as f:
for i in f:
for car in i.strip().split(', '):
for color in colors:
print(car, color)
import itertools
a = ['ford', 'Chrysler', 'pontiac', 'cadillac']
b = ['red', 'green', 'white', 'yellow']
for r in itertools.product(a, b):
print (r[0] + " " + r[1])
print (list(itertools.product(a,b))) #If you would like the lists for later modification.
您可以简单地使用两个for循环,如下所示:
给你:
Pythonic意味着使用可用的工具。在
使用^{} 模块读取逗号分隔的行:
使用^{} 创建Cartesian product:
^{pr2}$在Python 3.x中:
在Python 2.7中:
在一行中:
相关问题 更多 >
编程相关推荐