有没有更好的办法? [英] is there a better way?
问题描述
问题:
你有一个未知长度的列表,例如:list =
[X,X,X,O,O, O,O]。你想要提取全部而且只提取X'。你知道
X'都在前面,你知道最后一个X之后的项是
一个O,或者列表以X结尾。
X'之间永远都没有。
我一直在使用这样的东西:
_____________________
而list [0]!= O:
storage.append(list [0])
list.pop( 0)
如果len(列表)== 0:
休息
_____________________
但是这对我来说似乎很难看,而且使用while和给我这些heebies。
有更好的方法吗?
希望这很清楚。
谢谢
ma ******* @ gmail.com 写道:
你有一个未知长度的列表,例如:list =
[X,X,X,O,O,O,O]。你想要提取全部而且只提取X'。你知道吗?X'都在前面,你知道最后一个X之后的项目是O,或者列表以X结尾。从来没有O'
X'的。
什么不是
for x in list:
如果x == O:
休息
storage.append(x)
??
< br $> b $ b -
Jeremy Sanders
http://www.jeremysanders.net/
ma ******* @ gmail.com 写道:问题:
你有一个未知长度的列表,例如这个:list =
[X,X,X,O,O,O,O]。你想要提取全部而且只提取X'。你知道吗?X'都在前面,你知道最后一个X之后的项目是O,或者列表以X结尾。从来没有O'
X'的。
我一直在使用这样的东西:
_____________________
而list [0]!= O:
storage.append(list [0])
list.pop(0)
如果len(list)== 0:
中断
_____________________
>但这对我来说似乎很难看,而且使用while给我这些heebies。是否有更好的方法?
希望这很清楚。
谢谢
有几种方法可以做到这一点,真的取决于:
mylist = [1,2,3,4,5,6,0,0,0]
list理解(将获得所有非零,并删除所有零,
但与您的函数不同):
[如果x!= 0,则为x in mylist中的x]
列表切片(与你的功能相同):
mylist [:mylist.index(0)]
如果您的列表如下所示,则取决于您想要发生的事情:
[1,2,3,0,4,5,6,0,0]
[ 0,1,2,3,4,5,6]
[0,1,2,3,4,5,6,0]
[1, 2,3,4,5,6]
< ma ******* @ gmail.com>在消息中写道
news:11 ******************** @ g47g2000cwa.googlegrou ps.com ...问题:
你有一个未知长度的列表,例如:list =
[X,X,X,O,O,O,O]。你想要提取全部而且只提取X'。你知道吗?X'都在前面,你知道最后一个X之后的项目是O,或者列表以X结尾。从来没有O'
X'的。
我一直在使用这样的东西:
_____________________
而list [0]!= O:
storage.append(list [0])
list.pop(0)
如果len(list)== 0:
中断
_____________________
>但这对我来说似乎很难看,而且使用while给我这些heebies。是否有更好的方法?
希望这很清楚。
谢谢
使用itertools。
import itertools
lst =" X,X,X,O,O,O,O,O,X,X ,X,X,O,X" .split(",")
[z for itertools.takewhile(lambda x:x ==" X",lst)]
[''X'',''X'',''X'']
- Paul
Problem:
You have a list of unknown length, such as this: list =
[X,X,X,O,O,O,O]. You want to extract all and only the X''s. You know
the X''s are all up front and you know that the item after the last X is
an O, or that the list ends with an X. There are never O''s between
X''s.
I have been using something like this:
_____________________
while list[0] != O:
storage.append(list[0])
list.pop(0)
if len(list) == 0:
break
_____________________
But this seems ugly to me, and using "while" give me the heebies. Is
there a better approach?
hope this is clear.
thanks
ma*******@gmail.com wrote:
You have a list of unknown length, such as this: list =
[X,X,X,O,O,O,O]. You want to extract all and only the X''s. You know
the X''s are all up front and you know that the item after the last X is
an O, or that the list ends with an X. There are never O''s between
X''s.
What not
for x in list:
if x == O:
break
storage.append(x)
??
--
Jeremy Sanders
http://www.jeremysanders.net/
ma*******@gmail.com wrote:Problem:
You have a list of unknown length, such as this: list =
[X,X,X,O,O,O,O]. You want to extract all and only the X''s. You know
the X''s are all up front and you know that the item after the last X is
an O, or that the list ends with an X. There are never O''s between
X''s.
I have been using something like this:
_____________________
while list[0] != O:
storage.append(list[0])
list.pop(0)
if len(list) == 0:
break
_____________________
But this seems ugly to me, and using "while" give me the heebies. Is
there a better approach?
hope this is clear.
thanks
There''s a few ways to do this, really depends on :
mylist = [1,2,3,4,5,6,0,0,0]
list comprehension (will get ALL non zeros, and strip out all zeros,
but is different from your function):
[x for x in mylist if x != 0]
list slice(same as your function):
mylist[:mylist.index(0)]
Depends what you want to happen if your list is something like:
[1,2,3,0,4,5,6,0,0]
[0,1,2,3,4,5,6]
[0,1,2,3,4,5,6,0]
[1,2,3,4,5,6]
<ma*******@gmail.com> wrote in message
news:11********************@g47g2000cwa.googlegrou ps.com...Problem:
You have a list of unknown length, such as this: list =
[X,X,X,O,O,O,O]. You want to extract all and only the X''s. You know
the X''s are all up front and you know that the item after the last X is
an O, or that the list ends with an X. There are never O''s between
X''s.
I have been using something like this:
_____________________
while list[0] != O:
storage.append(list[0])
list.pop(0)
if len(list) == 0:
break
_____________________
But this seems ugly to me, and using "while" give me the heebies. Is
there a better approach?
hope this is clear.
thanks
Use itertools.
import itertools
lst = "X,X,X,O,O,O,O,O,X,X,X,X,O,X".split(",")
[z for z in itertools.takewhile(lambda x:x=="X",lst)]
[''X'', ''X'', ''X'']
-- Paul
这篇关于有没有更好的办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!