在不等列表上使用zip(),但避免返回无

时间:2019-04-25 10:09:47

标签: python-2.7 list itertools

有关于此的现有主题 Zipping unequal lists in python in to a list which does not drop any element from longer list being zipped

但这不是我所追求的。 而不是返回“无”,我需要它复制上一个列表中的条目。

这可能吗?

谢谢你看我的问题。

a = ["bottle","water","sky"]
b = ["red", "blue"]
for i in itertools.izip_longest(a,b):
    print i

#result
# ('bottle', 'red')
# ('water', 'blue')
# ('sky', None) 

# What I want on the third line is
# ('sky', 'blue')

2 个答案:

答案 0 :(得分:1)

itertools.izip_longest带有一个可选的fillvalue参数,该参数提供了精简列表用尽后使用的值。 fillvalue默认为None,给出您在问题中显示的行为,但是您可以指定其他值以获取所需的行为:

    fill = a[-1] if (len(a) < len(b)) else b[-1]
    for i in itertools.izip_longest(a, b, fillvalue=fill):
        print i

(显然,如果相同的列表总是较短的列表,那么选择填充字符会更加容易。)

答案 1 :(得分:0)

您可以chain的最后一个值repeat来作为较短的列表。然后使用常规izip,结果将是较长列表的长度:

from itertools import izip, repeat, chain

def izip_longest_repeating(seq1, seq2):
    if len(seq1) < len(seq2):
        repeating = seq1[-1]
        seq1 = chain(seq1, repeat(repeating))
    else:
        repeating = seq2[-1]
        seq2 = chain(seq2, repeat(repeating))
    return izip(seq1, seq2)   

print(list(izip_longest_repeating(a, b)))
#  [('bottle', 'red'), ('water', 'blue'), ('sky', 'blue')]    

这是一个适用于所有可迭代对象的版本:

from itertools import izip as zip # Python2 only

def zip_longest_repeating(*iterables):
    iters = [iter(i) for i in iterables]
    sentinel = object() 
    vals = tuple(next(it, sentinel) for it in iters)
    if any(val is sentinel for val in vals):
        return
    yield vals
    while True:
        cache = vals
        vals = tuple(next(it, sentinel) for it in iters)
        if all(val is sentinel for val in vals):
            return
        vals = tuple(old if new is sentinel else new for old, new in zip(cache, vals))
        yield vals

list(zip_longest_repeating(['a'], ['b', 'c'], ['d', 'r', 'f']))
#  [('a', 'b', 'd'), ('a', 'c', 'r'), ('a', 'c', 'f')]