我有一个清单,例如
[True, True, True, True, False, False, True, False, True, True, False]
我想知道 True 是连续的最小计数,因此在这种情况下为1。
或者在以下列表中,它是2,
[True, True, False, True, True, False, False, False, False, True, True, True]
感谢您的帮助!
答案 0 :(得分:0)
这是我的(幼稚)解决方案:
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
NC=$(tput sgr0)
输出:
list_1 = [True, True, True, True, False, False, True, False, True, True, False]
list_2 = [True, True, False, True, True, False, False, False, False, True, True, True]
list_3 = [False, False, False]
list_4 = [True, True, True]
list_5 = [False, False, False, True, True]
def min_successive_val_count(list_of_vals):
list_of_runs = []
counter = 0
for val in list_of_vals:
if val:
counter+=1
else:
if counter != 0:
list_of_runs.append(counter)
counter = 0
if list_of_runs:
return min(list_of_runs)
elif counter == len(list_of_vals):
return counter
elif list_of_vals[-1]:
return counter
else:
return "No True's in your list"
print(min_successive_val_count(list_1))
print(min_successive_val_count(list_2))
print(min_successive_val_count(list_3))
print(min_successive_val_count(list_4))
print(min_successive_val_count(list_5))
祝你好运(希望我的最后一次编辑;)
答案 1 :(得分:0)
来自Python: A program to find the LENGTH of the longest run in a given list?,但最多 - >分钟。
Entry
答案 2 :(得分:0)
让我们在遍历此列表时使用字典记录我们的真实计数器。
list = [True, True, False, True, True, False, False, False, False, True, True, True]
currentCounter = 0
record = []
def getMin (list):
for x in list:
if x:
currentCounter[0] = currentCounter[0]+1
else:
record.append(currentCounter)
currentCounter = 0
return secondSmallest(record)
#we need to get the second smallet number because always the smallest is equal 0
def secondSmallest(numbers):
m1, m2 = float('inf'), float('inf')
for x in numbers:
if x <= m1:
m1, m2 = x, m1
elif x < m2:
m2 = x
return m2