是否在列表中理解?如何在Python中完成以下操作?row = [unicode(x.strip()) for x in row if x is not None else '']实质上:用空字符串替换所有Nones,然后履行职能。
3 回答

斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
[unicode(x.strip()) if x is not None else '' for x in row]
[f(x) if condition else g(x) for x in sequence]
if
[f(x) for x in sequence if condition]
if
for…in
?:
value = 123print(value, 'is', 'even' if value % 2 == 0 else 'odd')

潇潇雨雨
TA贡献1833条经验 获得超4个赞
def change(f): if f is None: return unicode(f.strip()) else: return ''row = [change(x) for x in row]
row = map(change, row)

四季花海
TA贡献1811条经验 获得超5个赞
>>> print(", ".join(["ha" if i else "Ha" for i in range(3)]) + "!")Ha, ha, ha!
if i
False
0
True
range()
>>> ["ha" if i else "Ha" for i in range(3)]['Ha', 'ha', 'ha']
添加回答
举报
0/150
提交
取消