我想每隔一个元素向现有列表中插入一个元素。
让我们说列表是fruits = ["banana", "apple", "mango", "kiwi"]
,添加元素"peach"
就是我的事情:
fruits = ["banana", "apple", "mango", "kiwi"]
fruits_2 = list(fruits)
for i in range(len(fruits)):
fruits_2.insert(2*i + 1, "peach")
print(fruits_2)
输出为
['banana', 'peach', 'apple', 'peach', 'mango', 'peach', 'kiwi', 'peach']
这是我想要的,但是我觉得可能有一种更好,更简洁的方法来执行此操作,而无需创建第二个列表。
答案 0 :(得分:6)
使用zip
以及itertools.repeat
和itertools.chain
。
>>> from itertools import chain, repeat
>>> fruits = ["banana", "apple", "mango", "kiwi"]
>>> list(chain.from_iterable(zip(fruits, repeat("peach"))))
['banana', 'peach', 'apple', 'peach', 'mango', 'peach', 'kiwi', 'peach']
repeat
创建'peach'
的无限序列。 zip
创建一系列对,由水果中的一个项目和一个'peach'
的实例组成。 chain.from_iterable
将对的序列“平化”为单个序列,list
从该序列中生成具体列表。
看中间步骤:
>>> from itertools import islice
>>> list(islice(repeat("peaches"), 5))
['peaches', 'peaches', 'peaches', 'peaches', 'peaches']
>>> zip(fruits, repeat("peaches"))
[('banana', 'peaches'), ('apple', 'peaches'), ('mango', 'peaches'), ('kiwi', 'peaches')]
这种方法可以归结为一系列appends
到新列表,例如
result = []
for x in fruits:
result.append(x)
result.append("peaches")
但是,您无需从一对迭代器中获取参数,而不必将参数硬编码为append
:
def peach_source():
while True:
yield "peach"
def fruit_source(fruits):
for x in fruits:
yield x
result = []
peaches = peach_source()
fruits = fruit_source(fruits) # Yes, this is highly redundant; just showing the similarity to peach_source
done = False
while not done:
try:
result.append(next(fruits))
except StopIteration:
done = True
result.append(next(peaches))
itertools.repeat
为您创建peach_source
。 zip
处理获取水果和获取桃子之间的交替。 chain.from_iterable
定义了附加到result
的动作,而list
实际上是执行附加。
答案 1 :(得分:2)
仅使用“ 双倍长度”(以及偶数/奇数位置)魔术:
FROM (SELECT col_1, col_2, col_3 FROM t1)
INSERT OVERWRITE TABLE t2
SELECT `(col_1)?+.+`
输出:
fruits = ["banana", "apple", "mango", "kiwi"]
new_fruits = [fruits[i // 2] if i % 2 == 0 else 'peach' for i in range(len(fruits) * 2)]
print(new_fruits)
如果继续使用['banana', 'peach', 'apple', 'peach', 'mango', 'peach', 'kiwi', 'peach']
方法itertools
,功能似乎是最快的:
itertools.zip_longest
时间:
new_fruits = [v for v in itertools.zip_longest(fruits, add, fillvalue=add[0])]
答案 2 :(得分:2)
列表理解+拼合选项-无需导入:
fruits = ["banana", "apple", "mango", "kiwi"]
add = 'peach'
out = [i for s in [[f, add] for f in fruits] for i in s]
out
# ['banana', 'peach', 'apple', 'peach', 'mango', 'peach', 'kiwi', 'peach']
itertools产品选项:
fruits = ["banana", "apple", "mango", "kiwi"]
add = ["peach"]
out = list(itertools.chain(*itertools.product(fruits, add)))
out
# ['banana', 'peach', 'apple', 'peach', 'mango', 'peach', 'kiwi', 'peach']
一些时间:
%timeit list(chain.from_iterable(zip(fruits, repeat(*add))))
910 ns ± 19.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit list(itertools.chain(*itertools.product(fruits, add)))
1.07 µs ± 13.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit [fruits[i // 2] if i % 2 == 0 else "peach" for i in range(len(fruits) * 2)]
1.62 µs ± 40.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
%timeit [i for s in [[f, *add] for f in fruits] for i in s]
2.16 µs ± 630 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
但是,最快的选择似乎是最简单的:
def func(fruits):
result = []
for x in fruits:
result.append(x)
result.append("peaches")
return result
%timeit func(fruits)
746 ns ± 15.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
答案 3 :(得分:0)
与您完全相同,但在第一个列表上:
this.props.convert_point