2017-08-07 1 views
0

ここに私がしようとしていることがあります。forループの対象リストとして未知数の参照のリストを展開します

from itertools import product 

li = [] 
# Append arbitrary/unknown number of integers to li 

ranges = [range(1, n) for n in li] 

# Question 1: How to create a list of *references* whose length is that of li (or ranges) 
# Is this the right way? 

target_list = [None, ] * len(ranges) 

# Question 2: Unpack this target_list of references in a for-loop, How to? 
# Something like so (of course this does not work, just trying to convey intent) 

for *target_list in product(ranges): 
    # Access specific elements of target_list 

私は私がやっている何の意図が明確でない場合は知っているか、私はどのような方法でそれより良い単語可能であれば聞かせて下さい。

ありがとうございます。

答えて

0

私はtarget_listを作成してfor-loopで "unpack"する必要はありませんでした。私がやりたいことを達成するために

、私がしなければならなかったすべてがそうのようproductに引数を展開した。

from itertools import product 

li = [] 
# Append arbitrary/unknown number of integers to li 

ranges = [range(1, n) for n in li] 

for target_list in product(*ranges): # Note ranges is unpacked 
    # Access specific elements of target_list 
関連する問題