-1
from itertools import combinations
f = open('snc.csv', 'w')
for comb in combinations(range('1','20'), 5):
    f.write(str(comb))
    f.write('\n')
f.close()

My scripts works with numbers, i want it work with characters too from a to f, in range of 4, no repetition, no shuffling, like so

A,b,c,d,e

A,b,c,d,f

A,b,c,e,f

And so one, it always starts with a, if a doesn't exist in the range of 4, then it stats with b, order is important, so much like the script above

3
  • I'm marking this as off-topic since it is a pure programming question and has nothing to do with Ubuntu.
    – muru
    Oct 22, 2014 at 3:45
  • @muru note taken, won't happen again, how about migrating it to SO or programming.SE or something like that then?
    – Lynob
    Oct 22, 2014 at 9:54
  • You could flag this question for moderator attention asking for migration.
    – muru
    Oct 22, 2014 at 12:01

1 Answer 1

1
from itertools import combinations
f = open('snc.csv', 'w')
for comb in combinations(range(26), 4):
    f.write("%s,%s,%s,%s\n" % tuple([chr(x + ord('a')) for x in comb]))
f.close()

EDIT This one will also work and is probably a little easier to read:

from string import ascii_lowercase
from itertools import combinations
f = open('snc.csv', 'w')
for comb in combinations(ascii_lowercase, 4):
    f.write("%s,%s,%s,%s\n" % comb)
f.close()
3
  • the first snippet outputs Traceback (most recent call last): File "charcom.py", line 4, in <module> f.write("%s,%s,%s,%s\n" % tuple([chr(x + ord('a')) for x in comb])) TypeError: not all arguments converted during string formatting
    – Lynob
    Oct 22, 2014 at 9:51
  • the second one outputs strings that contains i and m and all the characters, how to limit it to just from a to f?
    – Lynob
    Oct 22, 2014 at 9:52
  • replace combinations(ascii_lowercase, 4) with combinations('abcdef', 4)
    – sмurf
    Oct 22, 2014 at 20:57

Not the answer you're looking for? Browse other questions tagged .