I have a simple generator to give me permutations of a set of coordinates. I wish to save each new permutation to an element in an array using the code below:
import random
def poss_comb(coord):
spin=random.shuffle
if spin:
spin(coord)
yield (coord)
...
a=[]
for n in xrange(0,10):
for item in poss_comb(coord):
print item
a.append(item)
However when printing the results printing item gives me what I want :
['0 1', '', '1 2', '1 3']
['0 1', '', '1 2', '1 3']
['1 2', '0 1', '1 3', '']
['0 1', '1 2', '', '1 3']
['1 3', '', '1 2', '0 1']
['1 3', '1 2', '0 1', '']
['0 1', '', '1 3', '1 2']
['1 2', '0 1', '', '1 3']
['1 2', '1 3', '', '0 1']
['', '1 2', '1 3', '0 1']
whereas printing list a provides an array where each element is a copy of the last permutation.
What would be a better way to do this?
coordor the locallink? - Martijn Pieters♦coord? It's not defined anywhere. - martineauposs_comb()only yields one value. - martineaua = list(poss_comb(coord))(ora.extend(poss_comb(coord))if you want to append them to an existing list instead of a new one). - abarnert