convert letter from a list into a number depending on another list in python
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
python list encryption
add a comment |
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
python list encryption
02300 is not a valid integer. Would strings innumber
be ok?
– timgeb
Nov 25 '18 at 13:00
First of all, you'll need a mapcharacter -> number
:Map = dict(zip(character, number))
...
– ForceBru
Nov 25 '18 at 13:02
Also how is the""
->124546324
mapping supposed to work? There's a lot of empty strings in any string...
– timgeb
Nov 25 '18 at 13:02
1
THere is no mapping for capitalH
- there is no mapping for space.
– Patrick Artner
Nov 25 '18 at 13:03
add a comment |
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
python list encryption
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
python list encryption
python list encryption
edited Nov 25 '18 at 13:02
Vasilis G.
3,7392824
3,7392824
asked Nov 25 '18 at 12:59
WaterploofWaterploof
474
474
02300 is not a valid integer. Would strings innumber
be ok?
– timgeb
Nov 25 '18 at 13:00
First of all, you'll need a mapcharacter -> number
:Map = dict(zip(character, number))
...
– ForceBru
Nov 25 '18 at 13:02
Also how is the""
->124546324
mapping supposed to work? There's a lot of empty strings in any string...
– timgeb
Nov 25 '18 at 13:02
1
THere is no mapping for capitalH
- there is no mapping for space.
– Patrick Artner
Nov 25 '18 at 13:03
add a comment |
02300 is not a valid integer. Would strings innumber
be ok?
– timgeb
Nov 25 '18 at 13:00
First of all, you'll need a mapcharacter -> number
:Map = dict(zip(character, number))
...
– ForceBru
Nov 25 '18 at 13:02
Also how is the""
->124546324
mapping supposed to work? There's a lot of empty strings in any string...
– timgeb
Nov 25 '18 at 13:02
1
THere is no mapping for capitalH
- there is no mapping for space.
– Patrick Artner
Nov 25 '18 at 13:03
02300 is not a valid integer. Would strings in
number
be ok?– timgeb
Nov 25 '18 at 13:00
02300 is not a valid integer. Would strings in
number
be ok?– timgeb
Nov 25 '18 at 13:00
First of all, you'll need a map
character -> number
: Map = dict(zip(character, number))
...– ForceBru
Nov 25 '18 at 13:02
First of all, you'll need a map
character -> number
: Map = dict(zip(character, number))
...– ForceBru
Nov 25 '18 at 13:02
Also how is the
""
-> 124546324
mapping supposed to work? There's a lot of empty strings in any string...– timgeb
Nov 25 '18 at 13:02
Also how is the
""
-> 124546324
mapping supposed to work? There's a lot of empty strings in any string...– timgeb
Nov 25 '18 at 13:02
1
1
THere is no mapping for capital
H
- there is no mapping for space.– Patrick Artner
Nov 25 '18 at 13:03
THere is no mapping for capital
H
- there is no mapping for space.– Patrick Artner
Nov 25 '18 at 13:03
add a comment |
4 Answers
4
active
oldest
votes
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
1
stackoverflow.com/questions/11041405/…
– Patrick Artner
Nov 25 '18 at 13:15
@PatrickArtner you do have a point. I will edit my answer.
– Vasilis G.
Nov 25 '18 at 13:16
add a comment |
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']
add a comment |
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()
add a comment |
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:
- I'm treating
word
as a string(word = "Hello world"
) rather than an array of strings(word = ["Hello world"]
). - I've replaced the last item (
""
) in the character array with a space (" "
). We need this to replace the space betweenHello
andworld
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467687%2fconvert-letter-from-a-list-into-a-number-depending-on-another-list-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
1
stackoverflow.com/questions/11041405/…
– Patrick Artner
Nov 25 '18 at 13:15
@PatrickArtner you do have a point. I will edit my answer.
– Vasilis G.
Nov 25 '18 at 13:16
add a comment |
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
1
stackoverflow.com/questions/11041405/…
– Patrick Artner
Nov 25 '18 at 13:15
@PatrickArtner you do have a point. I will edit my answer.
– Vasilis G.
Nov 25 '18 at 13:16
add a comment |
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
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
edited Nov 25 '18 at 13:19
answered Nov 25 '18 at 13:08
Vasilis G.Vasilis G.
3,7392824
3,7392824
1
stackoverflow.com/questions/11041405/…
– Patrick Artner
Nov 25 '18 at 13:15
@PatrickArtner you do have a point. I will edit my answer.
– Vasilis G.
Nov 25 '18 at 13:16
add a comment |
1
stackoverflow.com/questions/11041405/…
– Patrick Artner
Nov 25 '18 at 13:15
@PatrickArtner you do have a point. I will edit my answer.
– Vasilis G.
Nov 25 '18 at 13:16
1
1
stackoverflow.com/questions/11041405/…
– Patrick Artner
Nov 25 '18 at 13:15
stackoverflow.com/questions/11041405/…
– Patrick Artner
Nov 25 '18 at 13:15
@PatrickArtner you do have a point. I will edit my answer.
– Vasilis G.
Nov 25 '18 at 13:16
@PatrickArtner you do have a point. I will edit my answer.
– Vasilis G.
Nov 25 '18 at 13:16
add a comment |
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']
add a comment |
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']
add a comment |
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']
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']
answered Nov 25 '18 at 13:08
timgebtimgeb
51.1k116694
51.1k116694
add a comment |
add a comment |
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()
add a comment |
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()
add a comment |
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()
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()
edited Nov 25 '18 at 13:12
answered Nov 25 '18 at 13:06
Patrick ArtnerPatrick Artner
25.3k62444
25.3k62444
add a comment |
add a comment |
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:
- I'm treating
word
as a string(word = "Hello world"
) rather than an array of strings(word = ["Hello world"]
). - I've replaced the last item (
""
) in the character array with a space (" "
). We need this to replace the space betweenHello
andworld
.
add a comment |
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:
- I'm treating
word
as a string(word = "Hello world"
) rather than an array of strings(word = ["Hello world"]
). - I've replaced the last item (
""
) in the character array with a space (" "
). We need this to replace the space betweenHello
andworld
.
add a comment |
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:
- I'm treating
word
as a string(word = "Hello world"
) rather than an array of strings(word = ["Hello world"]
). - I've replaced the last item (
""
) in the character array with a space (" "
). We need this to replace the space betweenHello
andworld
.
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:
- I'm treating
word
as a string(word = "Hello world"
) rather than an array of strings(word = ["Hello world"]
). - I've replaced the last item (
""
) in the character array with a space (" "
). We need this to replace the space betweenHello
andworld
.
answered Nov 25 '18 at 13:25
singletonsingleton
814
814
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467687%2fconvert-letter-from-a-list-into-a-number-depending-on-another-list-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
02300 is not a valid integer. Would strings in
number
be ok?– timgeb
Nov 25 '18 at 13:00
First of all, you'll need a map
character -> number
:Map = dict(zip(character, number))
...– ForceBru
Nov 25 '18 at 13:02
Also how is the
""
->124546324
mapping supposed to work? There's a lot of empty strings in any string...– timgeb
Nov 25 '18 at 13:02
1
THere is no mapping for capital
H
- there is no mapping for space.– Patrick Artner
Nov 25 '18 at 13:03