如何在 python 列表中每隔一个元素插入一个元素
How to insert an element after every other element in python list
你好,我想知道如何使用 for 循环遍历列表并在新列表中的每个元素之后插入一个元素。
我看过这个link
但是当我尝试该方法时,在我的代码中实现它时出现了完全相同的问题
"""
Created on Sat Mar 28 20:40:37 2020
@author: DeAngelo
"""
import math
import matplotlib.pyplot as plt
import numpy as np
from numpy import sqrt
from quadpy import quad
import scipy.integrate as integrate
import scipy.special as special
experiment = [1,2,3,4,5]
count = len(experiment)
after = []
new = []
for i in range(0,count-1):
average2 = (experiment[i] + experiment[i+1])/2
new.append(average2)
print(experiment)
for i in range(0,count-1):
just = experiment.insert(i+1,new[i])
print(new,'\n')
print(experiment)
1st print(experiment) -> [1, 2, 3, 4, 5]
print(new,'\n') -> [1.5, 2.5, 3.5, 4.5]
和
2nd, print(experiment) -> [1, 1.5, 2.5, 3.5, 4.5, 2, 3, 4, 5]
但我希望它是 [1,1.5,2,2.5,3,3.5,4,4.5,5] 我知道我可以使用合并和排序,但是我不想这样做,因为我正在处理一个更大得多的列表,而且无法对其进行排序。这只是我现在的宝贝清单。
我真的很亲近我能感觉到它,就像第六感一样...非常感谢任何帮助和指导。非常感谢小可爱
对于大型数据集,多次插入列表中间会非常慢。看来您可以像现在一样构建一个新列表:
first = [1, 2, 3, 4, 5]
second = []
for i in range(0, len(first) - 1):
avg = (first[i] + first[i+1]) / 2
second.append(first[i])
second.append(avg)
second.append(first[-1])
print(second)
打印:
[1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
experiment = [1, 2, 3, 4, 5]
count = len(experiment)
for i in range(count - 1):
experiment.insert(2*i+1, (experiment[2*i] + experiment[2*i+1])/2)
print(experiment)
对我来说,itertools recipes 的 roundrobin
的任务是:
from itertools import cycle, islice
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).__next__ for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
a = [1, 2, 3, 4, 5]
b = [10, 20, 30, 40]
ab = list(roundrobin(a,b))
print(ab)
输出:
[1, 10, 2, 20, 3, 30, 4, 40, 5]
请注意,roundrobin
可能用于交错超过 2 个可迭代对象,并且总是在最长的一个迭代器上终止。当您 使用更大的列表 时,它具有创建 generator
而不是 list
的额外好处。
为了尝试使用列表理解在单次传递中生成所需的输出列表,我提供了这个:
e = [1, 2, 3, 4]
r = [e[i] if j == 0 else sum(e[i:i+2])/2. for i in range(len(e)) for j in range(min(len(e) - i, 2))]
assert r == [1, 1.5, 2, 2.5, 3, 3.5, 4]
我使用嵌套 for 循环只是为了在将原始元素传递给输出和计算后续元素的平均值之间切换。
你可以使用这个单行列表理解
from itertools import zip_longest
a = [1,2,3,4,5]
b = [1.5, 2.5, 3.5, 4.5]
flat_list = [val for sublist in zip_longest(a,b) for val in sublist if val]
输出 = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
你好,我想知道如何使用 for 循环遍历列表并在新列表中的每个元素之后插入一个元素。
我看过这个link
但是当我尝试该方法时,在我的代码中实现它时出现了完全相同的问题
"""
Created on Sat Mar 28 20:40:37 2020
@author: DeAngelo
"""
import math
import matplotlib.pyplot as plt
import numpy as np
from numpy import sqrt
from quadpy import quad
import scipy.integrate as integrate
import scipy.special as special
experiment = [1,2,3,4,5]
count = len(experiment)
after = []
new = []
for i in range(0,count-1):
average2 = (experiment[i] + experiment[i+1])/2
new.append(average2)
print(experiment)
for i in range(0,count-1):
just = experiment.insert(i+1,new[i])
print(new,'\n')
print(experiment)
1st print(experiment) -> [1, 2, 3, 4, 5]
print(new,'\n') -> [1.5, 2.5, 3.5, 4.5]
和
2nd, print(experiment) -> [1, 1.5, 2.5, 3.5, 4.5, 2, 3, 4, 5]
但我希望它是 [1,1.5,2,2.5,3,3.5,4,4.5,5] 我知道我可以使用合并和排序,但是我不想这样做,因为我正在处理一个更大得多的列表,而且无法对其进行排序。这只是我现在的宝贝清单。
我真的很亲近我能感觉到它,就像第六感一样...非常感谢任何帮助和指导。非常感谢小可爱
对于大型数据集,多次插入列表中间会非常慢。看来您可以像现在一样构建一个新列表:
first = [1, 2, 3, 4, 5]
second = []
for i in range(0, len(first) - 1):
avg = (first[i] + first[i+1]) / 2
second.append(first[i])
second.append(avg)
second.append(first[-1])
print(second)
打印:
[1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
experiment = [1, 2, 3, 4, 5]
count = len(experiment)
for i in range(count - 1):
experiment.insert(2*i+1, (experiment[2*i] + experiment[2*i+1])/2)
print(experiment)
对我来说,itertools recipes 的 roundrobin
的任务是:
from itertools import cycle, islice
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).__next__ for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
a = [1, 2, 3, 4, 5]
b = [10, 20, 30, 40]
ab = list(roundrobin(a,b))
print(ab)
输出:
[1, 10, 2, 20, 3, 30, 4, 40, 5]
请注意,roundrobin
可能用于交错超过 2 个可迭代对象,并且总是在最长的一个迭代器上终止。当您 使用更大的列表 时,它具有创建 generator
而不是 list
的额外好处。
为了尝试使用列表理解在单次传递中生成所需的输出列表,我提供了这个:
e = [1, 2, 3, 4]
r = [e[i] if j == 0 else sum(e[i:i+2])/2. for i in range(len(e)) for j in range(min(len(e) - i, 2))]
assert r == [1, 1.5, 2, 2.5, 3, 3.5, 4]
我使用嵌套 for 循环只是为了在将原始元素传递给输出和计算后续元素的平均值之间切换。
你可以使用这个单行列表理解
from itertools import zip_longest
a = [1,2,3,4,5]
b = [1.5, 2.5, 3.5, 4.5]
flat_list = [val for sublist in zip_longest(a,b) for val in sublist if val]
输出 = [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]