Created
October 6, 2012 07:16
-
-
Save ipconfiger/3844286 to your computer and use it in GitHub Desktop.
如何实现生成一个1-N位的字符串的列表
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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