给定 n 和一个可迭代对象,我如何 return 一组 n 个连续整数?
How do I return a set of n consecutive integers given n and an iterable?
例如调用consec(2, [2, 2, 3, 9, 9, 10, 1, 6, 6])
returns{2, 9, 6}
。我不能在 itertools
中使用任何东西,我只能使用 next
和 iter
并且我不能创建辅助列表来跟踪值或使用 len.
这是我目前的情况:
consec(n,iterable):
result = set()
count = 0
y = iter(iterable)
j = iter(iterable)
next(j)
try:
while True:
for x in y:
if x in result:
continue
for m in range(n):
if x == next(j):
count += 1
if count == n-1:
result.add(x)
else:
count = 0
break
except StopIteration:
return result
递归执行:
GlobalCons = 2
def recurse(list, previous, consecCount):
if(list == []):
return []
if(consecCount != GlobalCons and previous == list[0]):
return recurse(list[1:], list[0], consecCount + 1)
if(consecCount == GlobalCons previous == list[0]):
return [previous] + recurse(list[1:], list[0], consecCount + 1)
else:
return recurse(list[1:], list[0], 1)
您可以使用 itertools.groupby
:
def consec(n, iterable):
return [k for k, g in itertools.groupby(iterable) if len(list(g)) == n]
示例:
>>> consec(2, [2, 2, 3, 9, 9, 10, 1, 6, 6])
[2, 9, 6]
编辑:哎呀,我想我把 "I cannot use anything in itertools" 读成了 "I found nothing in itertools that I can use",但你可能指的是 "I am not allowed to"。不管怎样,我会把这个留给那些可以使用来自itertools
...
的人
通过对可迭代对象中的值进行简单的 for
循环,应该很容易做到这一点。只需计算您连续看到当前值的次数,当它达到 n
时,将其添加到结果中:
def consec(n, iterable):
result = set()
prev = None # use object() here instead if None could be a value in the iterable
count = 0
for item in iterable:
if item == prev:
count += 1
if count == n:
result.add(prev)
else:
prev = item
count = 1
return result
我不想避免重复计算相同的值,因为 set
会忽略添加到它的任何重复值。
如果您出于某种原因需要使用 while
循环,您可以随时将 for
循环转换为一个循环。
for
循环:
for item in iterable:
stuff(item)
几乎等同于:
it = iter(iterable)
try:
while True:
item = next(it)
stuff(item)
except StopIteration:
del it
两个版本之间的唯一区别是在迭代过程中存在 it
变量。 (for
循环使用的迭代器从不绑定到变量名。)
def consec(n,l):
return set(ele for i,ele in enumerate(l[:-1]) if all(ele == j for j in l[i:i+n]))
或者使用范围和索引代替切片:
def consec(n,l):
return set(ele for i, ele in enumerate(l[:-n+1]) if all(ele == l[j] for j in range(i,i+n)))
如果必须使用 next,请使用 n-1 范围内的内部循环,查看所有 next n - 1 和 prev 是否相等,如果循环完成则添加到集合中:
def consec(n,l):
it = iter(l)
prev = next(it)
st = set()
while prev != "":
for i in range(n-1):
ele = next(it,"")
if ele != prev or ele == "":
break
prev = ele
else:
st.add(ele)
prev = ele
return st
例如调用consec(2, [2, 2, 3, 9, 9, 10, 1, 6, 6])
returns{2, 9, 6}
。我不能在 itertools
中使用任何东西,我只能使用 next
和 iter
并且我不能创建辅助列表来跟踪值或使用 len.
这是我目前的情况:
consec(n,iterable):
result = set()
count = 0
y = iter(iterable)
j = iter(iterable)
next(j)
try:
while True:
for x in y:
if x in result:
continue
for m in range(n):
if x == next(j):
count += 1
if count == n-1:
result.add(x)
else:
count = 0
break
except StopIteration:
return result
递归执行:
GlobalCons = 2
def recurse(list, previous, consecCount):
if(list == []):
return []
if(consecCount != GlobalCons and previous == list[0]):
return recurse(list[1:], list[0], consecCount + 1)
if(consecCount == GlobalCons previous == list[0]):
return [previous] + recurse(list[1:], list[0], consecCount + 1)
else:
return recurse(list[1:], list[0], 1)
您可以使用 itertools.groupby
:
def consec(n, iterable):
return [k for k, g in itertools.groupby(iterable) if len(list(g)) == n]
示例:
>>> consec(2, [2, 2, 3, 9, 9, 10, 1, 6, 6])
[2, 9, 6]
编辑:哎呀,我想我把 "I cannot use anything in itertools" 读成了 "I found nothing in itertools that I can use",但你可能指的是 "I am not allowed to"。不管怎样,我会把这个留给那些可以使用来自itertools
...
通过对可迭代对象中的值进行简单的 for
循环,应该很容易做到这一点。只需计算您连续看到当前值的次数,当它达到 n
时,将其添加到结果中:
def consec(n, iterable):
result = set()
prev = None # use object() here instead if None could be a value in the iterable
count = 0
for item in iterable:
if item == prev:
count += 1
if count == n:
result.add(prev)
else:
prev = item
count = 1
return result
我不想避免重复计算相同的值,因为 set
会忽略添加到它的任何重复值。
如果您出于某种原因需要使用 while
循环,您可以随时将 for
循环转换为一个循环。
for
循环:
for item in iterable:
stuff(item)
几乎等同于:
it = iter(iterable)
try:
while True:
item = next(it)
stuff(item)
except StopIteration:
del it
两个版本之间的唯一区别是在迭代过程中存在 it
变量。 (for
循环使用的迭代器从不绑定到变量名。)
def consec(n,l):
return set(ele for i,ele in enumerate(l[:-1]) if all(ele == j for j in l[i:i+n]))
或者使用范围和索引代替切片:
def consec(n,l):
return set(ele for i, ele in enumerate(l[:-n+1]) if all(ele == l[j] for j in range(i,i+n)))
如果必须使用 next,请使用 n-1 范围内的内部循环,查看所有 next n - 1 和 prev 是否相等,如果循环完成则添加到集合中:
def consec(n,l):
it = iter(l)
prev = next(it)
st = set()
while prev != "":
for i in range(n-1):
ele = next(it,"")
if ele != prev or ele == "":
break
prev = ele
else:
st.add(ele)
prev = ele
return st