对迭代器的延迟计算修改链接在一起

iterator-chain的Python项目详细描述


迭代器链

链接在一起,懒洋洋地计算对迭代器的修改。

安装

requirements.txt文件中包含iterator-chain和/或使用pip安装它。

$ pip install iterator-chain

API

从导入包开始。

importiterator_chain

启动链条

要启动链,请使用from_iterable函数。它需要一个iterable。

an_iterable=[5,78,12,26]iterator_chain.from_iterable(an_iterable)
FunctionArgumentsDescription
^{}• ^{} - An iterable to be used in the iterator chainStarts the iterator chain with the supplied iterable. Chaining and terminating methods can now be called on the result.

继续链条

从那里,可以调用大量额外的方法来修改最初传入的iterable。方法是 概述如下。这些方法分为两类:链接或终止。

  • 链接方法对迭代器中的元素应用一些修改,但保持链的活动性。 这允许随后对结果调用其他链接方法。 由于修改是惰性计算的,因此在终止方法之后的之前,不会应用链接方法中的任何修改。 打电话。
  • 终止方法还对迭代器中的元素应用某些修改、请求某些信息或执行某些操作。他们返回来阻止锁链 实际值。此值将取决于首先执行的所有先前链接方法。

链接方法
MethodArgumentsDescription
^{}• ^{} - A function that takes a single argumentWill run the ^{} across all the elements in the iterator.
^{}• ^{} - A function that takes a single argumentWill run the ^{} on every element. ^{} should return a truthy or falsy value. On true, the element will stay; on false, the element will be removed.
^{}• ^{} - An integerThe ^{} number of elements will be skipped over and effectively removed.
^{}Any duplicates will be removed.
^{}• ^{} - An integerThe iterator will stop after ^{} elements. Any elements afterward are effectively removed.
^{}Any element that is an iterable itself will have its elements iterated over first before continuing with the remaining elements. Strings (^{}) do not count as an iterable for this method. Dictionaries flatten to its item tuples.
^{}• ^{} - Keyword. A function of one argument that is used to extract a comparison key from each element
• ^{} - Keyword. A Python 2.x "cmp" function that takes two arguments
• ^{} - Keyword. If set to ^{}, the elements will be sorted in the reverse order.
Sorts the iterator based on the elements' values. Use ^{} or ^{} to make a custom comparison. If ^{} is specified, ^{} cannot be used. This method is expensive because it must serialize all the values into a sequence.
^{}Reverses the iterator. The last time will be first, and the first item will be last. This method is expensive because it must serialize all the values into a list.

终止方法
MethodArgumentsDescription
^{}Serializes the iterator chain into a ^{} and returns it.
^{}Returns the number of elements in the iterator
^{}• ^{} - Keyword. Any value.Returns just the first item in the iterator. If the iterator is empty, the ^{} is returned.
^{}• ^{} - Keyword. Any value.Returns just the last item in the iterator. If the iterator is empty, the ^{} is returned.
^{}• ^{} - Keyword. Any value.Returns the largest valued element in the iterator. If the iterator is empty, the ^{} is returned.
^{}• ^{} - Keyword. Any value.Returns the smallest valued element in the iterator. If the iterator is empty, the ^{} is returned.
^{}• ^{} - Keyword. Any value.Sums all the elements in the iterator together. If any of the elements are un-summable, the ^{} is returned.
^{}• ^{} - A function that takes two argumentsApplies the function to two elements in the iterator cumulatively. Subsequent calls to ^{} uses the previous return value from ^{} as the first argument and the next element in the iterator as the second argument. The final value is returned.
^{}• ^{} - A function that takes one argument and returns nothingExecutes ^{} on every element in the iterator. There is no return value. If you are wanting to return a list of values based on the function, use ^{}.
^{}• ^{} - A function that takes one argument and returns a booleanReturns ^{} only if all the elements return ^{} after applying the ^{} to them. Else returns ^{}.
^{}• ^{} - A function that takes one argument and returns a booleanReturns ^{} if just one element return ^{} after applying the ^{} to it. If all elements result in ^{}, ^{} is returned.
^{}• ^{} - A function that takes one argument and returns a booleanReturns ^{} only if all the elements return ^{} after applying the ^{} to them. Else returns ^{}.

示例

importiterator_chainan_iterable=[5,78,12,26]iterator_chain.from_iterable(an_iterable) \  #starts the chain.map(lambdaelement:element*2) \  #multiplies every element by two: [10, 156, 24, 52].filter(lambdaelement:element>32) \  #keeps any element greater than 32: [156, 52].map(lambdaelement:element/3) \ #divides every element by three: [52.0, 17.333333333333332].list()#and finally returns a list of the result for later use in your application: [52.0, 17.333333333333332]

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在一个问题被连续正确回答三次/并添加差异后,我如何将程序循环回开始   Java中未实例化的匿名类   java如何在Android中录制视频,只允许横向模式和最长时间录制时间   java从另一个活动发送实时消息   多线程java线程和互斥   java禁用Spring安全日志   JAVA伊奥。StreamCorruptedException:在与子级和父级ProcessBuilder通信时写入子级中的标准输出时,流头无效   使用Java(HttpURLConnection)对Restheart进行身份验证(对于Mongodb)   java如何解决Jenkins中的SAXParseException?   java为什么我需要mockito来测试Spring应用程序?   计算sin-cos和tan时缺乏精度(java)   java Hibernate。不同项目中相同一对一映射的不同行为   java图像滑块:如何使用JavaFX将图像放在另一个图像上   java Mockito在使用when时抛出NotAMockException   http Java servlet发送回响应