Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Mapped Strings
#13
In code now it will overwrite all values and only save last.
I would do it something like this,making a replace_dict first then loop over and replace.
# mapp1.py
# Your .csv output append to list,can also make it dict on fly
lst = [
    ['-nrdbc', 'n3DeleteBearerCommand'],
    ['-nrrc', 'n3ContextRsp'],
]

# Make it to a dictionary
replace_dict = {key: value for key, value in lst}

input_string = "gsh modify_gtp_v2 -nrrc 3 -trac 3 -nrds 2 -trds 5 -nrdbc 2"
words = input_string.split()
output_words = []
for word in words:
    if word in replace_dict:
        output_words.append(replace_dict[word])
    else:
        output_words.append(word)

output_string = " ".join(output_words)
print(output_string)
Test.
λ python -i mapp1.py
gsh modify_gtp_v2 n3ContextRsp 3 -trac 3 -nrds 2 -trds 5 n3DeleteBearerCommand 2

>>> replace_dict
{'-nrdbc': 'n3DeleteBearerCommand', '-nrrc': 'n3ContextRsp'}

>>> # Made from
>>> lst
[['-nrdbc', 'n3DeleteBearerCommand'], ['-nrrc', 'n3ContextRsp']]
Reply


Messages In This Thread
Python Mapped Strings - by edualfaia - Aug-30-2023, 04:25 PM
RE: Python Mapped Strings - by Larz60+ - Aug-30-2023, 06:16 PM
RE: Python Mapped Strings - by edualfaia - Aug-30-2023, 10:12 PM
RE: Python Mapped Strings - by edualfaia - Aug-30-2023, 11:21 PM
RE: Python Mapped Strings - by Larz60+ - Aug-31-2023, 03:01 AM
RE: Python Mapped Strings - by edualfaia - Aug-31-2023, 07:45 AM
RE: Python Mapped Strings - by Larz60+ - Aug-31-2023, 10:01 PM
RE: Python Mapped Strings - by edualfaia - Sep-01-2023, 10:15 AM
RE: Python Mapped Strings - by Skaperen - Sep-03-2023, 05:43 PM
RE: Python Mapped Strings - by edualfaia - Sep-03-2023, 09:43 PM
RE: Python Mapped Strings - by edualfaia - Sep-03-2023, 10:22 PM
RE: Python Mapped Strings - by Skaperen - Sep-04-2023, 01:48 AM
RE: Python Mapped Strings - by snippsat - Sep-04-2023, 11:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  print a list strings is fast but printing the joined strings is slow Skaperen 9 4,002 Aug-26-2019, 07:48 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020