简体   繁体   English

Python 打印语句打印地址而不是值

[英]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: Output:

<itertools.product object at 0x7f02bdedb500>

Output that I want:我想要的 Output:

(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.我仍然无法得到那个 output。 I don't want the output as a list, the expected output is shown above.我不想将 output 作为列表,预期的 output 如上所示。

product(lst1,lst2) returns a itertools.product object just use map function to update internal tuple 1st index item or iterate though each element. product(lst1,lst2)返回一个itertools.product object 只需使用map function来更新每个内部元组第一个索引项或迭代。

So use map function and update each tuple by 1 using lambda function:所以使用 map function 并使用 lambda ZC1C425268E683894F1AB4A 将每个元组更新 1

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

OUTPUT: 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.这个 output 的问题是它是list object的字符串表示,所以最好的方法是使用这种方法 go。
So use iterate though this iterable object:所以通过这个可迭代的 object 使用迭代:

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

OUTPUT: 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: “itertools.product”返回一个生成器,以获取您需要的列表 output:

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:您可以迭代 l3 以获得所需的 output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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