# Replace list with Letters <-> Numers and vicaversa

Hi,

Challenge:
There is list A containing Letters.
I would like to replace the Letters, with numbers:
A --> 0
B --> 1
C -->2
etc.

To make it a bit harder, there are also items present which contain more info like:
A to D --> 0 to 3

I tried the replace text command, but I cant figure out how I can replace more Letters simultaneously.

Any suggestion how I could do this in a generic way?

Furthermore, 1 ‘almost’ simular/but opposite thing is, that I also have another list. Where the opposite has to happen.
0 --> A
1 --> B
2 --> C

11 --> L
I am a bit afraid that it will go wrong with 1/11 because the text has to be ‘recognized’ as 1 or 11, without mixing them up’

Replace-Letters with numbers.gh (12.2 KB)

You can also use ReplaceTextMultiple component from Wombatgh plug-in:

Wombatgh.gh (5.1 KB)

Hi!

Thanks👍

The list: Letters to nummers is solved .

However the nummer to letters list isn’t fully correct, because the 3rd branch has to be L to Q. Instead of B to G.

Do You have any suggestions?
Thanks again!

or use Python and ASCII…

Ahh thanks Again!
1 final thing to close the topic:
When I ‘join’ the text items, branch 3 results in
LtoQ
L to Q
Do you have any suggestions how to automatically generate the spaces?

The devil is in the detail😅.
Thanks again!

Ahh haha offcourse!
Thanks!

Hi,

Unfortunately I just ran into another challenge

It is also possible the list contains values like;
1,2 --> B,C
4,6 --> E,G (without space in between).

I tried with adding the , in the split command, but than the , is ‘deleted’ and a space is added in between E G.

Do you have any suggestions?

Thank you once again

Hi,
Thank you :).

Although I dont have any python skills, I think it should be possible.
Would it be possible to 3 ‘input’ variables and 1 output?

input A: List which has to be modified

input B: numbers which correspond to the Letters in input C
input C: Letter which correspond to the number in input B

Output D: The modified/translated input list A

Things to be aware of, is it also going correctly when there is a 1 and/or 11 present?
Other text or , have to be remain present. so only the numbers have to be converted.

Thank you again

``````import re
replacements = dict(zip(f, r))
a = re.sub('(\d+)', lambda m: replacements[m.group()], t)
``````

regex.gh (16.2 KB)

3 Likes

ahh perfect!
Exactly what I needed

Thank you!

Hi,

Thanks once again.
Today I ran into 1 (and final thing).
Chellenge:
I also need to ‘translate/convert’ the other way round. So letters into numbers.
I thought this would maybe be possible with this python script, simply by modifying the ‘find’ and ‘replace’ input of the python script. Unfortunately this is not the case.

Do you have any suggestion howcome, and/or modify the script?
Thank you once again!

``````import re
replacements = dict(zip(f, r))
a = re.sub(r'[A-Z]', lambda m: replacements[m.group()], t)
``````

regex.gh (14.8 KB)

You can also use this:

replace.gh (9.1 KB)

1 Like

Here I did something similar:

1 Like

Ahh thank you once again!