简体   繁体   English

Python-chain.from_iterable返回内存地址而不是值

[英]Python-chain.from_iterable returns memory address instead of value

I'm trying to write a simple program for taking in a set of elements and printing out the powerset. 我正在尝试编写一个简单的程序来接收一组元素并打印出powerset。

I'm using the recipe for a powerset function as given in python's documentation: https://docs.python.org/3.4/library/itertools.html 我正在使用python文档中给出的Powerset函数配方: https ://docs.python.org/3.4/library/itertools.html

For some reason instead of returning values, it returns the memory address of a itertools.chain object. 由于某种原因而不是返回值,它返回itertools.chain对象的内存地址。

Why is it doing this? 为什么这样做呢?

from itertools import chain,combinations
def pwrst(iter):
    xs = list(iter)
    return chain.from_iterable( combinations(xs,n) for n in range(len(xs)+1))
s = input("Enter elements seperated by comma: ")
il = s(',')
ss = [int(x.strip()) for x in il]
pwrst(ss)

chain.from_iterable returns an iterable object (whose default __repr__ tells you the memory address). chain.from_iterable返回一个可迭代的对象 (其默认__repr__告诉您内存地址)。 You actually have to iterate over it to get the values. 实际上,您必须对其进行迭代以获得值。 eg: 例如:

list(pwrst(ss))

Under normal circumstances, you might do something like: 在正常情况下,您可以执行以下操作:

for item in pwrst(ss):
    ...

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

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