我如何制作一个函数,通过 2 个特定步骤 (Python) 打印列表的值
How can i make a function which will print the values of the list through 2 specific steps(Python)
我有一个函数,它将接受 3 个参数:list、step1 和 step2。我需要编写一个生成器函数,它将在每次迭代时逐步打印 1 个值。这是一个例子
a = [1, 2, 3, 4, 5, 6, 7] # ordinary list
b = generate_step(a, 1, 2) # this is the function with this list and two steps.
next(b) # I need to print all the iterations
next(b)
next(b)
next(b)
输出将是
1
3
4
6 # I mean the steps alternate
7
我希望我能解释一下。所以,现在这是我的代码
def generate_step(lst, step1, step2):
l1 = []
i = 0
while i < len(lst):
for_step1 = lst[::step1]
l1.append(for_step1)
i += 1
for_step2 = lst[::step2]
l1.append(for_step2)
i += 1
for j in range(0, len(l1)):
yield l1[j]
a = [1, 2, 3, 4, 5, 6, 7]
b = generate_step(a, 2, 3)
while True:
try:
print(next(b))
except StopIteration:
pass
break
输出是
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
这不是任务要求的...请帮我解决这个问题。我不知道如何根据条件交替显示步骤以及如何显示一个数字而不是整个列表。
这是一种基于 itertools
的方法:
from itertools import chain, cycle, compress
def generate_step(a, *steps):
selections = chain.from_iterable(
[0] * (step - 1) + [1] for step in cycle(steps)
)
return compress(a, selections)
示例:
>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> list(generate_step(a, 1, 2))
[1, 3, 4, 6, 7]
代码首先创建一个“选择”的可迭代对象,它为每个应该跳过的元素生成一个零,为每个应该选择的元素生成一个 1。然后将这些选择传递给 itertools.compress()
——该库中使用频率较低的函数之一。
该函数支持任意数量的步骤和循环。
如果您想从头开始编写函数,而不使用 itertools
,一种可能的方法是:
def generate_step(a, *steps):
a = iter(a)
while True:
for step in steps:
for dummy in range(step):
try:
x = next(a)
except StopIteration:
return
yield x
我有一个函数,它将接受 3 个参数:list、step1 和 step2。我需要编写一个生成器函数,它将在每次迭代时逐步打印 1 个值。这是一个例子
a = [1, 2, 3, 4, 5, 6, 7] # ordinary list
b = generate_step(a, 1, 2) # this is the function with this list and two steps.
next(b) # I need to print all the iterations
next(b)
next(b)
next(b)
输出将是
1
3
4
6 # I mean the steps alternate
7
我希望我能解释一下。所以,现在这是我的代码
def generate_step(lst, step1, step2):
l1 = []
i = 0
while i < len(lst):
for_step1 = lst[::step1]
l1.append(for_step1)
i += 1
for_step2 = lst[::step2]
l1.append(for_step2)
i += 1
for j in range(0, len(l1)):
yield l1[j]
a = [1, 2, 3, 4, 5, 6, 7]
b = generate_step(a, 2, 3)
while True:
try:
print(next(b))
except StopIteration:
pass
break
输出是
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
[1, 3, 5, 7]
[1, 4, 7]
这不是任务要求的...请帮我解决这个问题。我不知道如何根据条件交替显示步骤以及如何显示一个数字而不是整个列表。
这是一种基于 itertools
的方法:
from itertools import chain, cycle, compress
def generate_step(a, *steps):
selections = chain.from_iterable(
[0] * (step - 1) + [1] for step in cycle(steps)
)
return compress(a, selections)
示例:
>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> list(generate_step(a, 1, 2))
[1, 3, 4, 6, 7]
代码首先创建一个“选择”的可迭代对象,它为每个应该跳过的元素生成一个零,为每个应该选择的元素生成一个 1。然后将这些选择传递给 itertools.compress()
——该库中使用频率较低的函数之一。
该函数支持任意数量的步骤和循环。
如果您想从头开始编写函数,而不使用 itertools
,一种可能的方法是:
def generate_step(a, *steps):
a = iter(a)
while True:
for step in steps:
for dummy in range(step):
try:
x = next(a)
except StopIteration:
return
yield x