如何制作将子列表添加到列表的函数
How to make a function that adds a sublist to a list
我想创建一个 NewList
个值(介于 1 和 0 之间),使用一个函数将 sublist
个值 (low_phase=[0] x 30)
添加到第二个列表 (high_phase=[1] x 960 elements long)
.我需要一个循环,因为函数需要及时遍历所有元素,我需要一个 if 函数,它检查时间元素何时等于列表间隔的元素,然后才应用 sublist
low_phase。当值相等时,列表应包含 0,当值不同时,则为 1。
#this is the `NewList` I want to create. It has to contain values of 1 or 0, which are assigned based on the function with loop
NewList = []
#this is the first list
time = list(range(1,960))
#this is the second list
interval= list(range(60,960,60))
#these are the values to be assigned to the newLIst if the condition is true
high_phase = [1]*960
#these are the values to be assigned to the newLIst if the condition is False
low_phase = [0]*29
def method(NewList):
for n,m in zip(time,interval):
if time[n] == interval[m]:
NewList.extend(low_phase)
else:
NewList.append(high_phase)
print(NewList)
example of output: for every time that interval (60, 120, 180 etc) is contained in time (0-960), add a list of 30 values = 0 to NewList, otherwise add 1 to NewList.
=================
time interval NewList
... 1
58 1
59 1
60 60 0
61 0
62 0
... 0
90 1
91 1
92 1
.... 1
120 120 0
121 0
122 0
... 0
150 1
根据您提供的代码,我认为这就是您想要的:
NewList = []
time = list(range(1,960))
interval= list(range(60,960,60))
high_phase = [1]*960
low_phase = [0]*59
def method(NewList):
for i in interval:
NewList.extend(low_phase) # 59 entries from low_phase
NewList.append(high_phase[len(NewList)]) # matching high_phase entry
return NewList
print(method(NewList))
输出(包装、截断)
[0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
...........
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1]
好的,我想现在我得到了你想要的:一个 960 1
s 的列表,范围为 30 0
s 从每个间隔开始。这正是您可以做到的:从 960*[1]
开始,并用 30*[0]
替换每个间隔的 30 个元素的切片。
result = [1] * 960
for i in range(60, 960, 60):
result[i:i+30] = [0] * 30
(注意:如果 30 个元素的最后一个片段比列表长,这将用更多的 0 扩展列表,因此您可能必须在末尾将其截断回 960 个元素。)
输出(一些):
>>> list(enumerate(result))
[...
(58, 1),
(59, 1),
(60, 0),
(61, 0),
(62, 0),
...
(88, 0),
(89, 0),
(90, 1),
(91, 1),
(92, 1),
...]
关于您原来的 method
功能:存在许多问题。首先,您压缩了两个长度截然不同的列表(960 对 15 个元素),因此您只能从两者中获得前 15 个元素。然后,n
和 m
已经是该列表中的元素,但是对于 time[n] == interval[m]
你将它们视为索引,这将给出一个 IndexError 因为第一个间隔 60
已经是大于整个区间列表。无论如何,条件永远不会为真,因为间隔从更高的值开始并且数字上升得更快,因此“配对”值永远不会相等。
我想创建一个 NewList
个值(介于 1 和 0 之间),使用一个函数将 sublist
个值 (low_phase=[0] x 30)
添加到第二个列表 (high_phase=[1] x 960 elements long)
.我需要一个循环,因为函数需要及时遍历所有元素,我需要一个 if 函数,它检查时间元素何时等于列表间隔的元素,然后才应用 sublist
low_phase。当值相等时,列表应包含 0,当值不同时,则为 1。
#this is the `NewList` I want to create. It has to contain values of 1 or 0, which are assigned based on the function with loop
NewList = []
#this is the first list
time = list(range(1,960))
#this is the second list
interval= list(range(60,960,60))
#these are the values to be assigned to the newLIst if the condition is true
high_phase = [1]*960
#these are the values to be assigned to the newLIst if the condition is False
low_phase = [0]*29
def method(NewList):
for n,m in zip(time,interval):
if time[n] == interval[m]:
NewList.extend(low_phase)
else:
NewList.append(high_phase)
print(NewList)
example of output: for every time that interval (60, 120, 180 etc) is contained in time (0-960), add a list of 30 values = 0 to NewList, otherwise add 1 to NewList.
=================
time interval NewList
... 1
58 1
59 1
60 60 0
61 0
62 0
... 0
90 1
91 1
92 1
.... 1
120 120 0
121 0
122 0
... 0
150 1
根据您提供的代码,我认为这就是您想要的:
NewList = []
time = list(range(1,960))
interval= list(range(60,960,60))
high_phase = [1]*960
low_phase = [0]*59
def method(NewList):
for i in interval:
NewList.extend(low_phase) # 59 entries from low_phase
NewList.append(high_phase[len(NewList)]) # matching high_phase entry
return NewList
print(method(NewList))
输出(包装、截断)
[0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
...........
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1,
0, 0, 0, 0, ... 0, 0, 0, 1]
好的,我想现在我得到了你想要的:一个 960 1
s 的列表,范围为 30 0
s 从每个间隔开始。这正是您可以做到的:从 960*[1]
开始,并用 30*[0]
替换每个间隔的 30 个元素的切片。
result = [1] * 960
for i in range(60, 960, 60):
result[i:i+30] = [0] * 30
(注意:如果 30 个元素的最后一个片段比列表长,这将用更多的 0 扩展列表,因此您可能必须在末尾将其截断回 960 个元素。)
输出(一些):
>>> list(enumerate(result))
[...
(58, 1),
(59, 1),
(60, 0),
(61, 0),
(62, 0),
...
(88, 0),
(89, 0),
(90, 1),
(91, 1),
(92, 1),
...]
关于您原来的 method
功能:存在许多问题。首先,您压缩了两个长度截然不同的列表(960 对 15 个元素),因此您只能从两者中获得前 15 个元素。然后,n
和 m
已经是该列表中的元素,但是对于 time[n] == interval[m]
你将它们视为索引,这将给出一个 IndexError 因为第一个间隔 60
已经是大于整个区间列表。无论如何,条件永远不会为真,因为间隔从更高的值开始并且数字上升得更快,因此“配对”值永远不会相等。