将标签添加到itertools.product生成的数据集中

时间:2016-12-27 10:02:07

标签: python-2.7 iterator iteration

我使用python 2.7 iter.product生成三个数据集的笛卡尔积。

这与三个嵌套循环相同。然而,在进入循环之前,我想打印标签" Fruit Type:"和"树类型:"如果那不清楚我已经把我想要的输出的例子放在下面。

什么是pythonic方式,我可以生成笛卡尔。集合的产品,然后在后期处理中添加标签,但是没有破坏使用itertools减少内存占用的目的?

代码示例

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)]

想要输出

***************************************
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 个答案:

答案 0 :(得分:1)

这是一种方式:

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'), ...];其余的只是字符串的格式。

通常很好的建议来收集您的数据'首先,尽可能晚地进行字符串格式化。