在迭代迭代器时,我想避开最终项目并停在倒数第二项 - 我该怎么做?
from itertools import product
from collections import namedtuple
param_list = []
Parameter = namedtuple("Parameter", ['bad', 'good'])
param1 = Parameter(["peanut", "gluten"], ["bacon", "pickle"])
param_list.append(param1)
param2 = Parameter([0], [1, 22])
param_list.append(param2)
param3 = Parameter([0, 1], [2, 3, 4, 9])
param_list.append(param3)
param4 = Parameter(["football"], ["running", "yoga"])
param_list.append(param4)
for prod in product(*param_list): # -- I want to skip the last product --
for sub_prod in product(*prod):
prod = [str(x) if type(x) is not str else x for x in sub_prod]
print ", ".join(prod)
注意 -
for prod in product_list[:-1] :
答案 0 :(得分:5)
为了避免使用最后一项(但不要避免拉最后一项,这一般是不可能的),你可以这样做:
str_slug
这是一个生成器,它将迭代序列并产生除最后一个项目之外的所有项目。
答案 1 :(得分:0)
基于@khelwood的答案 -
from itertools import product
from collections import namedtuple
param_list = []
Parameter = namedtuple("Parameter", ['bad', 'good'])
param1 = Parameter(["peanut", "gluten"], ["bacon", "pickle"])
param_list.append(param1)
param2 = Parameter([0], [1, 22])
param_list.append(param2)
param3 = Parameter([0, 1], [2, 3, 4, 9])
param_list.append(param3)
param4 = Parameter(["football"], ["running", "yoga"])
param_list.append(param4)
# Pulling one item ahead of loop so as to avoid the last item.
iterable = product(*param_list)
prev_prod = next(iterable)
for prod in iterable:
for sub_prod in product(*prev_prod): # Using 'prev_prod' instead of 'prod'
prod_str = [str(x) if type(x) is not str else x for x in sub_prod]
print ", ".join(prod_str)
prev_prod = prod
答案 2 :(得分:0)
使用itertools
documentation中提到的pairwise
食谱:
from itertools import tee, izip, imap
from operator import itemgetter
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
你可以定义
def init(iterable):
return imap(itemgetter(0), pairwise(iterable))
这会让你
>>> list(init(x for x in [1,2,3,4,5]))
[1, 2, 3, 4]