python:如何为字母赋值?

时间:2022-05-26 21:43:11

I want to assign a value to each letter in the alphabet, so that a -> 1, b -> 2, c -> 3, ... z -> 26. Something like a function which returns the value of the letter, for example:

我想为字母表中的每个字母赋一个值,这样一个 - > 1,b - > 2,c - > 3,... z - > 26.像函数一样返回字母的值,例如:

value('a') = 1

值('a')= 1

value('b') = 2

值('b')= 2

etc...

How would I go about doing this in python?

我将如何在python中执行此操作?

8 个解决方案

#1


6  

You want a native python dictionary.

你想要一个原生的python字典。

(and you probably also want your values to start from"0" not from "1" , so you can void adding a +1 on all your mappings, as bellow)

(并且您可能还希望您的值从“0”开始而不是从“1”开始,因此您可以无效地在所有映射上添加+1,如下所示)

Build one with this:

用这个构建一个:

import string
values = dict()
for index, letter in enumerate(string.ascii_lowercase):
   values[letter] = index + 1

This give syou things like:

这给了这样的东西:

print values["a"]
-> 1

Of course, you probably could use the "ord" built-in function and skip this dictionary altogether, as in the other answers:

当然,你可能可以使用“ord”内置函数并完全跳过这个字典,就像在其他答案中一样:

print ord("c") - (ord("a")) + 1

Or in python 3.x or 2.7, you can create the dicionary in a single pass with a dict generator expression:

或者在python 3.x或2.7中,您可以使用dict生成器表达式在单个传递中创建dicionary:

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}

#2


3  

If you just want to map characters of the ASCII alphabet to numbers, you can use ord() and then adjust the result:

如果您只想将ASCII字母的字符映射到数字,可以使用ord()然后调整结果:

>>> ord('a') - 96
1

If you want this to work for uppercase letters too:

如果您希望它也适用于大写字母:

>>> ord('A'.lower()) - 96
1

Also, you might want to validate that the argument is indeed a single ASCII character:

此外,您可能希望验证参数确实是单个ASCII字符:

>>> char = 'a'
>>> len(char) == 1 and char.isalpha() and 'a' <= char <= 'z'
True

Or:

>>> import string
>>> len(char) == 1 and char in string.ascii_lowercase
True

#3


2  

Use a dictionary for key:value pairs. Although for a simple mapping like this there are probably some clever ways of doing this.

使用字典表示键:值对。虽然对于像这样的简单映射,可能有一些聪明的方法来做到这一点。

#4


2  

def value(letter):
    return ord(letter) - ord('a') + 1

#5


2  

from itertools import count
from string import lowercase
value = dict(zip(lowercase, count(1))).get

#6


0  

You should exploit the fact that 'a', 'b', etc. have ASCII values behind them.

您应该利用“a”,“b”等具有ASCII值的事实。

ord('a') # this returns int 97
ord('b') # this returns int 98

Therefore, you could do something like:

因此,您可以执行以下操作:

ord(letter) - ord('a') + 1 # e.g., a: 97 - 97 + 1 = 1

#7


0  

Why not just make a list of each letter in the alphabet and then use the index values as the return value

为什么不在字母表中列出每个字母,然后使用索引值作为返回值

#8


0  

How about this?

这个怎么样?

import string
value = lambda x: string.ascii_lowercase.index(x) + 1

In [1]: value("a")
Out[1]: 1

#1


6  

You want a native python dictionary.

你想要一个原生的python字典。

(and you probably also want your values to start from"0" not from "1" , so you can void adding a +1 on all your mappings, as bellow)

(并且您可能还希望您的值从“0”开始而不是从“1”开始,因此您可以无效地在所有映射上添加+1,如下所示)

Build one with this:

用这个构建一个:

import string
values = dict()
for index, letter in enumerate(string.ascii_lowercase):
   values[letter] = index + 1

This give syou things like:

这给了这样的东西:

print values["a"]
-> 1

Of course, you probably could use the "ord" built-in function and skip this dictionary altogether, as in the other answers:

当然,你可能可以使用“ord”内置函数并完全跳过这个字典,就像在其他答案中一样:

print ord("c") - (ord("a")) + 1

Or in python 3.x or 2.7, you can create the dicionary in a single pass with a dict generator expression:

或者在python 3.x或2.7中,您可以使用dict生成器表达式在单个传递中创建dicionary:

values = {chr(i): i + 1 for i in range(ord("a"), ord("a") + 26)}

#2


3  

If you just want to map characters of the ASCII alphabet to numbers, you can use ord() and then adjust the result:

如果您只想将ASCII字母的字符映射到数字,可以使用ord()然后调整结果:

>>> ord('a') - 96
1

If you want this to work for uppercase letters too:

如果您希望它也适用于大写字母:

>>> ord('A'.lower()) - 96
1

Also, you might want to validate that the argument is indeed a single ASCII character:

此外,您可能希望验证参数确实是单个ASCII字符:

>>> char = 'a'
>>> len(char) == 1 and char.isalpha() and 'a' <= char <= 'z'
True

Or:

>>> import string
>>> len(char) == 1 and char in string.ascii_lowercase
True

#3


2  

Use a dictionary for key:value pairs. Although for a simple mapping like this there are probably some clever ways of doing this.

使用字典表示键:值对。虽然对于像这样的简单映射,可能有一些聪明的方法来做到这一点。

#4


2  

def value(letter):
    return ord(letter) - ord('a') + 1

#5


2  

from itertools import count
from string import lowercase
value = dict(zip(lowercase, count(1))).get

#6


0  

You should exploit the fact that 'a', 'b', etc. have ASCII values behind them.

您应该利用“a”,“b”等具有ASCII值的事实。

ord('a') # this returns int 97
ord('b') # this returns int 98

Therefore, you could do something like:

因此,您可以执行以下操作:

ord(letter) - ord('a') + 1 # e.g., a: 97 - 97 + 1 = 1

#7


0  

Why not just make a list of each letter in the alphabet and then use the index values as the return value

为什么不在字母表中列出每个字母,然后使用索引值作为返回值

#8


0  

How about this?

这个怎么样?

import string
value = lambda x: string.ascii_lowercase.index(x) + 1

In [1]: value("a")
Out[1]: 1