分发清单 [英] disgrating a list
问题描述
您好,
我如何能够(可能不是一个好词)列出一个列表?对于
例如:
a = [1,2]
b = 3
c = [a ] + [b]#使得[[1,2],3]
然后如何将c([[1,2],3])改为[1, 2,3]?我有一个简单的
功能:
========================
def flatten(l):
r = []
s = [l]
而len(s)0:
i = s.pop()
if i .__ class__ == list:
for c in i:
s.append(c)
else:
r.append(i)
返回r
= =======================
但是这个函数实际上并没有在python-way中进行。难道不是
python是一个不起眼的功能还是构建它?
Greetz,
Noud Aldenhoven
Hello,
How can I disgrate (probably not a good word for it) a list? For
example:
a = [1,2]
b = 3
c = [a] + [b] # which makes [[1,2],3]
Then how can I change c ([[1,2],3]) into [1,2,3]? I have a simple
function for this:
========================
def flatten(l):
r = []
s = [l]
while len(s) 0:
i = s.pop()
if i.__class__ == list:
for c in i:
s.append(c)
else:
r.append(i)
return r
========================
But this function isn''t really doing it in the "python-way". Doesn''t
have python an obscure function or buildin to do this?
Greetz,
Noud Aldenhoven
推荐答案
jwaixs schrieb:
jwaixs schrieb:
你好,
我如何能够列出一份清单(可能不是一个好词)?对于
例如:
a = [1,2]
b = 3
c = [a ] + [b]#使[[1,2],3]
Hello,
How can I disgrate (probably not a good word for it) a list? For
example:
a = [1,2]
b = 3
c = [a] + [b] # which makes [[1,2],3]
为什么要在列表中包装?只需
c = a + [b]
就可以了。
Diez
Why do you wrap a in a list? Just
c = a + [b]
will do it.
Diez
Diez B. Roggisch写道:
Diez B. Roggisch wrote:
为什么要包装一个清单?只需
c = a + [b]
就可以了。
Why do you wrap a in a list? Just
c = a + [b]
will do it.
是的我知道,但问题是我不知道''a'是否是一个清单。我b $ b可以进行类型检查,但我不想以这种方式编程。
Noud
Yes I know, but the problem is I don''t know if ''a'' is a list or not. I
could make a type check, but I don''t want to program in that way.
Noud
jwaixs写道:
jwaixs wrote:
你好,
我怎么能这样做(可能不是一个好词(一个列表)?对于
例如:
a = [1,2]
b = 3
c = [a ] + [b]#使得[[1,2],3]
然后如何将c([[1,2],3])改为[1, 2,3]?我有一个简单的
功能:
========================
def flatten(l):
r = []
s = [l]
而len(s)0:
i = s.pop()
if i .__ class__ == list:
for c in i:
s.append(c)
else:
r.append(i)
返回r
= =======================
但是这个函数实际上并没有在python-way中进行。难道没有
python是一个不起眼的功能还是构建这样做?
Hello,
How can I disgrate (probably not a good word for it) a list? For
example:
a = [1,2]
b = 3
c = [a] + [b] # which makes [[1,2],3]
Then how can I change c ([[1,2],3]) into [1,2,3]? I have a simple
function for this:
========================
def flatten(l):
r = []
s = [l]
while len(s) 0:
i = s.pop()
if i.__class__ == list:
for c in i:
s.append(c)
else:
r.append(i)
return r
========================
But this function isn''t really doing it in the "python-way". Doesn''t
have python an obscure function or buildin to do this?
不,它不是,但它很容易自己滚动,例如使用
递归生成器;事实上,如果你搜索过flatten在clpy的
google组中,您会发现: http ://tinyurl.com/ndobk
乔治
No, it doesn''t, but it''s easy to roll one on your own, e.g. using a
recursive generator; in fact, if you had searched for "flatten" in the
google group of c.l.py, you''d find this: http://tinyurl.com/ndobk
George
这篇关于分发清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!