简体   繁体   中英

Python print statement prints address instead of value

This is the code which I have did

from itertools import product
lst1=list(map(int,input().split()))
lst2=list(map(int,input().split()))
l3=product(lst1,lst2)

Input:

1 2
2 3 

Output:

<itertools.product object at 0x7f02bdedb500>

Output that I want:

(1, 3) (1, 4) (2, 3) (2, 4)

I have tried adding parentheses, brackets and also tried to store the value in a variable and printed it. I still couldn't able to get that output. I don't want the output as a list, the expected output is shown above.

product(lst1,lst2) returns a itertools.product object just use map function to update internal tuple 1st index item or iterate though each element.

So use map function and update each tuple by 1 using lambda function:

l3= list(map(lambda i: (i[0], i[-1]+1), product(lst1,lst2)))
print(l3)

OUTPUT:

[(1, 3), (1, 4), (2, 3), (2, 4)]

Problem with this output is that it is string representation of list object so the best way is to go with this method.
So use iterate though this iterable object:

for i in l3:
    i = list(i)
    i[-1] += 1
    print(tuple(i), end=' ')

OUTPUT:

(1, 3) (1, 4) (2, 3) (2, 4)

Convert or cast it to list, it works.

from itertools import product
lst1=list(map(int,input().split()))
lst2=list(map(int,input().split()))
l3=list(product(lst1,lst2))

"itertools.product" returns a generator, to get the list output you need:

list(l3)

However, not sure where you got your values from, I got:

[(1, 2), (1, 3), (2, 2), (2, 3)]

You can iterate l3 to get the desired output:

for i in l3:
    print(i, end=" ")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM