Python 如何检查列表而不出现错误:列表索引超出范围
我有一个索引列表,如下所示:Python 如何检查列表而不出现错误:列表索引超出范围,python,python-3.x,list,indexing,range,Python,Python 3.x,List,Indexing,Range,我有一个索引列表,如下所示: index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45] 我得到了一个: pattern_width = 4 n = pattern_width final_list = [] 我如何一次分析n个元素的列表,如果所有元素的值都相等,它们将被附加到一个空列表中 因此,由于前4个元素是[5,5,5
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
我得到了一个:
pattern_width = 4
n = pattern_width
final_list = []
我如何一次分析n个元素的列表,如果所有元素的值都相等,它们将被附加到一个空列表中
因此,由于前4个元素是[5,5,5,5],因此值5将被附加到最终的_列表中。但是,由于接下来的4个元素是[5,5,5,6],因此不会追加该元素
解决方案是[5,16,34,42]
我经常遇到的问题是列表索引超出范围
我的做法是:
for i in range(len(index_max)):
x = index_max[i]==index_max[i+(pattern_width)
final_list.append(x)
但是,这在列表末尾无法工作。我该如何解决这个问题?谢谢。您可以尝试以下方法
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
pattern_width = 4
final_list = []
for i in range(len(index_max) - pattern_width):
temp = index_max[i:i + pattern_width]
s = set(temp)
if len(s) == 1:
final_list.append(temp[0])
print(final_list) # Output [5, 16, 34, 42]
工作示例您可以尝试以下方法
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
pattern_width = 4
final_list = []
for i in range(len(index_max) - pattern_width):
temp = index_max[i:i + pattern_width]
s = set(temp)
if len(s) == 1:
final_list.append(temp[0])
print(final_list) # Output [5, 16, 34, 42]
工作示例尝试以下方法:
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
pattern_width = 4
final_list = []
for i in range(len(index_max)-3):
count = 1
for j in range(pattern_width):
if index_max[i] == index_max[i+j]:
count += 1
if count == 4:
final_list.append(index_max[i])
print(final_list)
试试这个:
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
pattern_width = 4
final_list = []
for i in range(len(index_max)-3):
count = 1
for j in range(pattern_width):
if index_max[i] == index_max[i+j]:
count += 1
if count == 4:
final_list.append(index_max[i])
print(final_list)
如果列表始终排序,只需对每个值进行计数:
final_list = [candidate for candidate in set(index_max) if index_max.count(candidate) >= pattern_width]
由于大多数调用都是针对内置的,因此应该足够快
如果需要实际窗口,请对方法进行两次修改:
图案宽度
集合
识别唯一的窗口
# stop iteration ``pattern_width`` elements before the end
for i in range(len(index_max) - pattern_width):
# slice the window from the list and eliminate duplicates
window_elements = set(index_max[i:i+pattern_width])
if len(window_elements) == 1:
final_list.extend(window_elements)
如果列表始终排序,只需对每个值进行计数:
final_list = [candidate for candidate in set(index_max) if index_max.count(candidate) >= pattern_width]
由于大多数调用都是针对内置的,因此应该足够快
如果需要实际窗口,请对方法进行两次修改:
图案宽度
集合
识别唯一的窗口
# stop iteration ``pattern_width`` elements before the end
for i in range(len(index_max) - pattern_width):
# slice the window from the list and eliminate duplicates
window_elements = set(index_max[i:i+pattern_width])
if len(window_elements) == 1:
final_list.extend(window_elements)
index_max[i]
如果满足条件,则返回索引值范围(len(index_max)-(n-1))
这将搜索列表中4个组合。n-1将确保列表在长度为4的最后一个组合上停止len(set(index_max[i:i+n])==1
将测试列表转换为一个集合。这将允许您根据集合的长度计算唯一值final_list = {index_max[i] for i in range(len(index_max)-(n-1)) if len(set(index_max[i:i+n])) == 1}
这应该能奏效。分解它
index_max[i]
如果满足条件,则返回索引值范围(len(index_max)-(n-1))
这将搜索列表中4个组合。n-1将确保列表在长度为4的最后一个组合上停止len(set(index_max[i:i+n])==1
将测试列表转换为一个集合。这将允许您根据集合的长度计算唯一值final_list = {index_max[i] for i in range(len(index_max)-(n-1)) if len(set(index_max[i:i+n])) == 1}
下面是一个使用itertools中的一系列方法的解决方案。我继续将问题分为3个部分,这样,如果您需要对代码执行其他操作,那么它将更加灵活
import itertools
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
n = 4
def all_equal(iterable):
g = itertools.groupby(iterable)
return next(g, True) and not next(g, False)
def window(iterable, window_size):
iters = itertools.tee(iterable, window_size)
for i in range(1, window_size):
for it in iters[i:]:
next(it, None)
return zip(*iters)
def uniques_from_window(iterable, window_size):
return [sub_list[0] for sub_list in window(iterable, window_size) if all_equal(sub_list)]
print(uniques_from_window(index_max, n))
输出
[5, 16, 34, 42]
下面是一个使用itertools中的一系列方法的解决方案。我继续将问题分为3个部分,这样,如果您需要对代码执行其他操作,那么它将更加灵活
import itertools
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
n = 4
def all_equal(iterable):
g = itertools.groupby(iterable)
return next(g, True) and not next(g, False)
def window(iterable, window_size):
iters = itertools.tee(iterable, window_size)
for i in range(1, window_size):
for it in iters[i:]:
next(it, None)
return zip(*iters)
def uniques_from_window(iterable, window_size):
return [sub_list[0] for sub_list in window(iterable, window_size) if all_equal(sub_list)]
print(uniques_from_window(index_max, n))
输出
[5, 16, 34, 42]
列表是否始终排序?列表是否始终排序?