gpt4 book ai didi

python - 填写两个列表之间的范围

转载 作者:行者123 更新时间:2023-12-02 02:36:29 24 4
gpt4 key购买 nike

我有一组像这样的列表:

listoflists = ['list1','list2']
list1_start = pd.DataFrame([1,6,11,24,33,45,55])
list1_end = pd.DataFrame([2,8,15,26,37,48,90])

list2_start = pd.DataFrame([5,9,44,72])
list2_end = pd.DataFrame([7,17,52,80])

我想为开始列表和结束列表之间创建一个新的数字列表:

预期结果:

list1_duration = [1,1.5,2,6,6.5,7,7.5,8,11,11.5,12,12.5,13,13.5,14,14.5,15....
list2_dutation = [5.5,6,6.5,7,9,9.5,10,10.5,11,11.5,12,12.5,13,13.5,14,14.5,15,15.5,16,16.6,17...

我试图找出两者之间的差异,然后为这些差异创建一个范围,我可以将其添加回起始列表,但我设法得到的只是一个范围列表,而且这仅适用于一个列表和我不知道如何迭代列表列表中的所有列表(尝试过,但失败了)。

list1_dif = (list1_end)-(list1_start)

list1_dif = list1_dif[0].tolist()

list1_dur = []

for i in range(len(list1_dif)):
command = ''
command = 'list1_dur.append(list(range('+str(list1_dif[i])+')))'
exec(command)

list1_duration = list1_dur + list1_start

print(list1_dur)

输出:

[[0], [0, 1], [0, 1, 2, 3], [0, 1], [0, 1, 2, 3], [0, 1, 2], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]]

谁能帮我解决这个问题吗?

谢谢

盖尔盖伊

最佳答案

.arange() 将获取之间的值,然后您可以在列表上使用 .extend() 而不是 .append() 尝试下面的代码片段:

import numpy as np
list1_start = [1,6,11,24,33,45,55]
list1_end = [2,8,15,26,37,48,90]

list2_start = [5,9,44,72]
list2_end = [7,17,52,80]

def fill_in_range(list_start,list_end):
if len(list_start) == len(list_end):
new_list = []
for x,y in zip(list_start, list_end):
new_list.extend(np.arange(x,y+0.0000001,0.5))
return new_list
else:
return 'Lists are not the same size'

list1_duration = fill_in_range(list1_start, list1_end)
list2_duration = fill_in_range(list2_start, list2_end)

list1_duration 和 list2_duration 变量:

list1_duration = [1.0, 1.5, 2.0, 6.0, 6.5, 7.0, 7.5, 8.0, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0...]

list2_duration = [5.0, 5.5, 6.0, 6.5, 7.0, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 44.0, 44.5, 45.0, 45.5, 46.0...]

关于python - 填写两个列表之间的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60902013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com