0

So I just went into python not too long ago, it is to develop my OCR project. I want the software to detect the character "A" and convert it to a set of integers like 101.

list=['haha', 'haaa']

I am thinking of using a dictionary with keys and item to try replacing it. I added a define function for the process. I use this method I found in other post but it doesn't work.

Dictionary={'a':101,'h':111}
for a,b in Dictionary.items():
    list = list.replace(a.lower(),b)
print (list)
2
  • Is there a strict requirement that you must convert the characters to a specific integer? If you use the function ord on a single character, it will return it's ascii ID. Commented Oct 1, 2021 at 8:40
  • 1
    Could you provide the expected output? Commented Oct 1, 2021 at 8:42

5 Answers 5

1

First, you should make sure your list variable is not list as this is a keyword in python. Then, loop through the items and replace the key with the value at the key as such:

    l = ['haha', 'haaa']
    refDict = {'a':101,'h':111}

   for i, item in enumerate(l):
        for key in refDict:
            item = item.replace(key, str(refDict[key]))
        l[i] = item

Output after this code:

['111101111101', '111101101101']
Sign up to request clarification or add additional context in comments.

2 Comments

ah alright, noted for the advise and thanks for your input.
If it worked would you mind setting the question as correct? That way I get points, and you do too.
0

Never use list as variable since it is already a python function.

One can use this:

l = ['haha', 'haaa']
conv_dict = {'a':101, 'h':111}

for j, ele in enumerate(l):
  ele = list(ele)
  for i, char in enumerate(ele):
      ele[i] = conv_dict[char.lower()]
  l[j] = int( ''.join(map(str, ele)))

print(l)
>> [111101111101, 111101101101]

This is not a robuste solution, since every character should be in the conv_dict to convert the char to int.

How it works:

  • Go over each word in the list
  • Convert string to list, with each char as element
  • Go over each character
  • Replace character with integer
  • Join the integers to one string and then convert it back to integer
  • Repeat for every string in list

Comments

0

I'm not very sure what output you're expecting but your question seems like you want the equivalent value of the elements in the dictionary to be substituted by the key values in the dictionary.
As each element of lst is considered in the first loop, an empty string ans is initialized. It then iterates through every character in the nested loop which concatenates the dictionary equivalent of each character into ans. The end result is appended into output

Dictionary={'a':101,'h':111}
lst=['haha', 'haaa']
output = []
for i in lst:
    ans = ""
    for j in i:
        ans+=str(Dictionary[j])

    output.append(ans)

print(output)

Output

['111101111101', '111101101101']

Comments

0

It sounds to me like you do not need to map the characters to a specific integer, just any unique integer. I would recommend not creating your own dictionary and using the standardized ascii mappings for characters (https://www.asciitable.com/). Python has a built-in function for converting characters to that value

Here is what that might look like (as others have pointed out, you also shouldn't use list as a variable name.

words = ['haha', 'haaa']
conversions = []
for word in words:
    converted_word = []
    for letter in word:
        converted_word.append(ord(letter))
    conversions.append(converted_word)

print(conversions)

This prints: [[104, 97, 104, 97], [104, 97, 97, 97]]

Comments

0

How about str.translate?

lst = ['haha', 'haaa']
table = {ord('a'): '101', ord('h'): '111'}
lst = [s.translate(table) for s in lst]
print(lst)

Output (Try it online!):

['111101111101', '111101101101']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.