str.join() 相当于通用可迭代对象

问题描述 投票:0回答:1

如何实现像这样的

join()
函数?

>>> separator = [')', '-a', '(']
>>> args = [['-name', '*.py'], ['-type', 'f'], ['-mtime', '0']]
>>> list(join(separator, args))
["-name", "*.py", ")", "-a", "(", "-type", "f", ")", "-a", "(", "-mtime", "0"]
python
1个回答
0
投票
def join(sep, iterable):
    iterator = iter(iterable)
    yield from next(iterator)
    for i in iterator:
        yield from sep
        yield from i
© www.soinside.com 2019 - 2024. All rights reserved.