Skip to content

Instantly share code, notes, and snippets.

@ipconfiger
Created October 6, 2012 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipconfiger/3844286 to your computer and use it in GitHub Desktop.
Save ipconfiger/3844286 to your computer and use it in GitHub Desktop.
如何实现生成一个1-N位的字符串的列表
class WordsArr(object):
def __init__(self,col_count):
self.total = 26**col_count
def __getitem__(self,idx):
if idx>self.total-1:
raise IndexError("index out of range")
nums = []
lst = idx%26
while idx/26:
if (idx/26)>25:
nums.append(25)
else:
nums.append(idx/26-1)
idx=idx/26
nums.append(lst)
return "".join([chr(97+i) for i in nums])
def __getslice__(self,start,end):
for idx in xrange(start,end):
yield self[idx]
if __name__ == "__main__":
arr = WordsArr(5)
for w in arr[23:200]:
print w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment