替换子列表中的项目而无需拼合

时间:2018-10-16 10:20:16

标签: python python-3.x

在以下列表中:

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',
...

6 个答案:

答案 0 :(得分:2)

您仅创建一个列表,而不是列表列表:

bar = []
for row in tab1:
    foo = []
    if row[0] is not None:
        for cell in row:
            if cell is None:
                cell = "..."
            foo.append(cell)
        bar.append(foo)

print(bar)

答案 1 :(得分:2)

您只需要具有临时变量:

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)

答案 2 :(得分:2)

好吧,您可以为此使用pandas。要安装熊猫,请使用pip install pandas或安装蟒蛇

将列表列表转换为熊猫数据框,用...填充所有缺少的值,然后将其转换回列表列表

import pandas as pd
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]]
df=pd.DataFrame(tab1)
df.fillna(value="...", inplace=True)
listoflist=df.values.tolist()

输出

[['D001', '...', '...', '...', 'Donald Duck', 'Organise a meeting with Scrooge McDuck', 'todo', '...'], ['D002', '...', '...', '...', 'Mickey Mouse', 'Organise a meeting with Minerva Mouse', 'done', '...'], ['D003', '...', '...', '...', 'Mickey Mouse', 'Organise a meeting with Daisy Duck', 'todo', '...'], ['...', '...', '...', '...', '...', '...', '...', '...']]

答案 3 :(得分:2)

使用嵌套列表推导,仅在列表中包含非值时才替换值:

output = [[如果在x中为y,则为y,否则为'...'];如果在tab1中为x,则为[x],否则为[x,y为,y中为y]

将其拆分以使其更易于解析:

首先,更改列表中的None值:

a = ['A','B',None,'C','D',None]
# if we have a value in y leave it alone, otherwise change it to '...'
a = [y if y else '...' for y in a]
# a is now ['A','B','...','C','D','...']

现在,如果列表中存在非值,则仅更改None值。如果any(a_list)中的任何值都不为None,则a_list将返回true。

a = ['A','B',None,'C','D',None]
b = [None,None,None,None,None,None]
a = [y if y else '...' for y in a] if any(a) else [y for y in a]
b = [y if y else '...' for y in b] if any(b) else [y for y in b]
# a changed as above, b unchanged

最后将其包装起来,以使其适用于列表列表中的每个列表:

output = [[y if y else '...' for y in x] if any(x) else [y for y in x] for x in tab1]

答案 4 :(得分:0)

使用理解力:

[['...' if y is None else y for y in x] for x in tab1]

答案 5 :(得分:0)

根据索引值遍历列表。

foo = tab1[:]
for row in range(0,len(tab1)):
    if tab1[row][0] is not None:
        for cell in range(0,len(tab1[row])):
            if tab1[row][cell] is None:
                foo[row][cell] = "..."