关于列表的查询 [英] A query about list

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

问题描述

大家好,


我对python很新。我在python中查询了

列表。


假设我有一个列表

a = [1,[2,3 ,4],5,6,7,[8,9,10],11,12]

我想知道是否有任何简单的python

设施可用这将扩大上面的列表

给予

a = [1,2,3,4,5,6,7,8,9,10,11,12]


我知道我可以用type()和for / while循环来实现,

但是有没有更简单的方法?


问候,

Santanu

Hello everybody,

I am very new to python. I have a query about
list in python.

Suppose I have a list
a = [1,[2,3,4],5,6,7,[8,9,10],11,12]
I want to know if there is any simple python
facility available that would expand the above list
to give
a = [1,2,3,4,5,6,7,8,9,10,11,12]

I know I can do that with a type() and a for/while loop,
but is there any simpler way?

Regards,
Santanu

推荐答案

文章< pa ****** **********************@softhome.net> ;, Santanu Chatterjee写道:
In article <pa****************************@softhome.net>, Santanu Chatterjee wrote:
我对python很新。我在python中查询了
列表。

假设我有一个列表
a = [1,[2,3,4],5,6,7,[ 8,9,10],11,12]
我想知道是否有任何简单的python
设施可以扩展上面的列表

a = [ 1,2,3,4,5,6,7,8,9,10,11,12]

我知道我可以用type()和for / while循环来做到这一点,
但是有没有更简单的方法?
I am very new to python. I have a query about
list in python.

Suppose I have a list
a = [1,[2,3,4],5,6,7,[8,9,10],11,12]
I want to know if there is any simple python
facility available that would expand the above list
to give
a = [1,2,3,4,5,6,7,8,9,10,11,12]

I know I can do that with a type() and a for/while loop,
but is there any simpler way?




通常情况下,我建议使用reduce(operator.add,...)。压扁一个清单,但是因为你有一些赤裸裸的条目,那不行......


我不知道你是否认为这个更简单,但你可以定义一个减少

的功能检查第二个参数的类型,并使用它如下:



Normally, I''d suggest "reduce(operator.add, ...)" to flatten a list, but
since you''ve got some "naked" entries, that won''t work...

I don''t know if you consider this simpler, but you could define a reduction
function that checks the type of the second argument, and use it like this:

def merge(x,y) :
....如果type(y)是type([]):return x + y

.... return x + [y]

.... reduce(merge,a,[])
def merge(x, y): .... if type(y) is type([]): return x + y
.... return x + [y]
.... reduce(merge, a, [])



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


戴夫


-

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

:drinklifeoutofthec ontainer:


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

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 :


Santanu Chatterjee写道:
Santanu Chatterjee wrote:
Hello eve rybody,

我是python的新手。我在python中查询了
列表。

假设我有一个列表
a = [1,[2,3,4],5,6,7,[ 8,9,10],11,12]
我想知道是否有任何简单的python
设施可以扩展上面的列表

a = [ 1,2,3,4,5,6,7,8,9,10,11,12]

我知道我可以用type()和for / while循环来做到这一点,
但是有没有更简单的方法?
Hello everybody,

I am very new to python. I have a query about
list in python.

Suppose I have a list
a = [1,[2,3,4],5,6,7,[8,9,10],11,12]
I want to know if there is any simple python
facility available that would expand the above list
to give
a = [1,2,3,4,5,6,7,8,9,10,11,12]

I know I can do that with a type() and a for/while loop,
but is there any simpler way?




为什么你首先有这样的数据结构?难道不是吗?b
$ b b b b b b b b b b b b b b b b b b b b b b b b b b b b b >
然后你可以扩展()它。


- 格哈德



Why do you have such a data structure in the first place? Can''t it be
avoided?

If for example you made the mistake of .append()-ing lists to a list,
then you can .extend() it instead.

-- Gerhard


开星期四,2003年10月9日20:05:08 +0000,Dave Benjamin写道:
On Thu, 09 Oct 2003 20:05:08 +0000, Dave Benjamin wrote:
假设我有一个清单
a = [1 ,[2,3,4],5,6,7,[8,9,10],11,12]
我想知道是否有任何简单的python工具可以扩展以上列表给出了
a = [1,2,3,4,5,6,7,8,9,10,11,12]

我知道我能做到这一点使用type()和for / while循环,但有没有更简单的方法?
Suppose I have a list
a = [1,[2,3,4],5,6,7,[8,9,10],11,12]
I want to know if there is any simple python facility available that
would expand the above list to give
a = [1,2,3,4,5,6,7,8,9,10,11,12]

I know I can do that with a type() and a for/while loop, but is there
any simpler way?
我不知道你是否认为这个更简单,但你可以定义一个
缩减函数来检查第二个参数的类型,并使用它
像这样:
def merge(x,y):...如果type(y)是type([]):return x + y
... return x + [y]
... reduce(merge,a,[])
I don''t know if you consider this simpler, but you could define a
reduction function that checks the type of the second argument, and use it
like this:
def merge(x, y): ... if type(y) is type([]): return x + y
... return x + [y]
... reduce(merge, a, [])


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


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




感谢您的快速回复。是的,这类似于我将要做的事情

使用for循环。


但是更简单,我真正的意思是操纵

列表''a''本身没有创建新的列表对象。

我读到创建新对象非常耗时(而且它是

更好地使用填充列表然后''join''来创建一个字符串,而不是使用+ =来增加一个字符串
。对不起,我以前忘记提了。


我正在尝试类似a.insert(1,a.pop(1))的东西,但我需要

以某种方式修改a.pop(1)以便括号消失......你知道

我的意思。这可能吗?


问候,

Santanu



Thanks for the quick reply. Yes, this is similar to what I would do
using for loop.

But by ''simpler'' what I really meant was that manipulating
the list ''a'' itself without creating a new list object.
I read that creating new objects is time consuming (and that it is
better to use fill a list and then ''join'' to create a string, than to
increase a string using += ). Sorry I forgot to mention it before.

I was trying something like a.insert(1,a.pop(1)) but I need to
modify a.pop(1) somehow so that the brackets vanish ...you know
what I mean. Is that possible ?

Regards,
Santanu


这篇关于关于列表的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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