Python 列表中集合至少为3个连续偶数的集合总数?
如果我有一个整数列表,我想找到列表中集合的总数,其中集合至少是3个连续的偶数 例如: 列表1=[1,2,4,6,8,11,2,8] 有1个这样的集合,因为只有1个集合(2,4,6,8)至少有3个连续的偶数 我的做法:Python 列表中集合至少为3个连续偶数的集合总数?,python,list,numbers,Python,List,Numbers,如果我有一个整数列表,我想找到列表中集合的总数,其中集合至少是3个连续的偶数 例如: 列表1=[1,2,4,6,8,11,2,8] 有1个这样的集合,因为只有1个集合(2,4,6,8)至少有3个连续的偶数 我的做法: list1 = [1,2,4,6,8,11,2,8] SetofEven = 0 for i in range(0,len(list1)-2): a, b, c = list1[i:i+3] if a%2 == b%2 == c%2 == 0: Se
list1 = [1,2,4,6,8,11,2,8]
SetofEven = 0
for i in range(0,len(list1)-2):
a, b, c = list1[i:i+3]
if a%2 == b%2 == c%2 == 0:
SetofEven = SetofEven + 1
首先,我想找到只有3个连续偶数的集合,然后在此基础上进行构建。不知道如何解决问题的至少3个部分
&
是一个按位AND运算符,而不是逻辑运算符。您所寻找的是这样一种情况:
if a % 2 == b % 2 == c % 2 == 0
if a % 2 == 0 and b % 2 == 0 and c % 2 == 0
或者像这样:
if a % 2 == b % 2 == c % 2 == 0
if a % 2 == 0 and b % 2 == 0 and c % 2 == 0
与C++语言相反,在<强>逻辑中,使用<代码> &和/>代码>运算符,在Python中使用<代码>和 ./P> 编辑:至于问题的第二部分,我认为这种方法不会有成效,因为这样一个循环显示了长度为3的重叠序列。您最好使用
itertools.groupby
:
from itertools import groupby
# list of pairs (True/False, list of numbers)
groups = groupby(list1, lambda x: x % 2 == 0)
count = 0
for even, sequence in groups:
if even and len(list(sequence)) >= 3:
count += 1
groupby
根据谓词对序列进行分组,因此
>>> list1 = [1, 2, 4, 6, 8, 11, 2, 8]
>>> list(groupby(list1, lambda x: x % 2 == 0))
[(False, [1]), (True, [2, 4, 6, 8]), (False, [11]), (True, [2, 8])]
# actually the second elements in pairs are not lists but iterators, but their contents are the same
&
是一个位AND运算符,而不是逻辑运算符。您所寻找的是这样一种情况:
if a % 2 == b % 2 == c % 2 == 0
if a % 2 == 0 and b % 2 == 0 and c % 2 == 0
或者像这样:
if a % 2 == b % 2 == c % 2 == 0
if a % 2 == 0 and b % 2 == 0 and c % 2 == 0
与C++语言相反,在<强>逻辑中,使用<代码> &和/>代码>运算符,在Python中使用<代码>和 ./P> 编辑:至于问题的第二部分,我认为这种方法不会有成效,因为这样一个循环显示了长度为3的重叠序列。您最好使用
itertools.groupby
:
from itertools import groupby
# list of pairs (True/False, list of numbers)
groups = groupby(list1, lambda x: x % 2 == 0)
count = 0
for even, sequence in groups:
if even and len(list(sequence)) >= 3:
count += 1
groupby
根据谓词对序列进行分组,因此
>>> list1 = [1, 2, 4, 6, 8, 11, 2, 8]
>>> list(groupby(list1, lambda x: x % 2 == 0))
[(False, [1]), (True, [2, 4, 6, 8]), (False, [11]), (True, [2, 8])]
# actually the second elements in pairs are not lists but iterators, but their contents are the same
对于问题的第二部分,您可以使用计数器来计算列表中连续的偶数。如果数字不是偶数,您可以将计数重置为0。如果计数器为3,则您至少已连续达到3个偶数,您可以将偶数添加到您的偶数集合中 见下文:
list1 = [1,2,4,6,8,11,2,8]
SetofEven = 0
count = 0
for i in range(0, len(list1)):
if list1[i] % 2 == 0:
count += 1
else:
count = 0
# Only increment set of even count once if 3 is reached
if count == 3:
SetofEven += 1
print(SetOfEven)
# Prints 1
提取集合
如果您还需要一组数字,而不仅仅是计数,您可以通过检查计数是否大于0,从我的答案中的else
子句中获得它:
my_evens_list = []
# ...
else:
if count > 0:
my_evens_list.append(list1[i - count:i])
count = 0
# ...
对于问题的第二部分,您可以使用计数器来计算列表中连续的偶数。如果数字不是偶数,您可以将计数重置为0。如果计数器为3,则您至少已连续达到3个偶数,您可以将偶数添加到您的偶数集合中 见下文:
list1 = [1,2,4,6,8,11,2,8]
SetofEven = 0
count = 0
for i in range(0, len(list1)):
if list1[i] % 2 == 0:
count += 1
else:
count = 0
# Only increment set of even count once if 3 is reached
if count == 3:
SetofEven += 1
print(SetOfEven)
# Prints 1
提取集合
如果您还需要一组数字,而不仅仅是计数,您可以通过检查计数是否大于0,从我的答案中的else
子句中获得它:
my_evens_list = []
# ...
else:
if count > 0:
my_evens_list.append(list1[i - count:i])
count = 0
# ...
我应该如何处理问题的至少3个部分?@JapnitSethi我使用
groupby
添加了一个示例解决方案我应该如何处理问题的至少3个部分?@JapnitSethi我使用groupby
添加了一个示例解决方案