-2

My question is similar to excel formula to replace or stubstitute only text that starts with a certain letter but I need a code in Python. I am trying to do nested if but it only replaces the first character not the entire element.

Also the elements are alphanumeric

List= ['F43.9', 'F53.2', 'H10.9', 'H60.9', 
       'S83.6', 'S01.88', 'J18.9', 'K35.9', 'S42.20', 'J06.9'....]

So my desired output is:

List= ['qwe', 'qwe', 'equal', 'equal', 'chronic', 'chronic', 'liable', 'priority', 'chronic', 'liable',....]

where I need to replace all values starting with F such as F43.9,F53.2,etc. with "qwe". And similarly for different alphabets. There are 30,000 elements in the list with 600 unique values.

  • 1
    Can you shoe your desired output – Jeril Feb 08 '19 at 04:13
  • Create a new list by iterating over your list and examining each item; if an item starts wtih an `F` add the substitute/replacement to the new list otherwise, add the item itself. – wwii Feb 08 '19 at 04:16
  • Sorry I'll edit the question again – AAKASH IYER Feb 08 '19 at 13:48
  • As you said the string starting with 'F' will be replaced by 'qwe', what is the condition for others? How can one identify which string should be replaced by 'equal' and which one by 'chronic' and so on? Could you please provide more details? – hemanta Feb 08 '19 at 14:07

4 Answers4

2

Your new edit indicates you want to replace the entire word. Simply check if the element startswith() the character you expect, then replace it with the desired string. This can get a bit robust though so you may be better off with a dictionary.


for i in range(0, len(list)):
    if list[i].startswith('F'):
        list[i] = 'qwe'
    elif list[i].startswith('H'):
        list[i] = 'equals'
    elif list[i].startswith('S'):
        list[i] = 'chronic'

map = {
    'F': 'qwe',
    'H': 'equals'
    'S': 'chronic'
     # etc etc
}

for i in range(0, len(list)):
    if list[i][0] in map.keys():    # check if first char is a key in map
        list[i] = map[list[i][0]]   # if it is, replace list[i] with the value in map

  • It gives the output as List = ['qwe43.9', 'Z53.2', 'H10.9','H60.9','S83.6',...] but I wish to have an output such as List = ['qwe', 'qwe', 'equal', 'equal', 'chronic', 'chronic', 'liable', 'priority', 'chronic', 'liable',....] – AAKASH IYER Feb 08 '19 at 13:49
1

By using list comprehension we can achieve this by doing:

new_list = ['qwe' + List[i][1:] if List[i].startswith('F') else List[i] for i in range(len(List)) ]
print(new_list)
['qwe43.9','qwe53.2','H10.9','H60.9', 'S83.6','S01.88','J18.9','K35.9','S42.20',
'J06.9']

you can replace the letter 'F' for any other letter in the string that you want to replace the beginning letter.

hemanta
  • 1,405
  • 2
  • 13
  • 23
  • I was working on this and saw AkshayaNevrekar answer after I posted my answer. His answer is more concise and clear. – hemanta Feb 08 '19 at 04:35
  • It gives the output as List = ['qwe43.9', 'Z53.2', 'H10.9','H60.9','S83.6',...] but I wish to have an output such as List = ['qwe', 'qwe', 'equal', 'equal', 'chronic', 'chronic', 'liable', 'priority', 'chronic', 'liable',....] – AAKASH IYER Feb 08 '19 at 13:49
0

May can use list-comprehension for it

List = ['qwe'+i[1:] if i.startswith('F') else i for i in List]
Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • It gives the output as ['qwe43.9', 'Z53.2', 'H10.9','H60.9','S83.6',...] but I wish to have an output such as ['qwe', 'zxcasdasd', 'asdasdaxas', 'vbnvcvvb',...] – AAKASH IYER Feb 08 '19 at 13:07
0

try this:

for i in range(0, len(list)):
    if list[i].startswith('F'):
        list[i] = 'qwe'
    elif list[i].startswith('H'):
        list[i] = 'chronic'
    elif list[i].startswith('S'):
        list[i] = 'liable'
    else list[i].startswith('J'):
        list[i] = 'priority'

etc...

Jonas
  • 1
  • 1