python 2.7 - Generating combinations in a list but only in ascending order -
i got list,
a=['z','g','b','h']
now when do,
for p in itertools.permutations(a,2):print p
i get,
('z', 'g') ('z', 'b') ('z', 'h') ('g', 'z') ('g', 'b') ('g', 'h') ('b', 'z') ('b', 'g') ('b', 'h') ('h', 'z') ('h', 'g') ('h', 'b')
i need these combinations,
('z', 'g') ('z', 'b') ('z', 'h') ('g', 'b') ('g', 'h') ('b', 'h')
and not need following. in reverse order,
('g', 'z') ('b', 'z') ('b', 'g') ('h', 'z') ('h', 'g') ('h', 'b')
can please help? thanks
late found myself :-)
just use combinations instead of permutations.
for p in itertools.combinations(a,2):print p out[1]: ('z', 'g') ('z', 'b') ('z', 'h') ('g', 'b') ('g', 'h') ('b', 'h')
Comments
Post a Comment