简体   繁体   English

为给定字符串的字母分配数字

[英]Assigning numbers to given string's letters

I am currently trying to finish a project which wants encode given paragraph using given matrix.我目前正在尝试完成一个想要使用给定矩阵对给定段落进行编码的项目。 I wanted to start make a letter list:我想开始制作一个字母列表:

letterlist = np.array([" ","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"])
letterlist2 = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
samplestr = "MEET ME MONDAY"

My goal is convert the letters to integer in order like A=1,B=2...Z=26 and " "=0.我的目标是将字母按顺序转换为整数,例如 A=1,B=2...Z=26 和“”=0。 Then assign them to 1x3 arrays.然后将它们分配给 1x3 数组。 like喜欢在此处输入图片说明

But I couldn't even make a progress.但我什至无法取得进展。 First I tried make for loops to match same letter in the letter list and samplestr.首先,我尝试使用 make for 循环来匹配字母列表和 samplestr 中的相同字母。 Then if they are same, give the order in the letterlist as integer.然后,如果它们相同,则将字母列表中的顺序作为整数。 But I didn't get any output.但我没有得到任何输出。

for letter in samplestr:
    for letter2 in letterlist:
        if letter2==letter:
            print("x") ## To see if I get any match

I don't know where did I wrong and how should I continue this.我不知道我哪里错了,我该如何继续。 Would making dictionary make it easier to assign letters to integers?制作字典会使将字母分配给整数更容易吗? Need some advices.需要一些建议。 Thanks for your time.谢谢你的时间。

The conversion to a number is done by converting the char to a ordinary number and then subtracting 64 because that is the starting ASCII-Index for 'A'转换为数字是通过将字符转换为普通数字然后减去 64 来完成的,因为这是 'A' 的起始 ASCII 索引

Code looks like this:代码如下所示:

from math import ceil

samplestr = "MEET ME MONDAY"

# Pad string to be dividable by 3 
samplestr = samplestr.ljust(ceil(len(samplestr)/3) * 3)
# "MEET ME MONDAY "

# Convert to number reprensentation
samplestr = [0 if c == ' ' else (ord(c)-64) for c in samplestr]
# [13, 5, 5, 20, 0, 13, 5, 0, 13, 15, 14, 4, 1, 25, 0]

# Split in chunks of 3
matrix = [samplestr[i:i+3] for i in range(0, len(samplestr), 3)]
print(matrix)

This produces the following output:这会产生以下输出:

[[13, 5, 5], [20, 0, 13], [5, 0, 13], [15, 14, 4], [1, 25, 0]]

Yes, dictionary will make it easier to assign letters to integers but if your final goal is to convert the letters to integer in order like A=1, B=2...Z=26 and " "=0, then assigning indices to the letters will also do the job.是的,字典可以更轻松地将字母分配给整数,但如果您的最终目标是将字母按顺序转换为整数,例如 A=1、B=2...Z=26 和“”=0,则将索引分配给这些信件也可以完成这项工作。

I don't have much knowledge of numpy , so I will do it simply like this:我对numpy了解不多,所以我会这样做:

letterlist2 = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
samplestr = "MEET ME MONDAY "

l = []
s = []
for i in samplestr:
    s.append(letterlist2.index(i))
    if len(s) == 3:
        l.append(s)
        s = []

if s != []:
    l.append(s)
print(l)

Output:输出:

[[13, 5, 5], [20, 0, 13], [5, 0, 13], [15, 14, 4], [1, 25, 0]]

Use a dictionary (with a single list comprehension) to convert the letters to numbers (would probably be the fastest) and then reshape to have 3 columns (-1 will take care of number of rows):使用字典(具有单个列表理解)将字母转换为数字(可能是最快的),然后重新整形为 3 列(-1 将处理行数):

convert = dict(zip(letterlist, np.arange(27)))
converted = np.array([convert[char] for char in samplestr])
#[13  5  5 20  0 13  5  0 13 15 14  4  1 25]
from math import ceil
#resize to closes upper multiple of 3
converted.resize(ceil(converted.size/3)*3)
#reshape to have 3 columns
converted = converted.reshape(-1,3)

output:输出:

[[13  5  5]
 [20  0 13]
 [ 5  0 13]
 [15 14  4]
 [ 1 25  0]]

Here is another solution with a simple dictionary mapping and list comprehensions.这是另一个具有简单字典映射和列表推导式的解决方案。 Note that you don't need to hardcode letters, it's in the standard library .请注意,您不需要对字母进行硬编码,它在标准库中

from string import ascii_uppercase

chars = " " + ascii_uppercase

encode = {char:"{}".format(i) for i, char in enumerate(chars)}

def str2num(s):
    return [[encode[char] for char in s[i:i+3]] for i in range(0, len(s), 3)]


s = "MEET ME MONDAY"
print(str2num(s))

which returns:返回:

[['13', '5', '5'],
 ['20', '0', '13'],
 ['5', '0', '13'],
 ['15', '14', '4'],
 ['1', '25']]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM