Accessing elements of itertools product in a for loop

前端 未结 2 1738
我寻月下人不归
我寻月下人不归 2021-01-29 11:45

I have a list of list being the result of appending some other results from itertools product. What I want is to be able to access each element of the lists in the list of lists

相关标签:
2条回答
  • 2021-01-29 11:59

    itertools.product itself returns an object you can iterate over. For example:

    >>> from itertools import product
    >>> animal = ['dog','cat']
    >>> number = [5,45,8,9]
    >>> color = ['yellow','blue','black']
    >>> myProduct = product(animal, number, color)
    >>> for p in myProduct:
    ...     print p
    ...     
    ('dog', 5, 'yellow')
    ('dog', 5, 'blue')
    ('dog', 5, 'black')
    [...]
    ('cat', 9, 'blue')
    ('cat', 9, 'black')
    

    After this, myProduct is exhausted (out of elements to yield) and so if you loop again you won't get anything:

    >>> for p in myProduct:
    ...     print p
    ...     
    >>> 
    

    If you want to materialize myProduct into a list, you can do that:

    >>> myProduct = list(product(animal, number, color))
    >>> len(myProduct)
    24
    >>> myProduct[7]
    ('dog', 8, 'blue')
    

    By appending the product object itself to a list, you're not getting the list iterating it would produce, only the object.

    >>> myProduct = product(animal, number, color)
    >>> myProduct
    <itertools.product object at 0x8e3ce3c>
    

    If you want, you can store this object somewhere, and get elements from it the same way:

    >>> some_list = [myProduct, myProduct]
    >>> next(some_list[0])
    ('dog', 5, 'yellow')
    >>> next(some_list[1])
    ('dog', 5, 'blue')
    >>> next(some_list[1])
    ('dog', 5, 'black')
    >>> next(some_list[0])
    ('dog', 45, 'yellow')
    

    but here I don't think that's what you're looking for.

    0 讨论(0)
  • 2021-01-29 12:00

    Reading your post many times, I think you are interested in this syntax:

    for animal, number, color in ...:
    

    It allows you to access the elements from the inner lists.

    import itertools
    animals = ['dog', 'cat']
    numbers = [5, 45, 8, 9]
    colors = ['yellow', 'blue', 'black']
    for animal, number, color in itertools.product(animals, numbers, colors):
        print "Animal is {0}, number is {1}, and color is {2}".format(animal, number, color)
    

    See this running on ideone

    EDIT: Reading your comments in the other thread, I guess(?) you're interested in iterating over the inner tuples. You don't have to convert them to a list, you can simply iterate over them.

    import itertools
    animals = ['dog', 'cat']
    numbers = [5, 45, 8, 9]
    colors = ['yellow', 'blue', 'black']
    for triplet in itertools.product(animals, numbers, colors):
        for element in triplet:
            print element
    

    Demo on ideone

    EDIT 2: You can try that approach for your score thing.

    import itertools
    animals = ['dog', 'cat']
    numbers = [5, 45, 8, 9]
    colors = ['yellow', 'blue', 'black']
    general_list = ['horse', 'blue', 'dog', 45]
    
    def give_score(element):
        return 1 if element in general_list else 0
    
    for triplet in itertools.product(animals, numbers, colors):
        print sum([give_score(element) for element in triplet])
    

    Demo here

    0 讨论(0)
提交回复
热议问题