itertools的一些扩展 [英] a few extensions for the itertools

查看:62
本文介绍了itertools的一些扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python(s itertools)中写了一些恕我直言的函数。


你可以在这里下载它们:
http://sourceforge.net/project/showf...kage_id=212104


所有函数的简短描述:


icmp(iterable1,iterable2)-integer

返回否定如果iterable1< iterable2,

如果iterable1 = = iterable1则为零,
如果iterable1为iterable1,则为



isum(iterable,start = 0) -value

返回可迭代元素的总和
加上参数''start''的值。当

iterable为空时,返回start。

iproduct(iterable,start = 0)-value

返回元素的乘积a iterable

乘以参数''start''的值。当

iterable为空时,返回start。

forall(谓词,可迭代,默认= True)-bool

返回True,何时为可迭代的所有元素x

谓词(x)为True。当iterable为空时,

返回默认值。

forany(谓词,可迭代,默认= False)-bool

返回True,何时为迭代中的任何元素x

谓词(x)为True。当iterable为空时,

返回默认值。

take(n,iterable)-iterator

返回第一个n
迭代器的元素

drop(n,iterable)-iterable

删除可迭代的前n个elemet和

在其余部分返回一个迭代器

头部(可迭代) - 所有头部的人 -

例如:
头部为头部的
([1,2] ,3,4,5,6,7,8,9]):

打印头


输出:

[ ]

[1]

[1,2]

[1,2,3]

[1 ,2,3,4]

[1,2,3,4,5]

[1,2,3,4,5,6]
[1,2,3,4,5,6,7]

[1,2,3,4,5,6,7,8]

[1,2,3,4,5,6,7,8,9]

尾巴(可迭代) - 所有尾巴上的东西

例如:<尾部尾部为
([1,2,3,4,5,6,7,8,9]):

打印尾巴

输出:

[1,2,3,4,5,6,7,8,9]

[2,3,4,5] ,6,7,8,9]

[3, 4,5,6,7,8,9]

[4,5,6,7,8,9]

[5,6,7,8, 9]

[6,7,8,9]

[7,8,9]

[8,9]
[9]

[]

fcain(功能,*功能)-function(...,***)

fcain(f1,f2,...,fn)(* args,* kwargs)等于f1(f2(... fn(* args,* kwargs)))

I wrote a few functions which IMHO are missing in python(s itertools).

You can download them here:
http://sourceforge.net/project/showf...kage_id=212104

A short description to all the functions:

icmp(iterable1, iterable2) -integer
Return negative if iterable1 < iterable2,
zero if iterable1 == iterable1,
positive if iterable1 iterable1.

isum(iterable, start=0) -value
Returns the sum of the elements of a iterable
plus the value of parameter ''start''. When the
iterable is empty, returns start.
iproduct(iterable, start=0) -value
Returns the product of the elements of a iterable
times the value of parameter ''start''. When the
iterable is empty, returns start.
forall(predicate, iterable, default=True) -bool
Returns True, when for all elements x in iterable
predicate(x) is True. When the iterable is empty,
returns default.
forany(predicate, iterable, default=False) -bool
Returns True, when for any element x in iterable
predicate(x) is True. When the iterable is empty,
returns default.
take(n,iterable) -iterator
returns a iterator over the first n
elements of the iterator
drop(n,iterable) -iterable
drops the first n elemetns of iterable and
return a iterator over the rest
heads(iterable) -iterator over all heads
example:
for head in heads([1,2,3,4,5,6,7,8,9]):
print head

output:
[]
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
tails(iterable) -iterator over all tails
example:
for tail in tails([1,2,3,4,5,6,7,8,9]):
print tail

output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[6, 7, 8, 9]
[7, 8, 9]
[8, 9]
[9]
[]
fcain(funct,*functs) -function(...,***)
fcain(f1,f2,...,fn)(*args,*kwargs) equals f1(f2(...fn(*args,*kwargs)))

推荐答案

" Mathias Panzenboeck" < e0 ****** @ student.tuwien.ac.atwrote in message

news:45 ******************** ***@tunews.univie.ac.at ...
"Mathias Panzenboeck" <e0******@student.tuwien.ac.atwrote in message
news:45***********************@tunews.univie.ac.at ...

>我在python(s itertools)中写了一些恕我直言的函数。 />

你可以在这里下载它们:
http://sourceforge.net/project/showf...kage_id=212104

所有功能的简短描述:
>I wrote a few functions which IMHO are missing in python(s itertools).

You can download them here:
http://sourceforge.net/project/showf...kage_id=212104

A short description to all the functions:



只是几个问题:

Just a couple of questions:


iproduct(iterable,start = 0)-value

返回可迭代元素的乘积

乘以参数''start''的值。当

可迭代为空时,返回开始。
iproduct(iterable, start=0) -value
Returns the product of the elements of a iterable
times the value of parameter ''start''. When the
iterable is empty, returns start.



开始时不是一个更好的默认值吗?

Wouldn''t 1 be a better default value for start?


forall(谓词,可迭代,默认=真)-bo

当迭代中的所有元素x时,返回True

谓词(x)是真的。当iterable为空时,

返回默认值。


forany(谓词,可迭代,默认= False)-bool

返回是的,当迭代中的任何元素x时

谓词(x)为True。当iterable为空时,

返回默认值。
forall(predicate, iterable, default=True) -bool
Returns True, when for all elements x in iterable
predicate(x) is True. When the iterable is empty,
returns default.
forany(predicate, iterable, default=False) -bool
Returns True, when for any element x in iterable
predicate(x) is True. When the iterable is empty,
returns default.



这些与Python 2.5中的所有内容有何不同?


- Paul

How are these different from all and any in Python 2.5?

-- Paul


On Sun,2006年11月19日21:35:24 +0100,Mathias Panzenboeck写道:
On Sun, 19 Nov 2006 21:35:24 +0100, Mathias Panzenboeck wrote:

我在python(s itertools)中写了一些恕我直言的函数。


你可以在这里下载它们:
http://sourceforge.net/project/showf...kage_id = 212104


所有函数的简短描述:


icmp(iterable1,iterable2)-integer

如果iterable1< iterable2,

如果iterable1 = = iterable1则为零,
如果iterable1为iterable1,则b $ b为正。
I wrote a few functions which IMHO are missing in python(s itertools).

You can download them here:
http://sourceforge.net/project/showf...kage_id=212104

A short description to all the functions:

icmp(iterable1, iterable2) -integer
Return negative if iterable1 < iterable2,
zero if iterable1 == iterable1,
positive if iterable1 iterable1.



如果一个iterable小于另一个iterable,这意味着什么?那个

它有更少的物品?这两个迭代如何比较?


iter([1,2,无,foo,3 + 2j])


def():

而1:

收益率1


哪个更小?


What does it mean for an iterable to be less than another iterable? That
it has fewer items? How do these two iterables compare?

iter([1, 2, None, "foo", 3+2j])

def ones():
while 1:
yield 1

Which is smaller?


isum(iterable,start = 0)-value

返回可迭代元素的总和
加上参数''的值开始''。当

iterable为空时,返回start。
isum(iterable, start=0) -value
Returns the sum of the elements of a iterable
plus the value of parameter ''start''. When the
iterable is empty, returns start.



你的意思就像内置的sum()?


You mean just like the built-in sum()?


>> sum(xrange(12),1000)
>>sum(xrange(12), 1000)



1066

1066


iproduct(iterable,start = 0)-value

返回可迭代元素的乘积

乘以参数''start''的值。当

iterable为空时,返回start。
iproduct(iterable, start=0) -value
Returns the product of the elements of a iterable
times the value of parameter ''start''. When the
iterable is empty, returns start.



如果我记得,产品()是在sum()引入的同时被请求的,并且Guido拒绝了它作为a b $ b内置,因为它实际上只有

有用于计算几何平均值,如果你需要它很容易实现它:


def product(it,start = 1):

#默认值1比0更明智

#1是乘法身份

p =开始

for x:

p * = x

返回页面

If I recall, product() was requested about the same time that sum() was
introduced, and Guido rejected it as a built-in because it was really only
useful for calculating geometric means, and it is easy to do if you need
it:

def product(it, start=1):
# default value of 1 is more sensible than 0
# 1 is the multiplicative identity
p = start
for x in it:
p *= x
return p


forall(谓词,可迭代,默认=真)-bo

当迭代中的所有元素x时,返回True

谓词(x ) 是真的。当iterable为空时,

返回默认值。


forany(谓词,可迭代,默认= False)-bool

返回是的,当迭代中的任何元素x时

谓词(x)为True。当iterable为空时,

返回默认值。
forall(predicate, iterable, default=True) -bool
Returns True, when for all elements x in iterable
predicate(x) is True. When the iterable is empty,
returns default.
forany(predicate, iterable, default=False) -bool
Returns True, when for any element x in iterable
predicate(x) is True. When the iterable is empty,
returns default.



我模糊地回忆起所有()和任何()内置的计划 - 也许是对于Python

2.5?


I vaguely recall plans for all() and any() builtins -- perhaps for Python
2.5?


take(n,iterable)-iterator

返回迭代器$ it $ $
元素的迭代器
take(n,iterable) -iterator
returns a iterator over the first n
elements of the iterator



就像itertools.islice(iterable,n)。

Just like itertools.islice(iterable, n).


>> list(itertools.islice(xrange(10),5))
>>list(itertools.islice(xrange(10), 5))



[0,1,2,3,4]

[0, 1, 2, 3, 4]


drop(n,iterable)-iterable

掉落第一个n elemetns of iterable和

返回迭代器,其余
drop(n,iterable) -iterable
drops the first n elemetns of iterable and
return a iterator over the rest



就像itertools.islice(iterable,n,None)

Just like itertools.islice(iterable, n, None)


>> list(itertools.islice(xr) ange(20),15,None))
>>list(itertools.islice(xrange(20), 15, None))



[15,16,17,18,19]


(旁白:我认为如果使用关键字

参数,islice会更加清晰。)

[15, 16, 17, 18, 19]

(Aside: I think islice would be so much cleaner if it took keyword
arguments.)


heads (可迭代的) - 所有头上的用户

尾巴(可迭代) - 所有尾巴上的用户
heads(iterable) -iterator over all heads
tails(iterable) -iterator over all tails



你会用它们做什么?

What would you use these for?


fcain(功能,*功能)-function(...,***)

fcain(f1,f2, ...,fn)(* args,* kwargs)等于f1(f2(... fn(* args,* kwargs)))
fcain(funct,*functs) -function(...,***)
fcain(f1,f2,...,fn)(*args,*kwargs) equals f1(f2(...fn(*args,*kwargs)))



通常用语这是功能组合。

-

史蒂文。


The usual term for this is function composition.
--
Steven.




Paul McGuire写道:

Paul McGuire wrote:

" Mathias Panzenboeck" < e0 ****** @ student.tuwien.ac.atwrote in message

news:45 ******************** ***@tunews.univie.ac.at ...
"Mathias Panzenboeck" <e0******@student.tuwien.ac.atwrote in message
news:45***********************@tunews.univie.ac.at ...

我在python(s itertools)中写了一些恕我直言的函数。


你可以在这里下载它们:
http://sourceforge.net/project/showf...kage_id=212104

所有功能的简短描述:
I wrote a few functions which IMHO are missing in python(s itertools).

You can download them here:
http://sourceforge.net/project/showf...kage_id=212104

A short description to all the functions:



只是几个问题:

Just a couple of questions:


iproduct(iterable,start = 0)-value

返回可迭代元素的乘积

乘以参数''start''的值。当

iterable为空时,返回start。
iproduct(iterable, start=0) -value
Returns the product of the elements of a iterable
times the value of parameter ''start''. When the
iterable is empty, returns start.



开始时不是一个更好的默认值吗?

Wouldn''t 1 be a better default value for start?



我同意; start应该默认为1.

I concur; start should default to 1.


forall(谓词,可迭代,默认=真)-bool

当迭代中的所有元素x时,返回True

谓词(x)为True。当iterable为空时,

返回默认值。


forany(谓词,可迭代,默认= False)-bool

返回是的,当迭代中的任何元素x时

谓词(x)为True。当iterable为空时,

返回默认值。
forall(predicate, iterable, default=True) -bool
Returns True, when for all elements x in iterable
predicate(x) is True. When the iterable is empty,
returns default.

forany(predicate, iterable, default=False) -bool
Returns True, when for any element x in iterable
predicate(x) is True. When the iterable is empty,
returns default.



这些与Python 2.5中的所有内容有何不同?

How are these different from all and any in Python 2.5?



1.这些函数对项目应用谓词。使用any / all和genexp很简单

,但是通过相同的论证,它很简单

足以做imap和ifilter with一个普通的genexp。

2.它们有默认值。任何和所有人的默认值都不会产生感觉,而且我认为它们在这里也没有意义。一切都没有

永远是真的;任何一切都是假的。

Carl Banks

1. These functions apply a predicate to the items. It''s simple enough
to do with any/all and a genexp, but by the same argument, it''s simple
enough to do imap and ifilter with a plain genexp.
2. They have default values. Default values for any and all don''t make
sense, and I don''t think they make sense here, either. All of nothing
is always True; any of nothing is always False.
Carl Banks


这篇关于itertools的一些扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆