是否有更简单的方法来表达此列表切片? [英] Is there an easier way to express this list slicing?

查看:97
本文介绍了是否有更简单的方法来表达此列表切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个说明,10个元素的列表,我需要将它切成

不规则大小列表,我将不得不创建一堆临时的

变量然后重新组合它们,比如:


#只是为了说明。 Alist可以是任何现有的10个元素列表

a_list =("",)* 10

(a,b,c1,c2,c3,d1,d2, d3,d4,d5)= a_list

alist =(a,)

blist =(b,)

clist =(c1,c2 ,c3)

dlist =(d2,d3,d4,d5)


这显然有效,但我真的必须这样做吗?


BTW:我知道你可以这样做:

alist = a_list [0]

blist = a_list [1]

clist = a_list [2:5]

dlist = a_list [5:]


但是我没看到它''更好。


我可以说一下效果:


(a,b,c [0:2],d [0:5])= a_list#显然这不会工作


??


我问的是因为我有一个代码部分包含* lot *

这样的事情。它使代码变得非常难以理解。


谢谢,

If I have a list of say, 10 elements and I need to slice it into
irregular size list, I would have to create a bunch of temporary
variables and then regroup them afterwords, like:

# Just for illustration. Alist can be any existing 10 element list
a_list=("",)*10
(a,b,c1,c2,c3,d1,d2,d3,d4,d5)=a_list
alist=(a,)
blist=(b,)
clist=(c1,c2,c3)
dlist=(d2,d3,d4,d5)

That obviously work but do I *really* have to do that?

BTW: I know you can do:
alist=a_list[0]
blist=a_list[1]
clist=a_list[2:5]
dlist=a_list[5:]

but I don''t see that it''s any better.

Can I say something to the effect of:

(a,b,c[0:2],d[0:5])=a_list # Obviously this won''t work

??

I am asking this because I have a section of code that contains *lots*
of things like this. It makes the code very unreadable.

Thanks,

推荐答案

John Henry schrieb:
John Henry schrieb:

如果我有一个说明,10个元素的列表,我需要将它切成

不规则尺寸列表,我将不得不创建一堆临时的

变量然后重新组合它们,比如:


#只是为了说明。 Alist可以是任何现有的10个元素列表

a_list =("",)* 10

(a,b,c1,c2,c3,d1,d2, d3,d4,d5)= a_list

alist =(a,)

blist =(b,)

clist =(c1,c2 ,c3)

dlist =(d2,d3,d4,d5)


这显然有效,但我真的必须这样做吗?


BTW:我知道你可以这样做:

alist = a_list [0]

blist = a_list [1]

clist = a_list [2:5]

dlist = a_list [5:]


但是我没看到它''更好。


我可以说一下效果:


(a,b,c [0:2],d [0:5])= a_list#显然这不会工作


??


我问的是因为我有一个代码部分包含* lot *

这样的事情。它使代码变得非常难以理解。


谢谢,
If I have a list of say, 10 elements and I need to slice it into
irregular size list, I would have to create a bunch of temporary
variables and then regroup them afterwords, like:

# Just for illustration. Alist can be any existing 10 element list
a_list=("",)*10
(a,b,c1,c2,c3,d1,d2,d3,d4,d5)=a_list
alist=(a,)
blist=(b,)
clist=(c1,c2,c3)
dlist=(d2,d3,d4,d5)

That obviously work but do I *really* have to do that?

BTW: I know you can do:
alist=a_list[0]
blist=a_list[1]
clist=a_list[2:5]
dlist=a_list[5:]

but I don''t see that it''s any better.

Can I say something to the effect of:

(a,b,c[0:2],d[0:5])=a_list # Obviously this won''t work

??

I am asking this because I have a section of code that contains *lots*
of things like this. It makes the code very unreadable.

Thanks,



您的代码中没有任何内容__is__列表。它们都是元组...

列表是:

aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]


托马斯

Nothing in your code actually __is__ a list. they are all tuples...
A list is:
aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]

Thomas


好吧,我好吧。


下一页。


Thomas Ploch写道:
Well, pardoon me.

Next.

Thomas Ploch wrote:

John Henry schrieb:
John Henry schrieb:

如果我有一个说明,10个元素的列表,我需要将它切成

不规则大小列表,我将不得不创建一堆临时的

变量然后重新组合它们后记,如:


#只是为了说明。 Alist可以是任何现有的10个元素列表

a_list =("",)* 10

(a,b,c1,c2,c3,d1,d2, d3,d4,d5)= a_list

alist =(a,)

blist =(b,)

clist =(c1,c2 ,c3)

dlist =(d2,d3,d4,d5)


这显然有效,但我真的必须这样做吗?


BTW:我知道你可以这样做:

alist = a_list [0]

blist = a_list [1]

clist = a_list [2:5]

dlist = a_list [5:]


但是我没看到它''更好。


我可以说一下效果:


(a,b,c [0:2],d [0:5])= a_list#显然这不会工作


??


我问的是因为我有一个代码部分包含* lot *

这样的事情。它使代码变得非常难以理解。


谢谢,
If I have a list of say, 10 elements and I need to slice it into
irregular size list, I would have to create a bunch of temporary
variables and then regroup them afterwords, like:

# Just for illustration. Alist can be any existing 10 element list
a_list=("",)*10
(a,b,c1,c2,c3,d1,d2,d3,d4,d5)=a_list
alist=(a,)
blist=(b,)
clist=(c1,c2,c3)
dlist=(d2,d3,d4,d5)

That obviously work but do I *really* have to do that?

BTW: I know you can do:
alist=a_list[0]
blist=a_list[1]
clist=a_list[2:5]
dlist=a_list[5:]

but I don''t see that it''s any better.

Can I say something to the effect of:

(a,b,c[0:2],d[0:5])=a_list # Obviously this won''t work

??

I am asking this because I have a section of code that contains *lots*
of things like this. It makes the code very unreadable.

Thanks,



你的代码中没有任何内容__is__列表。它们都是元组...

列表是:

aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]


Thomas


Nothing in your code actually __is__ a list. they are all tuples...
A list is:
aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]

Thomas


2006年11月30日,Thomas Ploch< Th ****** **** @ gmx.net写道:
On 11/30/06, Thomas Ploch <Th**********@gmx.netwrote:

John Henry schrieb:
John Henry schrieb:

如果我有一个清单比方说,10个元素我需要将它切成

不规则大小列表,我必须创建一堆临时的

变量,然后重新组合它们,如:


#只是为了说明。 Alist可以是任何现有的10个元素列表

a_list =("",)* 10

(a,b,c1,c2,c3,d1,d2, d3,d4,d5)= a_list

alist =(a,)

blist =(b,)

clist =(c1,c2 ,c3)

dlist =(d2,d3,d4,d5)


这显然有效,但我真的必须这样做吗?


BTW:我知道你可以这样做:

alist = a_list [0]

blist = a_list [1]

clist = a_list [2:5]

dlist = a_list [5:]


但是我没看到它''更好。


我可以说一下效果:


(a,b,c [0:2],d [0:5])= a_list#显然这不会工作


??


我问的是因为我有一个代码部分包含* lot *

这样的事情。它使代码变得非常难以理解。


谢谢,
If I have a list of say, 10 elements and I need to slice it into
irregular size list, I would have to create a bunch of temporary
variables and then regroup them afterwords, like:

# Just for illustration. Alist can be any existing 10 element list
a_list=("",)*10
(a,b,c1,c2,c3,d1,d2,d3,d4,d5)=a_list
alist=(a,)
blist=(b,)
clist=(c1,c2,c3)
dlist=(d2,d3,d4,d5)

That obviously work but do I *really* have to do that?

BTW: I know you can do:
alist=a_list[0]
blist=a_list[1]
clist=a_list[2:5]
dlist=a_list[5:]

but I don''t see that it''s any better.

Can I say something to the effect of:

(a,b,c[0:2],d[0:5])=a_list # Obviously this won''t work

??

I am asking this because I have a section of code that contains *lots*
of things like this. It makes the code very unreadable.

Thanks,



你的代码中没有任何内容__is__列表。它们都是元组...

列表是:

aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]


Nothing in your code actually __is__ a list. they are all tuples...
A list is:
aList = [a,b,c1,c2,c3,d1,d2,d3,d4,d5]



真但不相关,真的,他应该说序列。但更重要的是,

你不能用alist,blist,clist,dlist来表明你做了什么。没有

知道最终结果是什么,没有人能够帮助你

消除中间步骤。

True but not relevant, really, he should have said "sequence". But
more importantly,
you don''t show what you do with alist, blist,clist,dlist. Without
knowing what the end result is, nobody is going to be able to help you
eliminate the middle steps.


Thomas

-
http://mail.python.org/mailman/listinfo/python-list


这篇关于是否有更简单的方法来表达此列表切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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