简体   繁体   English

在列表中的元素之间添加元素N次

[英]add an element N times between elements in list

I have this code that intersperses from python: most elegant way to intersperse a list with an element . 我有这段代码是从python散布的:最优雅的方式是用一个元素散布一个列表

def joinit(iterable, delimiter):
    it = iter(iterable)
    yield next(it)
    for x in it:
        yield delimiter
        yield x

I would like to do about the same thing, only that I can add an element n times between the elements in the list. 我想做同样的事情,只是我可以在列表中的元素之间添加n次元素。

Example: 例:

joinNTimes(3,"a",[1,2,3])

Will become 会变成

[1,"a","a","a",2,"a""a","a",3,"a","a","a"]

Thanks. 谢谢。

You problem is simpler because you only need to insert delimiter after every iterable element. 您的问题更简单,因为您只需要在每个可迭代元素之后插入定界符即可。

def joinNTimes(count, delimiter, iterable):
    for x in iterable:
        yield x
        for i in xrange(count):
            yield delimiter

Here's my solution. 这是我的解决方案。 I just added an extra loop to yield the delimiter multiple times. 我只是添加了一个额外的循环来多次产生定界符。 It doesn't return the last set of the delimiter, but the code for the single version you posted doesn't seem to either. 它不会返回定界符的最后一组,但是您发布的单个版本的代码似乎也没有。

def joinNTimes(n, delimiter, iterable):
    it = iter(iterable)
    yield next(it)
    for x in it:
        for i in xrange(n):
            yield delimiter
        yield x

I am wondering, what's the point of iter(iterable) here, does it have any advantages to use it? 我想知道,这里iter(iterable)有什么意义,使用它有什么好处吗? Can't it simply be: 不能简单地是:

def joinit(count, iterable, delimiter):
    for x in iterable:
        yield x
        for _ in range(count):
            yield delimiter

list(joinit(2, [1,2,3], 'a'))

Out: 出:

[1, 'a', 'a', 2, 'a', 'a', 3, 'a', 'a']

You just need another for loop inside - 您只需要在其中另一个for循环-

def joinNtimes(delimiter, times, iterable):
    it = iter(iterable)
    yield next(it)
    for x in it:
        for i in range(times):
            yield delimiter
        yield x

A cleaner version of the same function is posted by @Nix. @Nix发布了具有相同功能的更清洁版本。 You don't need to convert iterable . 您不需要转换iterable

Adding a parameter n to the joinit() function and an extra loop within it does the job: joinit()函数添加参数n并在joinit()添加一个额外的循环即可完成此工作:

def joinit_n(iterable, delimiter, n):
    it = iter(iterable)
    yield next(it)
    for x in it:
        # yield the delimiter n times instead of just once:
        for i in range(n):
            yield delimiter
        yield x

If you definitely want the delimiter to be added on the end n times too, as in your example, then you'll want to simply add another loop to the end: 如果你一定要要在结尾添加分隔符n次也一样,在你的榜样,那么你会希望只是另一种循环添加到末尾:

def joinit_n(iterable, delimiter, n):
    it = iter(iterable)
    yield next(it)
    for x in it:
        # yield the delimiter n times instead of just once:
        for i in range(n):
            yield delimiter
        yield x
    # And another n delimiters at the end
    for i in range(n):
        yield delimiter

There are surely ways to cleverly one-liner this, but readability counts! 当然,有多种方法可以巧妙地实现这一点,但是可读性很重要! Explicitly writing out the loop is just fine. 明确地写出循​​环就好了。

def joinit(iterable, delimiter):
    for x in iterable:
        yield x
        if isinstance(delimiter, list):
            for y in list(delimiter):
                yield y
        else:
            yield delimiter

and

list(joinit([1, 2, 3], ['a', 'a']))

returns 退货

[1, 'a', 'a', 2, 'a', 'a', 3, 'a', 'a']

Advantage: the semantics of joinit stay the same for single-element delimiters and for multi-element delimiters. 优点: joinit的语义对于单元素定界符和多元素定界符保持相同。 The function caller decides, not the function. 函数调用者决定函数,而不是函数。

Hint: ['a', 'a'] is ['a'] * 2 or list('a' * 2) 提示: ['a', 'a']['a'] * 2list('a' * 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM