Python &引用;如果……或……”;列表理解中的语句?
在Python 3.4.2中,是否有一种方法可以在列表中使用“if…or…”语句 例如:Python &引用;如果……或……”;列表理解中的语句?,python,python-3.x,list-comprehension,Python,Python 3.x,List Comprehension,在Python 3.4.2中,是否有一种方法可以在列表中使用“if…or…”语句 例如: filtered_list = [x for x in other_list if not ' ' in x or ' ' in other_list[int(other_list.index(x) -1)]] #^item just before "x" in "
filtered_list = [x for x in other_list
if not ' ' in x or ' ' in other_list[int(other_list.index(x) -1)]]
#^item just before "x" in "other_list"
我知道这个命令不起作用。我知道这一个也没有:
filtered_list = [x for x in other_list
if not (' ' in x) or (' ' in other_list[int(other_list.index(x) -1)])]
#^item just before "x" in "other_list"
有办法做到这一点吗?“for”循环可能更容易阅读,但我正试图推动我对列表理解的理解。提前谢谢
(奇怪的是,我在stackoverflow上找不到像这样的问题。)您的代码确实执行了您告诉它的操作(在简单的情况下): i、 e,它选择所有不包含四个空格的项目,或者在包含四个空格的项目后面 它在很多方面都是非常奇怪的代码——例如,为什么要使用
.index
,在重复的情况下,它会选择第一个项目,该项目等于x
;而且,是否应将第一项视为“遵循”本规范所述的最后一项?但是,如果没有用英语明确解释您的代码应该完成什么,只是按照编写的代码进行“反向工程”,它似乎确实在简单的情况下做到了它所说的(第一项不包含四个空格,没有重复项),尽管效率很低(O(N平方)使用枚举
)使其成为O(N)非常简单
因此,请提供一个清晰、详尽的解释,说明你认为你在这里做什么,我很乐意编辑这个答案,告诉你如何做到你真正的意思,而不是你(显然是错误的)编码的内容。但是,我看不懂你的心思,所以。。。(顺便说一句,这确实需要是一个“答案”,因为格式化上述代码对其可读性至关重要,所以它不能是“注释”)
补充:正常的方法“在其他列表中的当前项之前获取该项”
,当然不会使用.index
(这是O(N)
,因此使整个列表理解O(N平方)
),而是枚举。什么是“前一项”第一项需要解释,但正常阅读是“没有这样的东西”
因此,假设您有两个函数curp
(谓词限定当前项,如果truthy在上面)和prevp
(谓词限定当前项,如果truthy在前面的项上,如果有)。然后,从列表xs
中选择项目的正常方式显然是:
[x for i, x in enumerate(xs) if curp(x) or i>0 and prevp(xs[i-1])]
当然,无论谓词是优雅地封装到函数中(如上所述),还是更粗略地在线表达,其工作方式都完全相同。因此,如果,例如:
def curp(x): return 'AB' not in x
def prevp(xp): return 'CD' in xp
那么下面的listcomp与前面的listcomp相同,只是可读性较差:
[x for i, x in enumerate(xs) if 'AB' not in X or i>0 and 'CD' in xs[i-1]]
括号在这里是相当多余的,而不是X中的“AB”
这是一种更不优雅、可读性更强、毫不含糊的方式来表达而不是X中的“AB”
——但是,请忽略这些风格观点,它们只来自于15年的Python经验,毕竟:-)您可以使用多个和s/或s,但如果,则只能使用一个。例如,以下内容:
In [7]: a = [1,2,3,4,5,6,7,8,9,10]
In [8]: b = [x for x in a if x % 2 == 0 or x % 3 == 0]
In [9]: b
Out[9]: [2, 3, 4, 6, 8, 9, 10]
工作正常。我希望这能解决您的问题:
other_list = ['foo', 'bar ', 'baz ']
filtered_list = [x for x in other_list if (' ' not in x) or (' ' not in other_list[int(other_list.index(x) -1)])]
print filtered_list
注意:not的位置决定命令的解释方式。我以为你想要这样的东西。是的,我刚刚意识到我的例子写错了。我想切换if和for语句:
filtered_list = [x
if not ' ' in x or ' ' in other_list[int(other_list.index(x) -1)]
for x in other_list]
我将发布我的整个列表,以说明我为什么要按此顺序排列。基本上,我有一个带有缩进项的列表。对于每一个缩进的项目,我想将其缩进加倍,并缩进它前面的项目,同时保持列表中的其余项目不变
indented_list = [t
if not ' ' in t or ' ' in stripped_list[int(stripped_list.index(t) + 1)]
else ''.join((' ', t))
if not t[0].isdigit()
else re.sub(r'^(\d)(.*)', r'\1\2 \3',
stripped_list[int(stripped_list.index(t))])
if not t[1].isdigit()
else re.sub(r'^(\d)(\d)(.*)', r'\1\2 \3',
stripped_list[int(stripped_list.index(t))])
for t in stripped_list]
我知道可能有更简单的方法。几周前我才开始学习编程。很好!我从来没有想到过!我认为这可能只是一个小的语法错误,比如“not的位置决定了命令的解释方式”:not它不<代码>非x或y中的a
始终与非x或y中的a
和(a不在x中)或y
相同。只有非常具体的括号,如a not in(x或y)
会改变这一点。我想你是说我应该在我的问题中添加答案,而不是在你的答案中添加注释,所以我肯定会这样做。嘿,我甚至没有想到使用enumerate!在信用证中,顺序是严格的:首先是
for
条款(强制性),然后是
if
条款(可选)。而且,没有if/else
子句(可以使用if/else
表达式,但需要在if
关键字前面加一个值)。如果您“几周前刚开始学习编程”,那么现在最好避免列表理解,并坚持使用for
循环、If
语句(如果需要,可以使用else
子句!-)和.append
调用来建立新列表。太棒了,谢谢!另外,你的答案中的“或i>0和”也是我想知道的另一个问题。谢谢你的帮助!总是很高兴能帮上忙!
indented_list = [t
if not ' ' in t or ' ' in stripped_list[int(stripped_list.index(t) + 1)]
else ''.join((' ', t))
if not t[0].isdigit()
else re.sub(r'^(\d)(.*)', r'\1\2 \3',
stripped_list[int(stripped_list.index(t))])
if not t[1].isdigit()
else re.sub(r'^(\d)(\d)(.*)', r'\1\2 \3',
stripped_list[int(stripped_list.index(t))])
for t in stripped_list]