Python - 更改列表中的项目值

Eri*_*pur 1 python python-3.x

我有一些使用 itertools.zip_longest 压缩在一起的数据

import itertools
names = 'Tim Bob Julian Carmen Sofia Mike Kim Andre'.split()
locations = 'DE ES AUS NL BR US'.split()
confirmed = [False, True, True, False, True]
zipped_up = list(itertools.zip_longest(names, locations, confirmed))
Run Code Online (Sandbox Code Playgroud)

如果我按照现在的方式打印 zipped_up,我会得到以下信息:

[('Tim', 'DE', False), ('Bob', 'ES', True), 
('Julian','AUS', True), ('Carmen', 'NL', False), 
('Sofia', 'BR',True), ('Mike', 'US', None), 
('Kim',None, None),('Andre', None, None)]
Run Code Online (Sandbox Code Playgroud)

这很好,缺失值默认为“无”。 现在我想将“无”值更改为 '-'

似乎我应该能够在以下嵌套循环中这样做。如果我在下面的代码中包含一个打印语句,看起来一切都按我想要的方式工作:

for items in zipped_up:
    for thing in items:
        if thing == None:
            thing = '-'
        print(thing)
Run Code Online (Sandbox Code Playgroud)

但是如果我再次打印 zipped_up (在循环之外),“无”值没有改变。为什么不?是否与列表项的数据类型有关,哪些是元组?

我已经引用了其他一些 stackoverflow 线程,包括这个线程,但一直无法使用它: 查找和替换列表中的元素 (python)

sac*_*cuL 5

只需使用fillvalue参数:

zipped_up = list(itertools.zip_longest(names, locations, confirmed, fillvalue='-'))

>>> zipped_up
[('Tim', 'DE', False), ('Bob', 'ES', True), ('Julian', 'AUS', True), ('Carmen', 'NL', False), ('Sofia', 'BR', True), ('Mike', 'US', '-'), ('Kim', '-', '-'), ('Andre', '-', '-')]
Run Code Online (Sandbox Code Playgroud)