python - 替换子列表中的项目而不展平
问题描述
在以下列表中:
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)
但是 foo 给了我:
['D001',
'...',
'...',
'...',
'Donald Duck',
'Organise a meeting with Scrooge McDuck',
'todo',
'...',
'D002',
...
代替:
[['D001',
'...',
'...',
'...',
'Donald Duck',
'Organise a meeting with Scrooge McDuck',
'todo',
'...',]
['D002',
...
解决方案
你只需要有临时变量:
foo = []
for row in tab1:
temp_list = []
if row[0] is not None:
for cell in row:
if cell is None:
cell = "..."
temp_list.append(cell)
foo.append(temp_list)
推荐阅读
- php - Symfony 分析器的执行时间不正确?
- javascript - 如何通过 Javascript 按 3 列对数组进行排序?
- python - Pickle 说它不能腌制 pygame.surface,但我的对象不是 pygame.surface
- recursion - Prolog 递归可能不会在适当的边界条件下退出
- html - 资源解释为文档,但传输时出现 MIME 类型音频/mpeg 错误
- spring - Spring security - 带有 REST 方法的 CSRF
- java - 创建一个创建数组的类
- django - 如何将我的用户详细信息的个人资料检索到用户的个人资料页面
- javascript - 使用 Helmet 加载 JavaScript 文件不起作用
- image-processing - 如何使用 skimage 在图像上使用阈值标记和绘制边界