0

If I have two files:

file car.txt

ford, Chrysler, pontiac, cadillac 

file color.txt

red, green, white, yellow

What is the pythonic way to make all possible combination of color and car?

example output

ford red
ford green
ford white
ford yellow
Chrysler red
Chrysler green
and so on...
Community
  • 1
  • 1
  • 1
    [`from product from itertools`](https://docs.python.org/2/library/itertools.html#itertools.product) or simple nested for loops. – R Nar Dec 15 '15 at 21:14
  • 1
    what have you tried and what is not working? You're asking for the most pythonic way but it would help to see what you've tried. – Mike McMahon Dec 15 '15 at 21:15
  • Does it have to be car then color or can it be both color then car and car then color? If order doesn't matter you can just leverage itertools.permutations – dkroy Dec 15 '15 at 21:18
  • I try nested for loops but the first for loop stop it at the first line – mario Letterman Dec 15 '15 at 21:19
  • show us the code that did not work. – Mike McMahon Dec 15 '15 at 21:21
  • for car in carfile: for color in colorfile: print color.rstrip(), car ford red ford green ford white ford yellow Process finished with exit code 0 – mario Letterman Dec 15 '15 at 21:30
  • I understand you're quite new here, it would be best to put this in the body of the question as some sample output. You can preface lines with 4 spaces to give them a "code" like output. – Mike McMahon Dec 15 '15 at 21:33
  • 1
    @marioLetterman: Please [edit] your post to include any additional information you have to your question. Avoid adding this in the comments, as they are harder to read and can be deleted easier. The edit button for your post is just below the post's tags. – Remi Guan Dec 15 '15 at 21:42

3 Answers3

3

Here you go:

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.
abe
  • 504
  • 4
  • 13
  • what if the files have much more of 4 line? – mario Letterman Dec 15 '15 at 22:04
  • @marioLetterman You must figure out how you will cipher thought and assemble lists a and b. This code will work with any two lists of any lengths. Add 'acura' to list a and 'black' to list b and see what happens. – abe Dec 15 '15 at 22:05
  • Ok, but how do i fill the list if i have 400 items? Can I import from file? – mario Letterman Dec 15 '15 at 22:23
  • @marioLetterman You should open a question for that and show the file so I can understand how we would process it. – abe Dec 15 '15 at 22:24
1

You could simply use two for loop like this:

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)
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
0

Pythonic would mean using the tools available.

Use the csv module to read the comma separated line:

with open('cars.txt') as cars_file:
    cars = next(csv.reader(cars_file))

with open('colors.txt') as colors_file:
    colors = next(csv.reader(colors_file))

Use itertools.product to create the Cartesian product:

from itertools import product

In Python 3.x:

for car, color in product(cars, colors):
    print(car, color)

In Python 2.7:

for car, color in product(cars, colors):
    print car, color

In one line:

print('\n'.join('{car} {color}'
                .format(car=car, color=color)
                for car, color in product(cars, colors)))
Peter Wood
  • 23,859
  • 5
  • 60
  • 99