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?

2020-05-10 Replace Letters with numbers.gh (5.9 KB)

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’

Thank you in advance :slight_smile:


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 :slight_smile:.

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!:slight_smile:


Replace-Letters with numbers.gh (7.9 KB)

or use Python and ASCII…

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

The devil is in the detail😅.
Thanks again! :slight_smile:


Replace-Letters with numbers.gh (9.3 KB)

Ahh haha offcourse!
Thanks!:+1::+1:

Hi,

Unfortunately I just ran into another challenge :sweat_smile:

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 :slight_smile:

2020-05-10 Replace-Letters with numbers_RE_re.gh (11.6 KB)

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.

2020-05-10 Replace-Letters with numbers_RE_re.gh (11.6 KB)

Thank you again :slight_smile:

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 :+1: :+1:

Thank you!

Hi,

Thanks once again. :slight_smile:
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! :slight_smile:

2020-05-10 Replace-Letters with numbers_RE_re_RE_re.gh (21.9 KB)

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! :slight_smile:
I really like your script.

Just wandering, would it also be possible to make it ‘only’ based on the input parameters f and r? So that its independent on number or Letters?

So its;

f: find --> find the symbols/letters/numbers in this list
r: replace --> replace which the symbols/letters/numbers in this list

This so I can use the same python script for both ‘translations’: A and B?

2020-05-10 Replace-Letters with numbers_RE_re_RE_re_RE_re.gh (21.9 KB)

Thanks again :slight_smile: