0

I want to convert each letter of the list word to a number depending on the lists character and number. So a = 0, b = 02300, c = 2. I want this output:

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

ps: It's not import if there's no space between each number.

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]

I don't no what to do because the numbers of the list number are not equal to the index of the letters in the list character.

PS: I hope there's not another topic like this because I didn't find it

Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
Waterploof
  • 101
  • 7

4 Answers4

2

Your post contains quite a few irregularities, such as missing characters, illegal integers, missing mappings and that peculiar translation for the empty string - so I'm going to answer in general.

What you want (what I gathered from "It's not import if there's no space between each number") is a translation table that maps characters to their translation. You can get it by passing a mapping of characters to strings to 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'

If you actually do want a list, use

>>> [char_to_number[c] for c in plain]
['0', '02300', '2', '0', '02300', '2']
timgeb
  • 76,762
  • 20
  • 123
  • 145
1

You can use zip() to create a lookup dictionary.

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]

I opted to lowercase your input (and moved it to a normal string, not a list of strings with one string in it).

If a character is not mapped, it will use it instead of a number (f.e. for the space).

You can create a space seperated string fom it with:

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

Output:

34 9 432 432 104   104693 104 332 432 5

See Why dict.get(key) instead of dict[key]? for dict.get()

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

You can create a dictionary using zip and map to match the corresponding letter with the encryption number.

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)

Output:

34 9 432 432 104   104693 104 332 432 5
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
1

You can do it like this:

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)

Note:

  1. I'm treating word as a string(word = "Hello world") rather than an array of strings(word = ["Hello world"]).
  2. I've replaced the last item ("") in the character array with a space (" "). We need this to replace the space between Hello and world.
singleton
  • 111
  • 6