如何迭代 Python 中的字母表
How to Iterate over Alphabets in Python
假设a=0,b=1....,z=25,aa=26,...等等。
我如何在 python 中形成一个列表来查找任何给定字母表的索引?
book = xlrd.open_workbook(input("Enter name of the excel file "))
table_list=list(book.sheet_names())
print("the sheets in the excel file are :")
for name in table_list:
print(name)
first_sheet = book.sheet_by_index(table_list.index(input("Enter sheet name ")))
arrayofvalues = first_sheet.col_values(152,2,239)
我需要在col_val函数中使用获取的值。
还有其他方法吗?
这是一个 base-26 转换,有一点扭曲(a
仅在最右边的位置用作零):
def col2idx(colname):
colname = colname.lower()
idx = 0
for digit in colname:
idx *= 26
idx += ord(digit) - ord('a') + 1
return idx - 1
print(col2idx('a')) # == 0
print(col2idx('b')) # == 1
print(col2idx('c')) # == 2
print(col2idx('z')) # == 25
print(col2idx('aa')) # == 26
print(col2idx('ab')) # == 27
print(col2idx('az')) # == 51
print(col2idx('aij')) # == 919
print(col2idx('amj')) # == 1023
print(col2idx('zzad')) # == 474581
假设a=0,b=1....,z=25,aa=26,...等等。
我如何在 python 中形成一个列表来查找任何给定字母表的索引?
book = xlrd.open_workbook(input("Enter name of the excel file "))
table_list=list(book.sheet_names())
print("the sheets in the excel file are :")
for name in table_list:
print(name)
first_sheet = book.sheet_by_index(table_list.index(input("Enter sheet name ")))
arrayofvalues = first_sheet.col_values(152,2,239)
我需要在col_val函数中使用获取的值。
还有其他方法吗?
这是一个 base-26 转换,有一点扭曲(a
仅在最右边的位置用作零):
def col2idx(colname):
colname = colname.lower()
idx = 0
for digit in colname:
idx *= 26
idx += ord(digit) - ord('a') + 1
return idx - 1
print(col2idx('a')) # == 0
print(col2idx('b')) # == 1
print(col2idx('c')) # == 2
print(col2idx('z')) # == 25
print(col2idx('aa')) # == 26
print(col2idx('ab')) # == 27
print(col2idx('az')) # == 51
print(col2idx('aij')) # == 919
print(col2idx('amj')) # == 1023
print(col2idx('zzad')) # == 474581