- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
从以下列表理解开始:
new_list = [re.sub(r'replace_a', 'with__b', i)
if not re.findall(r'find_pattern', i, re.M) else
re.sub(r'replace_c', r'with__d', i)
for i in old_list]
现在我想在 else
之后添加一个 if 条件,使用 re.sub()
替换另一个模式。我试过:
new_list = [re.sub(r'replace_a', 'with_b', i)
if not re.findall(r'find_pattern', i, re.M) else
(re.sub(r'replace_c', r'with_d', i)
if foo == re.match("pattern", foo))
for i in old_list]
试图找出使用列表理解的正确方法,但没有成功。
最佳答案
作为常规 for 循环,这将更容易阅读:
new_list = []
for i in old_list:
if not re.findall(r'find_pattern', i, re.M):
new_list.append(re.sub(r'replace_a', 'with_b', i))
elif foo == re.match("pattern", foo):
new_list.append(re.sub(r'replace_c', r'with_d', i))
# else:
# i
您的问题是条件表达式必须始终采用else
子句,因为无论条件是否为真,它都必须有一些值。但是,对于 if
语句,您可以省略 else
。例如,上面的代码能够使 new_list
比 old_list
更短,因为不是每个 i
都需要调用 >new_list.append
。取消注释 else
的结果与 jpp 的答案相同。
如果你坚持使用列表理解,那么你可以格式化它以使其更具可读性。考虑
new_list = [re.sub(r'replace_pattern_a', 'with_pattern_b', i)
if not re.findall(r'find_pattern', i, re.M) else
re.sub(r'replace_pattern_c', r'with_pattern_d', i)
if foo == re.match("pattern", foo) else
i
for i in old_list]
虽然条件表达式并不是为这种嵌套而设计的。这在视觉上将可以添加到新列表的表达式与用于做出决定的条件分开,但我不是很喜欢。还有其他格式化方式可以做出不同的权衡,但 IMO 常规 for
循环更优越。
正如 jpp 提到的,另一种重构方法是定义一个生成器函数:
def foo(old_list):
for i in old_list:
if not re.findall(r'find_pattern', i, re.M):
yield re.sub(r'replace_a', 'with_b', i))
elif foo == re.match("pattern", foo):
yield re.sub(r'replace_c', r'with_d', i))
else:
yield i
new_list = list(foo())
这也有其优点和缺点。 (我认为列举这些可能超出了这个答案的范围,但它确实介于单个列表理解和显式 for
循环的两个极端之间。它也最接近我想念的构造从我的 Perl 时代开始,do
语句类似于 lambda
,它不接受任何参数,但可以包含任意语句。)
关于python - 列表理解语法错误中的嵌套 if/else 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49558561/
我试图从一些 sql 查询中获得一些额外的性能,这些查询在一个字段上有一个 where 子句,它是一个非唯一的非聚集索引,它也是表 A 中的一个外键。该外键是主键在表 B 上,是聚集索引。 我想知道的
当包含在 FOR 中时,应该如何编写此 WMIC 命令脚本中的命令? wmic service where (name="themes" and state="running") get 下面的代码不
请帮我理解如何订购 对over子句的影响。我已经阅读了 msdn 和一本书,但仍然误解了。 假设我们有这样的查询: SELECT Count(OrderID) over(Partition By Ye
参见如下SQL语句: SELECT datediff("d", MAX(invoice.date), Now) As Date_Diff , MAX(invoice.date) AS ma
不知何故,对我来说构建这样的查询有点困难:给我所有链接名称不为空的导航条目 $query = $this->db->get_where('navigation',array('linkname'!==
我一直在寻找这个,但没有发现任何特别的东西。 是否可以有一个像 ALL IN 一样的 SQL 查询?为了更好地解释,这是一个表结构。 Orders table OrderItem table (hav
SELECT DISTINCT Campaign_id FROM Impressions WHERE Date BETWEEN '2015-03-01' AND '2015-03-31' ; 上述查询
我尝试在 MyBatis 中遵循 if 子句并得到以下异常请帮助我确定这里的问题.. public class Student{ private Integer studId; private Str
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我尝试在 MyBatis 中遵循 if 子句并得到以下异常请帮助我确定这里的问题.. public class Student{ private Integer studId; private Str
是否可以用 where in 子句做这样的事情,我需要使用 where in 查询以下数据。 select * FROM instructor AS i INNER JOIN teaches AS t
嗨,我怎样才能让这个查询工作。我想要一个关于 where 子句的条件,如果 @BACHNUMB = '',那么 WHERE 是 (h.sopnumbe = @SOPNUMBE) 否则 WHERE 是
我在 MVC3 项目中工作。我浏览了一段时间并尝试了几个示例,但无法正常工作。 我需要从 OrderForm 表中获取记录列表,其 DeptID 在我已经获得的另一个列表中。 我知道我需要使用 Con
select * from staff LEFT JOIN servicereservation on servicereservation.snic = staff.snic where servi
我正在尝试使用 MySQL 创建带有“WITH”子句的 View WITH authorRating(aname, rating) AS SELECT aname, AVG(quantity)
我正在尝试使用 MySQL 创建触发器,但遇到错误。限制是:用户不得对他或她同时销售的商品出价。 Create Trigger before_insert_bid Before Insert on B
我正在尝试在 PostgreSql 的 WHERE IN 子句中使用 split_part,如下所示。这里 Objcode 是 small int 类型,objection 可能像 1374,824,
这可能很简单,只是我太厚了 - 我试图阻止保留的元素在记录中被拾取,但只有当库存大于 0 时,我不知道该怎么做除非 "....WHERE blah blah AND (reserved = 0 OR
我总结了两个表中两列的行,即如下所示: SUM( tableA.age ) + sum( tableB.age) as 'Total Ages' 但在某些情况下,A表的结果为空,而B表的结果则不是。在
我写了一个查询,从出生日期字段开始计算出一个人的年龄,然后使用 AS age 创建一个年龄字段。 我的问题是,是否可以再次匹配那个年龄字段? 像这样, SELECT `candidates`.`can
我是一名优秀的程序员,十分优秀!