如何使用特定集合中的元素填充 python 中的列表?
How do I fill a list in python with elements from a specific set?
如果我有一组表示列表元素可以采用的值的整数和给定长度的 python 列表。
我想用所有可能的组合填充列表。
例子
list length=3
and the my_set ={1,-1}
可能的组合
[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],
[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]
我试着用 random.sample 方法随机 class
但它没有帮助。我做到了:
my_set=[1,-1]
from random import sample as sm
print sm(my_set,1) #Outputs: -1,-1,1,1 and so on..(random)
print sm(my_set,length_I_require) #Outputs**:Error
这就是 itertools.product
的用途:
>>> from itertools import product
>>> list(product({1,-1},repeat=3))
[(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]
>>>
如果你想要结果作为列表,你可以使用 map
将元组的迭代器转换为列表 if 列表(在 python3 中它 returns 一个迭代器,它作为一个更使用列表推导的有效方法):
>>> map(list,product({1,-1},repeat=3))
[[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]]
在python3中:
>>> [list(pro) for pro in product({1,-1},repeat=3)]
[[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]]
>>>
使用itertools.product()
function:
from itertools import product
result = [list(combo) for combo in product(my_set, repeat=length)]
list()
调用是可选的;如果元组而不是列表可以,那么 result = list(product(my_set, repeat=length))
就足够了。
演示:
>>> from itertools import product
>>> length = 3
>>> my_set = {1, -1}
>>> list(product(my_set, repeat=length))
[(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]
>>> [list(combo) for combo in product(my_set, repeat=length)]
[[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]]
random.sample()
为您提供给定输入序列的随机子集;它不会产生所有可能的值组合。
lst_length = 3
my_set = {1,-1}
result = [[x] for x in my_set]
for i in range(1,lst_length):
temp = []
for candidate in my_set:
for item in result:
new_item = [candidate]
new_item += item
temp.append(new_item)
result = temp
print result
如果列表长度为1,结果是一个列表,其元素等于集合。列表长度每增加一,可以通过将集合的每个元素附加到结果列表来得到结果。
如果我有一组表示列表元素可以采用的值的整数和给定长度的 python 列表。
我想用所有可能的组合填充列表。
例子
list
length=3
and themy_set ={1,-1}
可能的组合
[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],
[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]
我试着用 random.sample 方法随机 class 但它没有帮助。我做到了:
my_set=[1,-1]
from random import sample as sm
print sm(my_set,1) #Outputs: -1,-1,1,1 and so on..(random)
print sm(my_set,length_I_require) #Outputs**:Error
这就是 itertools.product
的用途:
>>> from itertools import product
>>> list(product({1,-1},repeat=3))
[(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]
>>>
如果你想要结果作为列表,你可以使用 map
将元组的迭代器转换为列表 if 列表(在 python3 中它 returns 一个迭代器,它作为一个更使用列表推导的有效方法):
>>> map(list,product({1,-1},repeat=3))
[[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]]
在python3中:
>>> [list(pro) for pro in product({1,-1},repeat=3)]
[[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]]
>>>
使用itertools.product()
function:
from itertools import product
result = [list(combo) for combo in product(my_set, repeat=length)]
list()
调用是可选的;如果元组而不是列表可以,那么 result = list(product(my_set, repeat=length))
就足够了。
演示:
>>> from itertools import product
>>> length = 3
>>> my_set = {1, -1}
>>> list(product(my_set, repeat=length))
[(1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1), (-1, 1, 1), (-1, 1, -1), (-1, -1, 1), (-1, -1, -1)]
>>> [list(combo) for combo in product(my_set, repeat=length)]
[[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]]
random.sample()
为您提供给定输入序列的随机子集;它不会产生所有可能的值组合。
lst_length = 3
my_set = {1,-1}
result = [[x] for x in my_set]
for i in range(1,lst_length):
temp = []
for candidate in my_set:
for item in result:
new_item = [candidate]
new_item += item
temp.append(new_item)
result = temp
print result
如果列表长度为1,结果是一个列表,其元素等于集合。列表长度每增加一,可以通过将集合的每个元素附加到结果列表来得到结果。