2016-12-27 1 views
0

나는 python 2.7 iter.product를 사용하여 3 개의 데이터 세트의 데카르트 곱을 생성합니다.itertools.product에 의해 생성 된 데이터 세트에 레이블 추가하기

이것은 세 개의 중첩 루프와 같습니다. 그러나 루프를 입력하기 전에 레이블 "과일 유형 :"및 "트리 유형 :"을 인쇄하고 싶습니다. 명확하지 않으면 Ive는 내가 원하는 출력의 예를 넣습니다.

무엇을 할 파이썬 방법은 데카르트를 생성 할 수 있습니다. 제품을 사후 처리에서 레이블을 추가하지만 메모리 풋 프린트를 줄이기 위해 itertools를 사용하는 목적을 무효화하지는 않습니까?

출력이는 방법입니다

 
*************************************** 
Fruit Type: apple 
*************************************** 
--------------------------------------- 
Tree Type: ash 
--------------------------------------- 
fruit is apple, tree is ash, animal is dog 
fruit is apple, tree is ash, animal is cat 
fruit is apple, tree is ash, animal is horse 
--------------------------------------- 
Tree Type: oak 
--------------------------------------- 
fruit is apple, tree is oak, animal is dog 
fruit is apple, tree is oak, animal is cat 
fruit is apple, tree is oak, animal is horse 
--------------------------------------- 
Tree Type: beech 
--------------------------------------- 
fruit is apple, tree is beech, animal is dog 
fruit is apple, tree is beech, animal is cat 
fruit is apple, tree is beech, animal is horse 
--------------------------------------- 
Tree Type: ash 
--------------------------------------- 
*************************************** 
Fruit Type: orange 
*************************************** 
--------------------------------------- 
Tree Type: ash 
--------------------------------------- 

fruit is orange, tree is ash, animal is dog 
fruit is orange, tree is ash, animal is cat 
fruit is orange, tree is ash, animal is horse 
--------------------------------------- 
Tree Type: oak 
--------------------------------------- 
fruit is orange, tree is oak, animal is dog 
fruit is orange, tree is oak, animal is cat 
fruit is orange, tree is oak, animal is horse 
--------------------------------------- 
Tree Type: beech 
--------------------------------------- 
fruit is orange, tree is beech, animal is dog 
fruit is orange, tree is beech, animal is cat 
fruit is orange, tree is beech, animal is horse 
*************************************** 
Fruit Type: pear 
*************************************** 
--------------------------------------- 
Tree Type: ash 
--------------------------------------- 

fruit is pear, tree is ash, animal is dog 
fruit is pear, tree is ash, animal is cat 
fruit is pear, tree is ash, animal is horse 
--------------------------------------- 
Tree Type: oak 
--------------------------------------- 
fruit is pear, tree is oak, animal is dog 
fruit is pear, tree is oak, animal is cat 
fruit is pear, tree is oak, animal is horse 
--------------------------------------- 
Tree Type: beech 
--------------------------------------- 
fruit is pear, tree is beech, animal is dog 
fruit is pear, tree is beech, animal is cat 
fruit is pear, tree is beech, animal is horse 

답변

1

import itertools 

FRUITS=['apple','orange','pear'] 
TREES=['ash','oak','beech'] 
ANIMALS=['dog','cat','horse'] 


def foo(a,b,c): 
    print "fruit is " + str(a) + ", tree is " + str(b) + ", animal is " + str(c) 

[ foo(a,b,c) for a, b, c in itertools.product(FRUITS, TREES, ANIMALS)] 

코드 샘플 :

from itertools import product 

FRUITS = ['apple', 'orange', 'pear'] 
TREES = ['ash', 'oak', 'beech'] 
ANIMALS = ['dog', 'cat', 'horse'] 

lst = [(fruit, tree, animal) for fruit, tree in product(FRUITS, TREES) 
     for animal in ANIMALS] 

fmt = 'fruit is {} tree {} is animal is {}' 
last_tree_type = None 
last_fruit_type = None 
for item in lst: 
    if last_fruit_type != item[0]: 
     last_fruit_type = item[0] 
     print(20*'=') 
     print('fruit type: {}'.format(item[0])) 
     print(20*'=') 
    if last_tree_type != item[1]: 
     last_tree_type = item[1] 
     print(20*'-') 
     print('tree type: {}'.format(item[1])) 
     print(20*'-') 
    print(fmt.format(*item)) 

lst가 세 쌍둥이를 포함 [('apple', 'ash', 'dog'), ('apple', 'ash', 'cat'), ...]; 나머지는 문자열의 형식입니다.

'데이터'를 먼저 수집하고 가능한 한 늦게 문자열 형식을 지정하는 것이 좋습니다.

관련 문제