简体   繁体   English

如何在这个问题中迭代。实际上我发现从 A 到 Z 我们会收到索引 1-26,从 AA 到 ZZ 是 26+26^2。但是如何进行?

[英]HOW TO ITERATE IN THIS QUESTION.ACTUALLY I FOUND THAT FROM A to Z WE WOULD RECIEVE INDEX 1-26 AND FROM AA TO ZZ IT IS AS 26+26^2.BUT HOW TO PROCEED?

In spreadsheets, columns are labeled as follows:在电子表格中,列的标签如下:

Label           Number
A               1
...             ...
Z               26
AA              27
...             ...
AZ              52
BA              53
...             ...
ZZ              702
AAA             703
...             ...
AAZ             728
ABA             729

A is the first column, B is the second column, Z is the 26th column, and so on. A 是第一列,B 是第二列,Z 是第 26 列,依此类推。 The three dots represent the missing labels and their column numbers.三个点代表缺失的标签及其列号。 Using the table given above, deduce the mapping between column labels and their corresponding numbers.使用上面给出的表格,推断列标签与其对应编号之间的映射。 Accept the column label as input and print the corresponding column number as output.接受列标签作为输入并打印相应的列号作为输出。

I started as:我开始是:

   n=input()
   alpha='ABCDEFGHIJKLMNOPQRSTUVWXYZ'


   if len(n)==1:
       print(alpha.index(n.upper())+1)
   elif len(n)==2

but I know it won't work.但我知道这行不通。

First, we want to write a function which gives the position of a letter (which you already did more or less).首先,我们想写一个函数来给出一个字母的位置(你已经或多或少做了)。

alpha='ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def position(letter):
    return alpha.index(letter.upper())+1

You then have 2 cases:那么你有两种情况:

  • either the column input has one letter, so you just return the position of the letter in the alphabet要么列输入有一个字母,所以你只需返回字母在字母表中的位置
  • either the input has 2 letters, in which case the column number is position(letter1) * 26 + position(letter2)要么输入有 2 个字母,在这种情况下,列号是 position(letter1) * 26 + position(letter2)

This gives us the following program:这给了我们以下程序:

def column_number(label):
    if len(label) == 1:
        return position(label)
    elif len(label) == 2:
        return 26*position(label[0]) + position(label[1])
    else:
        print("incorrect label format")

Since we have to continue as many times as the input requires, it's ideal to make use of recursion:由于我们必须根据输入的需要继续多次,因此使用递归是理想的:

alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# retrieve position of a letter in the alphabet set
def position(letter):
  pos = alpha.index(letter) + 1
  return pos

# function to recursively find the column number  
def column_number(label, n):
  if n==1:
    return position(label)

  else:
    return ((26**(n-1)) * position(label[0])) + column_number(label[1:], n-1)

# main code
label = input().upper()
n = len(label)

col_n = column_number(label, n)
print(col_n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何生成从aa…到zz的字符串范围 - How to generate a range of strings from aa… to zz 如何删除除26个字母之外的所有字母,以及。 ,()'“?!来自Python中的字符串? - How to remove all but the 26 letters, and . , ( ) ' " ? ! from a string in Python? 如何仅从文本文件中读取第26列? - How do I read only the 26th column from a textfile? 对于Kasiski测试,如何在Python中实现26x26表格 - For Kasiski Test,How to implement 26x26 table in Python CNN - 检测手写笑脸:ValueError:无法将输入数组从形状 (26,26,3) 广播到形状 (26) - CNN - Detecting Handwritten Smilies: ValueError: could not broadcast input array from shape (26,26,3) into shape (26) 我如何在 0 到 26 之间更快地标准化像素 - how do i normalize pixels faster between 0 and 26 找不到python26.dll - python26.dll was not found 如何编写从给定文本返回 ("26", "jan", "2014") 的正则表达式 - How to write a regex expression that returns ("26", "jan", "2014") from a given text 如何将日期从 26-MAY-20 03.43.48.861000000 重新格式化为 'yyyy-mm-dd hh:mm:ss'? - How to reformat date from 26-MAY-20 03.43.48.861000000 to 'yyyy-mm-dd hh:mm:ss'? IndexError:索引 26 超出轴 0 的范围,大小为 26 - IndexError: index 26 is out of bounds for axis 0 with size 26
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM