使用python中的列表简化代码

时间:2016-06-08 23:50:37

标签: python

我有一行python代码,基本上将一个整数与一个字符串(一个字母)对齐。由于我使用if语句,它需要很多代码行...有没有一种方法可以使用列表简化我的代码,这样它就不会占用63行代码?这是我的代码:

if character in " ":
    character = 0
elif character in "Aa":
    character = 1
elif character in "Bb":
    character = 2
elif character in "Cc":
    character = 3
elif character in "Dd":
    character = 4
elif character in "Ee":
    character = 5
elif character in "Ff":
    character = 6
elif character in "Gg":
    character = 7
elif character in "Hh":
    character = 8
elif character in "Ii":
    character = 9
elif character in "Jj":
    character = 10
elif character in "Kk":
    character = 11
elif character in "Ll":
    character = 12
elif character in "Mm":
    character = 13
elif character in "Nn":
    character = 14
elif character in "Oo":
    character = 15
elif character in "Pp":
    character = 16
elif character in "Qq":
    character = 17
elif character in "Rr":
    character = 18
elif character in "Ss":
    character = 19
elif character in "Tt":
    character = 20
elif character in "Uu":
    character = 21
elif character in "Vv":
    character = 22
elif character in "Ww":
    character = 23
elif character in "Xx":
    character = 24
elif character in "Yy":
    character = 25
elif character in "Zz":
    character = 26
elif character in ".":
    character = 27
elif character in ",":
    character = 28
elif character in "?":
    character = 29
elif character in "'":
    character = 30
else:
    character = 0

6 个答案:

答案 0 :(得分:3)

将所有角色放在一个大字符串中,其中包含字符'该字符串中的位置是您要分配的值。您可以通过首先将输入转换为小写来处理大写和小写字母。

value = " abcdefghijklmnopqrstuvwxyz.,?'".find(character.lower())

答案 1 :(得分:1)

str.lower 中使用 dict

d = {" ":0, "a": 1, "b": 2, ......,"'":30}

character = d.get(character.lower(), 0)

如果字符不在dict中,则默认值为0,但我没有看到将空格设置为0并将其作为默认值的逻辑

根据@ user23571152评论创建dict:

d = {c: i for i, c in enumerate(" " + string.ascii_lowercase + ".,?'")}

答案 2 :(得分:1)

没有理由手工编写所有这些字母a-z ... 如果它是alpha,则从ord中减去97,否则从dict中减去.get(),如果它不在dict中则默认为0

specialChars = {" " : 0, "." : 27, "," : 28, "?" : 29, "'" : 30}
char = "'"
if char.isalpha():
    print(ord(char.lower())-ord('a')+1)
else:
    print(specialChars.get(char, 0))

这会对每个字符的ord进行两次调用,我不知道你的实际函数是如何工作的,但理想情况下你只需要计算一次ord('a')+1并使用每个字符。或者你可以使用print(ord(char.lower())-96)如果你对幻数

没问题

答案 3 :(得分:1)

您可以通过使用ord()函数设置ASCII值来设置范围,然后使用index()函数查找索引。

TestCase='T'

# Make all Input to lowercase.

TestCase=TestCase.lower()


#calculate ASCII value of all lower character String

RangeVariable=range(ord('a'),ord('z'))

#Now Returning required Index value by comparing ASCII value of Testcase with that in RangVariable


print(RangeVariable.index(ord(TestCase)))

答案 4 :(得分:0)

这是基本想法:

character = ord(character.lower()) - ord('a') + 1 if character != ' ' else 0

您可以根据输入的特定要求进行调整。由于您未指定输入值的可接受域,因此我假设输入仅限于示例中的内容。

答案 5 :(得分:0)

我会像dict这样使用:

In [0]: import string

In [1]: keys = list(" ") + list(string.ascii_lowercase)

In [2]: dict = {}

In [3]: for v, k in enumerate(keys): dict[k] = v

In [4]: def getCharVal(character): return dict.get(character.lower(), -1)

In [5]: getCharVal("B")
Out[5]: 2