输入:
intersperse(666, ["once", "upon", "a", 90, None, "time"])
输出:
["once", 666, "upon", 666, "a", 666, 90, 666, None, 666, "time"]
写intersperse
的最优雅(读取:Pythonic)方法是什么?
答案 0 :(得分:30)
我自己会写一个生成器,但是像这样:
def joinit(iterable, delimiter):
it = iter(iterable)
yield next(it)
for x in it:
yield delimiter
yield x
答案 1 :(得分:17)
itertools
救援
- 或 -
你可以在一行中使用多少个itertools函数?
from itertools import chain, izip, repeat, islice
def intersperse(delimiter, seq):
return islice(chain.from_iterable(izip(repeat(delimiter), seq)), 1, None)
用法:
>>> list(intersperse(666, ["once", "upon", "a", 90, None, "time"])
["once", 666, "upon", 666, "a", 666, 90, 666, None, 666, "time"]
答案 2 :(得分:10)
适用于序列的另一个选项:
def intersperse(seq, value):
res = [value] * (2 * len(seq) - 1)
res[::2] = seq
return res
答案 3 :(得分:3)
我会选择一个简单的发电机。
def intersperse(val, sequence):
first = True
for item in sequence:
if not first:
yield val
yield item
first = False
然后你就可以得到你的清单:
>>> list(intersperse(666, ["once", "upon", "a", 90, None, "time"]))
['once', 666, 'upon', 666, 'a', 666, 90, 666, None, 666, 'time']
或者你可以这样做:
def intersperse(val, sequence):
for i, item in enumerate(sequence):
if i != 0:
yield val
yield item
我不确定哪个更像pythonic
答案 4 :(得分:2)
def intersperse(word,your_list):
x = [j for i in your_list for j in [i,word]]
>>> intersperse(666, ["once", "upon", "a", 90, None, "time"])
['once', 666, 'upon', 666, 'a', 666, 90, 666, None, 666, 'time', 666]
[修改] 以下修正后的代码:
def intersperse(word,your_list):
x = [j for i in your_list for j in [i,word]]
x.pop()
return x
>>> intersperse(666, ["once", "upon", "a", 90, None, "time"])
['once', 666, 'upon', 666, 'a', 666, 90, 666, None, 666, 'time']
答案 5 :(得分:2)
使用more_itertools.intersperse
解决方案很简单:
>>> from more_itertools import intersperse
>>> list(intersperse(666, ["once", "upon", "a", 90, None, "time"]))
['once', 666, 'upon', 666, 'a', 666, 90, 666, None, 666, 'time']
从技术上讲,这个答案不是"写" intersperse
,它只是从其他图书馆使用它。但它可能会让其他人不必重新发明轮子。
答案 6 :(得分:1)
Dunno,如果它是pythonic,但它很简单:
def intersperse(elem, list):
result = []
for e in list:
result.extend([e, elem])
return result[:-1]
答案 7 :(得分:1)
怎么样:
from itertools import chain,izip_longest
def intersperse(x,y):
return list(chain(*izip_longest(x,[],fillvalue=y)))
答案 8 :(得分:1)
我现在想出了这个,用谷歌搜索看看是否有更好的东西......而恕我直言那里没有: - )
def intersperse(e, l):
return list(itertools.chain(*[(i, e) for i in l]))[0:-1]
答案 9 :(得分:1)
您可以做的基本且简单的事情是:
a = ['abc','def','ghi','jkl']
# my separator is : || separator ||
# hack is extra thing : --
'--|| separator ||--'.join(a).split('--')
输出:
['abc','|| separator ||','def','|| separator ||','ghi','|| separator ||','jkl']
答案 10 :(得分:0)
这有效:
>>> def intersperse(e, l):
... return reduce(lambda x,y: x+y, zip(l, [e]*len(l)))
>>> intersperse(666, ["once", "upon", "a", 90, None, "time"])
('once', 666, 'upon', 666, 'a', 666, 90, 666, None, 666, 'time', 666)
如果您不想跟踪666
,请return reduce(...)[:-1]
。
答案 11 :(得分:0)
与yield next(iterator)
或itertools.iterator_magic()
相比,我认为它看起来不错并且易于掌握:)
def list_join_seq(seq, sep):
for i, elem in enumerate(seq):
if i > 0: yield sep
yield elem
print(list(list_join_seq([1, 2, 3], 0))) # [1, 0, 2, 0, 3]
答案 12 :(得分:-1)
def intersperse(items, delim):
i = iter(items)
return reduce(lambda x, y: x + [delim, y], i, [i.next()])
应该适用于列表或生成器。