2016-12-27 12 views
0

私はpython 2.7 iter.productを使用して3つのデータセットのデカルト積を生成しています。itertools.productによって生成されたデータセットにラベルを追加する

これは3つのネストされたループと同じです。しかし、ループに入る前に、ラベル "Fruit Type:"と "Tree Type:"を印刷したいと思います。

これを行うにはどうすればいいですか?私はデカルトを生成できます。そのラベルを後処理で追加しますが、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'), ...]。残りは文字列の書式設定にすぎません。

「データ」を最初に収集し、できるだけ後で文字列フォーマットを行うことは、通常はお勧めです。

関連する問題