将列表中的字母转换为数字,具体取决于python中的另一个列表

时间:2018-11-25 12:59:32

标签: python list encryption

我想根据列表字符和数字将列表单词的每个字母转换为数字。因此a = 0,b = 02300,c = 2。 我想要这个输出:

encoding = [34, 9, 432, 432, 104, 124546324, 104693, 104, 432, 5]

ps:如果每个数字之间没有空格,则不会导入。

word = ["Hello world"]
    encoding = []
    charachter = ["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",""]
    number = [0,02300,2,5,9,7,10,34,876,23,125,432,567,103,104,10234,102435,332,7654,12435,65434,12121,104693,130694,120357,12346,124546324]

我不知道要做什么,因为列表编号的数字不等于列表字符中字母的索引。

PS:我希望不会再有这样的话题,因为我没有找到

4 个答案:

答案 0 :(得分:2)

您的帖子中包含很多违规行为,例如缺少字符,非法整数,缺少映射以及对空字符串的特殊翻译-因此,我将总体上回答。

您想要的(我从“如果每个数字之间没有空格,则不会导入”中收集到的东西)是一个将字符映射到其翻译的翻译表。您可以通过将字符到字符串的映射传递到str.maketrans来获得它。

>>> char_to_number = {'a': '0', 'b': '02300', 'c': '2'} # ... and so on
>>> translator = str.maketrans(char_to_number)
>>> plain = 'abcabc'
>>> 
>>> plain.translate(translator)
'00230020023002'

如果您确实想要列表,请使用

>>> [char_to_number[c] for c in plain]
['0', '02300', '2', '0', '02300', '2']

答案 1 :(得分:1)

您可以使用Here is working example of your code创建查找字典。

word = "Hello world"
encoding = []
charachter = ["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",""]
number = [0,2300,2,5,9,7,10,34,876,23,125,432,567,103,104,10234,102435,332,7654,12435,
          65434,12121,104693,130694,120357,12346,124546324]

mapping = {k:v for k,v in zip(charachter,number)}  # or dict(zip(...))

enc = [mapping.get(c, c) for c in word.lower()] # use character as default if not mapped

print(enc)  # [34, 9, 432, 432, 104, ' ', 104693, 104, 332, 432, 5]

我选择小写您的输入(并将其移至普通字符串,而不是其中包含一个字符串的字符串列表)。

如果未映射字符,它将使用它而不是数字(例如空格)。

您可以使用以下命令创建空格分隔的字符串

s = ' '.join(map(str,enc))
print( s )  

输出:

34 9 432 432 104   104693 104 332 432 5

有关dict.get(),请参见zip()

答案 2 :(得分:1)

您可以使用zipmap创建字典,以将对应的字母与加密数字进行匹配。

word = "Hello world"
encoding = []
charachter = ["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",""]
number = [0,2300,2,5,9,7,10,34,876,23,125,432,567,103,104,10234,102435,332,7654,12435,65434,12121,104693,130694,120357,12346,124546324]

lookup = dict(zip(charachter,number))
output = " ".join(list(map(lambda elem: str(lookup.get(elem,' ')), word.lower())))
print(output)

输出:

34 9 432 432 104   104693 104 332 432 5

答案 3 :(得分:1)

您可以这样做:

word = "Hello world"
encoding = []
character = ["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"," "]
number = [0,02300,2,5,9,7,10,34,876,23,125,432,567,103,104,10234,102435,332,7654,12435,65434,12121,104693,130694,120357,12346,124546324]

# create a dictionary with keys as characters and values as numbers
mapping = {}
for i in range(0,27):
    mapping[character[i]] = number[i]

# now iterate over the string and look up the dictionary for each character
for x in word:
    encoding.append(mapping[x.lower()])

print(encoding)

注意:

  1. 我将word视为字符串(word = "Hello world")而不是字符串数组(word = ["Hello world"])。
  2. 我已将字符数组中的最后一项("")替换为空格(" ")。我们需要它来替换{​​{1}}和Hello之间的空格。