简体   繁体   English

如何使用python itertools模块

[英]How to use python itertools module

I have got the following code:我有以下代码:

import itertools


x = ['Lebron' 'James']
y = ['is', 'the', 'goat']
z = ['is', 'not', 'the', 'goat']
itertools.chain(x, y)

And I get the following output: itertools.chain at 0x104baab50我得到以下输出: itertools.chain at 0x104baab50

What does this output mean?这个输出是什么意思? And how can I see the result of the method?我怎样才能看到该方法的结果?


Then the same for the code below:那么下面的代码也是一样的:

itertools.chain.from_iterable([x,y])

I get the following output: itertools.chain at 0x104af7550我得到以下输出: itertools.chain at 0x104af7550

What does this mean?这是什么意思? And how can I see the actual result of the method?我怎样才能看到该方法的实际结果? I'm not quite sure what the difference is between the two methods.我不太确定这两种方法之间有什么区别。

itertools.chain returns a iterator that will allow you to iterate through the values in a for loop, via __next__ , etc. like a regular Python iterator . itertools.chain返回一个迭代器,它允许您通过__next__等方式迭代for循环中的值,就像常规 Python 迭代器一样

For example:例如:

In [3]: import itertools
   ...: x = ['Lebron', 'James']
   ...: y = ['is', 'the', 'goat']
   ...: z = ['is', 'not', 'the', 'goat']

In [4]: for thing in x:
   ...:     print('Thing is', thing)
   ...:
Thing is Lebron
Thing is James

In [5]: for thing in itertools.chain(x, y):
   ...:     print('Thing is', thing)
   ...:
Thing is Lebron
Thing is James
Thing is is
Thing is the
Thing is goat

and from_iterable takes and iterable (eg a list) of iterables (eg other lists) and iterates over each in turn:from_iterable需要和 iterable (例如列表)的可迭代对象(例如其他列表)并依次迭代每个:

In [8]: for thing in itertools.chain.from_iterable([x, y]):
   ...:     print('Thing is', thing)
   ...:
Thing is Lebron
Thing is James
Thing is is
Thing is the
Thing is goat

Simply put, chain is not a function;简单地说, chain不是一个函数; it's a type .这是一种类型 Like most types, you get back an instance of that type when you call it.与大多数类型一样,调用该类型时会返回该类型的实例。 The instance chain(x,y) is iterable;实例chain(x,y)是可迭代的; it first yields elements from x , and when it exhausts x , it yields elements from y .它首先从x产生元素,当它耗尽x ,它会从y产生元素。

chain.from_iterable is a class method; chain.from_iterable是一个类方法; it's definition is effectively the same as它的定义实际上与

def from_iterable(itr):
    return chain(*itr)

assuming you could use * with an arbitrary iterable.假设您可以将*与任意可迭代对象一起使用。

You should see more about the itertools module in Python Docs .您应该在Python Docs 中看到更多关于itertools模块的信息。

What does this output mean?这个输出是什么意思? And how can I see the result of the method?我怎样才能看到该方法的结果?

An itertools.chain object was returned.返回了一个itertools.chain对象。 This is a generator.这是一个发电机。 You can see the result in such a way for example (iterate through the values using for loop):例如,您可以通过以下方式查看结果(使用for循环遍历值):

for item in itertools.chain(x, y):
   print(item)

or this way (make a list from this iterable):或者这样(从这个可迭代列表中创建一个列表):

print(list(itertools.chain(x, y)))


itertools.chain(*iterables) itertools.chain(*iterables)

Here you are passing several iterables to create a chain from as a function arguments directly:在这里,您将传递几个可迭代对象以直接作为函数参数创建一个链:

itertools.chain(x, y)

itertools.chain.from_iterable(iterable) itertools.chain.from_iterable(可迭代)

And here you are passing a single iterable which contains other iterables to create a chain from:在这里,你路过它包含其他iterables从创建链单一迭代:

itertools.chain.from_iterable([x, y])

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

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