0

I'm trying to rename text/num ids (eg. ABC123) to just num ids (eg. 123123) by matching text to a dictionary code. (just learning how to code, so if better idea, please do tell).

I tried searching for solutions but many are for using print. I need to pass the results to another function, so can't use print.

def convertid(old_id):
    code = {'A' : '1','B' : '2','C' : '3','D' : '4','E' : '5','F' : 
    '6','G' : '7','H' : '8','I' : '9','J' : '10','K' : '11','L' : 
    '12','M' : '13','N' : '14','O' : '15','P' : '16','Q' : '17','R' :        
    '18','S' : '19','T' : '20','U' : '21','V' : '22','W' : '23','X' : 
    '24','Y' : '25','Z' : '26', '1' : '1','2' : '2','3' : '3','4' : 
    '4','5' : '5','6' : '6','7' : '7','8' : '8','9' : '9','0' : '0'}

    for x in old_id:
        new_id = code[x],end = ""
        return new_id

I've also tried new_id = "".join(code[x]),and new_id += code[x], but none of them work.

I was hoping to get "123123" as the new id in a single line, instead of:

1

2

3

1

2

3

on multiple lines (sorry, don't know why the preview is showing the numbers on double space lines. The result I got were in single space lines.).

S7bvwqX
  • 147
  • 3
  • 13
  • 1
    The answer given is about as close as you can get. So, on another angle, you know that this transformation is irreversible, right? Letters and numbers are mapping to the same integer values. – roganjosh Feb 08 '19 at 18:37
  • Also, `"26"` could have either been `"Z"` or `"BF"`. – chepner Feb 08 '19 at 18:50

3 Answers3

2

Just join on an empty string:

code = {'A' : '1','B' : '2','C' : '3','D' : '4','E' : '5','F' : 
    '6','G' : '7','H' : '8','I' : '9','J' : '10','K' : '11','L' : 
    '12','M' : '13','N' : '14','O' : '15','P' : '16','Q' : '17','R' :        
    '18','S' : '19','T' : '20','U' : '21','V' : '22','W' : '23','X' : 
    '24','Y' : '25','Z' : '26', '1' : '1','2' : '2','3' : '3','4' : 
    '4','5' : '5','6' : '6','7' : '7','8' : '8','9' : '9','0' : '0'}

i = 'A4BC'

n = "".join(code[l] for l in i)

print(n)

# 1423
Ben
  • 5,952
  • 4
  • 33
  • 44
  • `join` is one side case where the expression is slower than a list comprehension. `n = "".join([code[l] for l in i])` will be faster and is just as clean IMO – roganjosh Feb 08 '19 at 18:38
  • 1
    Really @roganjosh ? I didn't realize. Hopefully I'll have time to set up some timing tests later today. That's interesting, thank you – Ben Feb 08 '19 at 18:39
  • Yep, the whole list will still get expanded out prior to `join` doing any work. It's faster to use the list comp – roganjosh Feb 08 '19 at 18:40
1

You can do it with new_id += code[x] like below

def convertid(old_id):
    code = {'A' : '1','B' : '2','C' : '3','D' : '4','E' : '5','F' : 
    '6','G' : '7','H' : '8','I' : '9','J' : '10','K' : '11','L' : 
    '12','M' : '13','N' : '14','O' : '15','P' : '16','Q' : '17','R' :        
    '18','S' : '19','T' : '20','U' : '21','V' : '22','W' : '23','X' : 
    '24','Y' : '25','Z' : '26', '1' : '1','2' : '2','3' : '3','4' : 
    '4','5' : '5','6' : '6','7' : '7','8' : '8','9' : '9','0' : '0'}

    new_id = ''
    for x in old_id:
        new_id += code[x]
    return new_id
print(convertid('ABC123')) # 123123
xashru
  • 3,400
  • 2
  • 17
  • 30
  • 1
    See: https://stackoverflow.com/a/34008289/4799172 Don't concatenate strings in loops if you can use `join` – roganjosh Feb 08 '19 at 18:50
  • I just wanted to point out that it can be done with `new_id += code[x]` since OP explicitly mentioned they could not. – xashru Feb 08 '19 at 18:51
  • Thanks! This solution works too! I put the return under the wrong indentation, still learning! – S7bvwqX Feb 09 '19 at 02:53
1

using get which will handle the case if key is not present and use default value'' in that case

code = {'A' : '1','B' : '2','C' : '3','D' : '4','E' : '5','F' : 
        '6','G' : '7','H' : '8','I' : '9','J' : '10','K' : '11','L' : 
        '12','M' : '13','N' : '14','O' : '15','P' : '16','Q' : '17','R' :        
        '18','S' : '19','T' : '20','U' : '21','V' : '22','W' : '23','X' : 
        '24','Y' : '25','Z' : '26', '1' : '1','2' : '2','3' : '3','4' : 
        '4','5' : '5','6' : '6','7' : '7','8' : '8','9' : '9','0' : '0'}

i = 'A4BC'
print(''.join(code.get(l,'') for l in i))
mad_
  • 8,121
  • 2
  • 25
  • 40
  • 1
    See: https://stackoverflow.com/a/34008289/4799172 Don't concatenate strings in loops, use `join` – roganjosh Feb 08 '19 at 18:48
  • @roganjosh Thanks this is interesting. – mad_ Feb 08 '19 at 18:55
  • Next up is my comment [here](https://stackoverflow.com/questions/54598296/how-to-get-for-loop-in-a-function-to-return-not-print-in-single-one-line#comment95993002_54598344). I'll try track down the link :) – roganjosh Feb 08 '19 at 18:57