我有一个如下列表,
list = [0,0,0,0,2,1,4,1,432,431,1,4,43,423,,45,54534,0,665,765,5687,89870,890,98089,6,0,1,5,7,3,6,7,8,3,4,4,7,543,6554,765,768,87234,765876,0,897,987,0,4,7,4,6,8,0,0,0,0]
从上面的列表中,我想根据高(> 10)值的密度提取子列表。 如果较高的数字之间存在一个或两个零,则它可以包含在更高的数字集
中我想打印,
op1 = [2,1,4,1,432,431,1,4,43,423,,45,54534,0,665,765,5687,89870,890,98089,6,0]
op2 = [7,543,6554,765,768,87234,765876,0,897,987,0,4,7,4,6,8]
我想从上面的列表中提取更高的数字集。
high_numbers> 10 lownumbers< 10
main_list= [<few_high_numbers_1>,<few_low_numbers_1>,<few_high_numbers_2><few_low_numbers_2><few_high_numbers_3><few_low_numbers_3>]..
从此我想提取列表
list1 = <few_high_no_1>
list2= <few_high_no_2>
list3 = <few_high_no_3>
等等
如果我不清楚,请告诉我。
谢谢,
答案 0 :(得分:1)
lists = []
low = True
for n in main_list:
if n > 10:
if not low:
lists[-1].append(n)
else:
lists.append([n])
low = False
else:
low = True
答案 1 :(得分:1)
这里有一些工作代码,据说评论得足够重。
sample = [0,0,0,0,2,1,4,1,
432,431,1,4,43,423,45,54534,0,665,765,5687,89870,890,98089,
6,0,1,5,7,3,6,7,8,3,4,4,7,
543,6554,765,768,87234,765876,0,897,987,
0,4,7,4,6,8,0,0,0,0]
def findStreak(data, initial_index, threshold=10, allowed_fluctuation_length=2):
"""Looks for a streak with values all below or all above threshold.
* data: the list to scan.
* initial_index: where to start looking.
* thresold: a value that determines the upper/lower limit of the streak.
* allowed_fluctuation_length: how many values may be beyond threshold
without breaking the streak.
Returns a tuple (start, end), so that data[start : end] is the streak,
or None if initial_index is out of range.
"""
if 0 <= initial_index < len(data):
# determine what kind of streak we want to see
if data[initial_index] > threshold:
belongsToStreak = lambda x: x > threshold # high streak
else:
belongsToStreak = lambda x: x <= threshold # low streak
# scan forward as long as predicate holds
last_good_index = initial_index # where the streak still has not ended
index = initial_index + 1
fluctuation_length = 0 # how many values did not satisfy the predicate
while index < len(data):
if not belongsToStreak(data[index]):
fluctuation_length += 1
if fluctuation_length > allowed_fluctuation_length:
# it was not a fluctuation, it's another streak beginning!
return (initial_index, last_good_index + 1)
else:
last_good_index = index # position under index belongs to the streak
index += 1 # advance
# we ran out of data, but the streak did not seem to end
return (initial_index, last_good_index + 1)
# here initial_index is out of range
# we could omit this; if a function does not return something explicitly,
# it implicitly returns None.
return None
def printStreaks(data=sample):
initial_index = 0
while True:
streak_span = findStreak(data, initial_index)
if not streak_span:
break
initial_index, end_index = streak_span # unpack the tuple
print "Streak is %r, data is %r" % (
streak_span,
data[initial_index : end_index])
initial_index = end_index # advance to next streak
现在尝试printStreaks(sample)
,也尝试使用其他列表或阈值。
请注意,您的第一个列表包含一个额外的逗号,可以解析解析。