根据值从长列表中派生子列表

2024-10-03 09:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的清单

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]

我想从上面的列表中提取更多的数字集。。你知道吗

高\u数字>;10 低数字<;10

main_list= [<few_high_numbers_1>,<few_low_numbers_1>,<few_high_numbers_2><few_low_numbers_2><few_high_n‌​umbers_3><few_low_numbers_3>]..

我想从中提取列表

list1 = <few_high_no_1> 
list2= <few_high_no_2> 
list3 = <few_high_no_3>

等等等等

如果我不清楚就告诉我。你知道吗

谢谢你


Tags: noltgt列表main数字listlow
2条回答
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

这里有一些工作代码,据说注释已经足够多了。你知道吗

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),也试试其他列表或阈值。你知道吗

请注意,您的第一个列表包含一个额外的逗号,用于中断解析。你知道吗

相关问题 更多 >