在以下列表中:
tab1 = [['D001', None, None, None, 'Donald Duck', 'Organise a meeting with Scrooge McDuck', 'todo', None],
['D002', None, None, None, 'Mickey Mouse','Organise a meeting with Minerva Mouse', 'done', None],
['D003', None, None, None, 'Mickey Mouse', 'Organise a meeting with Daisy Duck', 'todo', None],
[None, None, None, None, None, None, None, None]]
对于每个不为空的子列表,我想用“…”替换None值
我试过:
foo =[]
for row in tab1:
if row[0] is not None:
for cell in row:
if cell is None:
cell = "..."
foo.append(cell)
但是富给了我:
['D001',
'...',
'...',
'...',
'Donald Duck',
'Organise a meeting with Scrooge McDuck',
'todo',
'...',
'D002',
...
而不是:
[['D001',
'...',
'...',
'...',
'Donald Duck',
'Organise a meeting with Scrooge McDuck',
'todo',
'...',]
['D002',
...
您只创建了一个列表,而不是一个列表列表:
您只需要有临时变量:
使用嵌套列表并仅在列表中有非None值时替换值:
输出=[[y if y else'…'for y in x]if any(x)else[y for y in x]for x in tab1]
将其拆分以便于分析:
首先,更改列表中的
None
值:现在,仅当列表中有非None值时才更改}将返回true。你知道吗
None
值。^如果a_list
中的任何值不是None,{最后将其打包,使其在列表中的每个列表上工作:
相关问题 更多 >
编程相关推荐