花式切片的乐趣 [英] Fun with fancy slicing

查看:68
本文介绍了花式切片的乐趣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,


我刚刚意识到你可以很容易地实现一个序列分组功能

使用Python 2.3的花式切片支持:


def组(值,大小):

返回映射(无,* [值[i :: size] for i in range(size)])< br>

group(range(20),4)
[(0,1,2,3) ,(4,5,6,7),(8,9,10,11),(12,13,14,15),

(16,17,18,19)]

group(范围(14),3)



[(0,1,2),(3,4, 5),(6,7,8),(9,10,11),(12,13,无)]


我必须使用地图(无,* .. 。)而不是拉链(* ...)来转换结果

因为拉开了孤儿而在末尾。无论如何,这是一个有用的

函数,如果你需要进行分页或

多列显示,还有其他东西......


任何人都有任何其他有趣的扩展切片语法应用程序?


和平,

Dave

-

..:[dave benjamin(ramenboy) - : - www.ramenfest.com - : - www.3dex.com ] :.

:drinklifeoutofthec ontainer:

解决方案

Dave Benjamin< ra *** @ lackingtalent.com>写道:

任何人都有任何其他有趣的扩展切片语法应用程序?




你可以在筛子素数中使用它发现者(哦,穿越:-):


/>> def筛(p):

| ..候选人=范围(p)

| ..我在候选人中[2:]:

| ..如果不是候选人[i]:继续

| .. n = len(候选人[2 * i :: i])

| ..候选人[ 2 * i :: i] = [0] * n

| ..返回过滤器(无,候选人[2:])

\__

- >>筛子(50)

[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]

但它不是特别有效(速度或内存)。非常可爱,但是
虽然。


干杯,

mwh


-

一个典型的luser可以执行真正超人的力量和灵巧性,以避免阅读文档。 - Lionel,asr


在文章< 7h ************* @ pc150.maths.bris.ac.uk>中, Michael Hudson写道:

Dave Benjamin< ra *** @ lackingtalent.com>写道:

任何人都有任何其他有趣的扩展切片语法应用程序?



你可以在筛子找到者中使用它(哦,穿越:-):
...
但它不是特别有效(速度或记忆)。相当可爱,虽然。



但非常富有表现力!我很惊讶这段代码有多小。它让我想起了Haskell中商标quicksort实现的
,即。实际上它可能不是很好,但是你可以看到算法处于高水平,这使得b / b $ b更容易弄清楚发生了什么。 />

无论如何,谢谢。那很酷。 =)


和平,

Dave


-

..: [dave benjamin(ramenboy) - : - www.ramenfest.com - : - www.3dex.com ]:

:drinklifeoutofthec ontainer:


Dave Benjamin在Haskell中写道:

...

商标quicksort实现,即。它在实践中可能不是很好,但你可以看到高水平的算法,这样可以更容易地弄清楚发生了什么。




嗯,你的意思是......:


def quicksort(alist):

head = alist [0]

返回quicksort([x in alist [1:]如果x< = head])+ [

head] + quicksort([x in alist [1:]如果x>头部))


....?

Alex


Hey all,

I just realized you can very easily implement a sequence grouping function
using Python 2.3''s fancy slicing support:

def group(values, size):
return map(None, *[values[i::size] for i in range(size)])

group(range(20), 4) [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15),
(16, 17, 18, 19)]
group(range(14), 3)


[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, None)]

I had to use map(None, *...) instead of zip(*...) to transpose the result
because zip throws away the "orphans" at the end. Anyway, this is a useful
function to have in your toolkit if you need to do pagination or
multi-column display, among other things...

Anyone have any other interesting applications of the extended slice syntax?

Peace,
Dave

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

解决方案

Dave Benjamin <ra***@lackingtalent.com> writes:

Anyone have any other interesting applications of the extended slice syntax?



You can use it in a sieve prime finder (ooh, thread crossing :-):

/>> def sieve(p):
|.. candidates = range(p)
|.. for i in candidates[2:]:
|.. if not candidates[i]: continue
|.. n = len(candidates[2*i::i])
|.. candidates[2*i::i] = [0]*n
|.. return filter(None, candidates[2:])
\__
->> sieve(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

but it''s not especially efficient (in speed or memory). Quite cute,
though.

Cheers,
mwh

--
A typical luser can perform truly superhuman feats of strength &
dexterity to avoid reading documentation. -- Lionel, asr


In article <7h*************@pc150.maths.bris.ac.uk>, Michael Hudson wrote:

Dave Benjamin <ra***@lackingtalent.com> writes:

Anyone have any other interesting applications of the extended slice syntax?



You can use it in a sieve prime finder (ooh, thread crossing :-):
...
but it''s not especially efficient (in speed or memory). Quite cute,
though.



But quite expressive! I''m amazed at how small that code was. It reminds me
of the trademark quicksort implementation in Haskell, ie. it may not be
great in practice, but you can see the algorithm at a high level which makes
it easier to figure out what''s going on.

Anyway, thanks. That was cool. =)

Peace,
Dave

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :


Dave Benjamin wrote:
...

of the trademark quicksort implementation in Haskell, ie. it may not be
great in practice, but you can see the algorithm at a high level which
makes it easier to figure out what''s going on.



Hmmm, you mean as in...:

def quicksort(alist):
head = alist[0]
return quicksort([ x in alist[1:] if x<=head ]) + [
head ] + quicksort([ x in alist[1:] if x>head ])

....?
Alex


这篇关于花式切片的乐趣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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