import random lst=['a','b','c'] t=['a','a','a'] def nextch(c): if c in lst: i=lst.index(c) if i==len(lst): return lst[0] else: return lst[i+1] def nextlist(l): if l: newlist = l[:-1] else: return [] if l[-1] == lst[-1]: newlist = nextlist(l[:-1]) newlist.append(lst[0]) return newlist else: newlist.append(nextch(l[-1])) return newlist r = random.randint(0,len(lst)**3-1) print 'r is %s' % r for i in range(len(lst)**3+r): print 't is %s' % t t=nextlist(t)
r is 18
t is ['a', 'a', 'a']
t is ['a', 'a', 'b']
t is ['a', 'a', 'c']
t is ['a', 'b', 'a']
t is ['a', 'b', 'b']
t is ['a', 'b', 'c']
t is ['a', 'c', 'a']
t is ['a', 'c', 'b']
t is ['a', 'c', 'c']
t is ['b', 'a', 'a']
t is ['b', 'a', 'b']
t is ['b', 'a', 'c']
t is ['b', 'b', 'a']
t is ['b', 'b', 'b']
t is ['b', 'b', 'c']
t is ['b', 'c', 'a']
t is ['b', 'c', 'b']
t is ['b', 'c', 'c']
t is ['c', 'a', 'a']
t is ['c', 'a', 'b']
t is ['c', 'a', 'c']
t is ['c', 'b', 'a']
t is ['c', 'b', 'b']
t is ['c', 'b', 'c']
t is ['c', 'c', 'a']
t is ['c', 'c', 'b']
t is ['c', 'c', 'c']
t is ['a', 'a', 'a']
t is ['a', 'a', 'b']
t is ['a', 'a', 'c']
t is ['a', 'b', 'a']
t is ['a', 'b', 'b']
t is ['a', 'b', 'c']
t is ['a', 'c', 'a']
t is ['a', 'c', 'b']
t is ['a', 'c', 'c']
t is ['b', 'a', 'a']
t is ['b', 'a', 'b']
t is ['b', 'a', 'c']
t is ['b', 'b', 'a']
t is ['b', 'b', 'b']
t is ['b', 'b', 'c']
t is ['b', 'c', 'a']
t is ['b', 'c', 'b']
t is ['b', 'c', 'c']
下一步可以做的:写一个函数,输出字符串中字符的所有排序。(比如:abc acb bac bca cab cba)