问题
Hi I am trying to split the values in a (y2)
list into (inv)
amount of parts.
x = [0,100,200,300,400,500,600,700,800,900,1000,1100]
y2 = [58,55,49,12,6,5,4,4.5,35,48,56,58]
interval = 5
inv = (x[0] + x[1])/interval
I will like to split y2 into inv parts so my answer should be:
[58 57.85 57.7 57.55 57.4 57.25 57.1 56.95 56.8 56.65 56.5 56.35 56.2 56.05 55.9 55.75 55.6 55.45 55.3 55.15 55...…….49...…………..12...….6 ………...5...……..]
I will like to mention that inv will always be (x[0] + x[1])/interval
回答1:
Not sure I understood you correctly, but if I did, this would be one way of doing it:
x = [0,100,200,300,400,500,600,700,800,900,1000,1100]
y2 = [58,55,49,12,6,5,4,4.5,35,48,56,58]
inv = 5
intervals = int((x[0] + x[1]) / inv)
step_size = [(a - b)/intervals for i, (a, b) in enumerate(zip(y2, y2[1:]))]
res = []
for i, (top, bot) in enumerate(zip(y2, y2[1:])):
for j in range(intervals):
res.append(top - j * step_size[i])
res.append(y2[-1])
Which results in:
# [58.0, 57.85, 57.7, 57.55, 57.4, 57.25, 57.1, 56.95, 56.8, 56.65, 56.5, 56.35, 56.2, 56.05, 55.9, 55.75, 55.6, 55.45, 55.3, 55.15, 55.0, 54.7, 54.4, 54.1, 53.8, 53.5, 53.2, 52.9, 52.6, 52.3, 52.0, 51.7, 51.4, 51.1, 50.8, 50.5, 50.2, 49.9, 49.6, 49.3, 49.0, 47.15, 45.3, 43.45, 41.6, 39.75, 37.9, 36.05, 34.2, 32.349999999999994, 30.5, 28.65, 26.799999999999997, 24.95, 23.099999999999998, 21.25, 19.4, 17.549999999999997, 15.699999999999996, 13.850000000000001, 12.0, 11.7, 11.4, 11.1, 10.8, 10.5, 10.2, 9.9, 9.6, 9.3, 9.0, 8.7, 8.4, 8.1, 7.8, 7.5, 7.2, 6.9, 6.6000000000000005, 6.3, 6.0, 5.95, 5.9, 5.85, 5.8, 5.75, 5.7, 5.65, 5.6, 5.55, 5.5, 5.45, 5.4, 5.35, 5.3, 5.25, 5.2, 5.15, 5.1, 5.05, 5.0, 4.95, 4.9, 4.85, 4.8, 4.75, 4.7, 4.65, 4.6, 4.55, 4.5, 4.45, 4.4, 4.35, 4.3, 4.25, 4.2, 4.15, 4.1, 4.05, 4.0, 4.025, 4.05, 4.075, 4.1, 4.125, 4.15, 4.175, 4.2, 4.225, 4.25, 4.275, 4.3, 4.325, 4.35, 4.375, 4.4, 4.425, 4.45, 4.475, 4.5, 6.025, 7.55, 9.075, 10.6, 12.125, 13.649999999999999, 15.174999999999999, 16.7, 18.225, 19.75, 21.275, 22.799999999999997, 24.325, 25.849999999999998, 27.375, 28.9, 30.424999999999997, 31.95, 33.474999999999994, 35.0, 35.65, 36.3, 36.95, 37.6, 38.25, 38.9, 39.55, 40.2, 40.85, 41.5, 42.15, 42.8, 43.45, 44.1, 44.75, 45.4, 46.05, 46.7, 47.35, 48.0, 48.4, 48.8, 49.2, 49.6, 50.0, 50.4, 50.8, 51.2, 51.6, 52.0, 52.4, 52.8, 53.2, 53.6, 54.0, 54.4, 54.8, 55.2, 55.6, 56.0, 56.1, 56.2, 56.3, 56.4, 56.5, 56.6, 56.7, 56.8, 56.9, 57.0, 57.1, 57.2, 57.3, 57.4, 57.5, 57.6, 57.7, 57.8, 57.9, 58]
回答2:
I would go with
import numpy as np
# Your code here
res = np.empty((0,))
for i in range(1, len(x)):
delta = int((x[i] - x[i-1]) / interval)
res = np.append(res, np.linspace(y2[i-1], y2[i], delta, endpoint=False))
print(res)
来源:https://stackoverflow.com/questions/60222175/is-it-possible-to-divide-the-value-of-a-list-in-a-certain-amount-of-times