我遇到迭代问题,特别是在达到某个值时停止迭代。当我达到此值时,我想执行某个任务(如下所述)并继续执行列表中的下一个元素。
这就是我想要完成的事情。我有一个列表lst
,如下所示:
[(1, 'x', 'NA'),
(2, 'x', 'low'),
(3, 'x', 'NA'),
(4, 'x', 'NA'),
(5, 'x', 'NA'),
(6, 'x', 'high'),
(7, 'x', 'NA'),
(8, 'x', 'NA'),
(9, 'x', 'NA'),
(10, 'x', 'NA'),
(11, 'x', 'low'),
(12, 'x', 'NA'),
(13, 'x', 'NA'),
(14, 'x', 'NA'),
(15, 'x', 'high'),
(16, 'x', 'NA'),
(17, 'x', 'NA'),
(18, 'x', 'NA'),
(19, 'x', 'NA'),
(20, 'x', 'NA'),
(21, 'x', 'low'),
(22, 'x', 'NA'),
(23, 'x', 'NA'),
(24, 'x', 'NA'),
(25, 'x', 'high'),
(26, 'x', 'NA')]
我想要做的是跟踪每个low_lst
和每个high_lst
。
例如,如果是第二个索引,则会将元素添加到low_lst
在元组中标记为low
,并且该元素后面未标记为high
的所有其他元素也将添加到列表中。在第一种情况下,它将是[2,3,4,5]
。
6
,因为它被标记为high.
因此,包含6
及其后的所有元素都会添加到high_lst
:{{1} }。
最终输出应如下所示:
[6,7,8,9,10]
这是我到目前为止所做的:
low_lst = [[2,3,4,5],[11,12,13,14], [21,22,23,24]]
high_lst = [[6,7,8,9,10],[15,16,17,18,19,20], [25,26]]
我正在尝试将所有元素添加到某个索引,但我不确定如何。
答案 0 :(得分:1)
您可以使用变量来跟踪列表生成的状态。当您点击'low'
元素时,它会切换到low_lst
;反之亦然'high'
。字典将状态映射到列表,子列表保存值。
low_lst = []
high_lst = []
STATE = None
state_dict = {'low': low_lst, 'high': high_lst}
for x, y, z in lst:
if z=='low' or z=='high':
STATE = z
sublist = []
state_dict[STATE].append(sublist)
sublist.append(x)
if STATE and z=='NA':
sublist.append(x)
low_lst
high_list
# returns:
[[2, 3, 4, 5], [11, 12, 13, 14], [21, 22, 23, 24]]
[[6, 7, 8, 9, 10], [15, 16, 17, 18, 19, 20], [25, 26]]
答案 1 :(得分:1)
a = [(1, 'x', 'NA'),
(2, 'x', 'low'),
(3, 'x', 'NA'),
(4, 'x', 'NA'),
(5, 'x', 'NA'),
(6, 'x', 'high'),
(7, 'x', 'NA'),
(8, 'x', 'NA'),
(9, 'x', 'NA'),
(10, 'x', 'NA'),
(11, 'x', 'low'),
(12, 'x', 'NA'),
(13, 'x', 'NA'),
(14, 'x', 'NA'),
(15, 'x', 'high'),
(16, 'x', 'NA'),
(17, 'x', 'NA'),
(18, 'x', 'NA'),
(19, 'x', 'NA'),
(20, 'x', 'NA'),
(21, 'x', 'low'),
(22, 'x', 'NA'),
(23, 'x', 'NA'),
(24, 'x', 'NA'),
(25, 'x', 'high'),
(26, 'x', 'NA')]
low_list = []
high_list = []
last=0
mark=None
for num,tup in enumerate(a):
if num == len(a)-1:
if mark == "high":
high_list.append([i[0] for i in a[last:]])
elif mark == "low":
low_list.append([i[0] for i in a[last:]])
elif a[num][2] == "high":
if mark == "low":
low_list.append([i[0] for i in a[last:num]])
last = num
mark = "high"
elif a[num][2] == "low":
if mark == "high":
high_list.append([i[0] for i in a[last:num]])
last = num
mark = "low"
low_list
high_list
答案 2 :(得分:1)
使用current_state
存储我们所处的状态(low
,high
或NA
),然后将内部列表存储在sub_list
。< / p>
当状态改变时,内部列表完成,我们重新开始并将sub_list
附加到右外部列表。
vals_list_low = []
vals_list_high = []
sub_list = None
current_state = 'NA'
for x, y, z in lst:
if z == 'low' or z == 'high':
state_changed = current_state != z
current_state = z
if state_changed:
sub_list = []
sub_list.append(x)
current_list = vals_list_low if z == 'low' else vals_list_high
current_list.append(sub_list)
continue
if sub_list:
sub_list.append(x)
print vals_list_low
print vals_list_high
如果出现两个连续low
或high
,则上述答案有效:
a = [(1, 'x', 'NA'),
(2, 'x', 'low'),
(3, 'x', 'NA'),
(4, 'x', 'low'),
(5, 'x', 'NA'),
(6, 'x', 'high'),
(7, 'x', 'NA'),
(8, 'x', 'NA'),
]
给出:
[[2, 3, 4, 5]]
[[6, 7, 8]]
答案 3 :(得分:1)
试试这个
lst=[(1, 'x', 'NA'),
(2, 'x', 'low'),
(3, 'x', 'NA'),
(4, 'x', 'NA'),
(5, 'x', 'NA'),
(6, 'x', 'high'),
(7, 'x', 'NA'),
(8, 'x', 'NA'),
(9, 'x', 'NA'),
(10, 'x', 'NA'),
(11, 'x', 'low'),
(12, 'x', 'NA'),
(13, 'x', 'NA'),
(14, 'x', 'NA'),
(15, 'x', 'high'),
(16, 'x', 'NA'),
(17, 'x', 'NA'),
(18, 'x', 'NA'),
(19, 'x', 'NA'),
(20, 'x', 'NA'),
(21, 'x', 'low'),
(22, 'x', 'NA'),
(23, 'x', 'NA'),
(24, 'x', 'NA'),
(25, 'x', 'high'),
(26, 'x', 'NA')]
def formatlist(inputlist):
startlist=[]
returnlist=[]
for i in inputlist:
if i == 'border':
if(startlist):
returnlist.append(startlist)
startlist=[]
else:
startlist.append(i)
return returnlist
temp=""
high_lst=[]
low_lst=[]
for i in lst:
if (i[2]== 'high'):
temp=""
high_lst.append('border')
elif (i[2] == 'low'):
temp=""
low_lst.append('border')
if (i[2]=='high') | (temp == 'high'):
high_lst.append(i[0])
temp='high'
elif (i[2]=='low') | (temp == 'low'):
low_lst.append(i[0])
temp='low'
low_lst.append('border')
high_lst.append('border')
print formatlist(low_lst)
print formatlist(high_lst)