Replacing letters in Python under a given condition

Source:
meds = [ "tuberculin Cap(s)", "tylenol Cap(s)", "tramadol 2 Cap(s)"]


for i in meds:
    new_meds = i.replace(" Cap(s)", " 1 Cap(s)")

    print(new_meds)
Output:
 tuberculin 1 Cap(s)
 tylenol 1 Cap(s)
 tramadol 2 1 Cap(s)

I am trying to replace all the medics with only “Caps (s)” in “1 Cap (s)” the first 2 meds were right, however the third result leads to “tramadol 2 1 Cap (s)”.

How can I fix my script so that all the doctors with the number inside the line do not change?

The end result should be that only medications such as tuberculin Cap (s), tylenol Cap (s) are modified, not tramadol 2 Cap (s).

+4
source share
4 answers

You can use regex with re module :

import re
meds = [ "tuberculin Cap(s)", "tylenol Cap(s)", "tramadol 2 Cap(s)"]
meds = [med.replace(" Cap(s)", " 1 Cap(s)") if len(re.findall("[a-zA-Z]+ \d+ Cap\(s\)", med)) == 0 else med for med in meds]
print meds

Prints above

['tuberculin 1 Cap(s)', 'tylenol 1 Cap(s)', 'tramadol 2 Cap(s)']

Parsing it as given :

, . python , for. , :

lst = ["one", "two", "three"]
print [element for element in lst]

['one', 'two', 'three'].

.

  • () " ". , [ab] a, b.

  • . [a-e] a e ().

  • A + regex " " - [ab]+, , 1 a / b.

  • \d ( [0-9]).

  • , - , ( ' ), , .

; [a-z]+, \d+ Cap\(s\). :

" 1 , " + ", , +. (Cap)" ".

re.findall(pattern, string) , pattern, string. 0 , . , + + "Cap (s)".

, , - , , "word + number +" Cap (s) ".

(, ), [a-zA-Z\d]+ \d+ Cap\(s\), , .

for

, for:

for index, med in enumerate(meds):
  if len(re.findall("[a-zA-Z\d]+ \d+ Cap\(s\)", med)) == 0:
    meds[index] = med.replace(" Cap(s)", " 1 Cap(s)")

, for , ( enumerate). , enumerate , :

for i in xrange(len(meds)):
  if len(re.findall("[a-zA-Z\d]+ \d+ Cap\(s\)", meds[i])) == 0:
    meds[i] = meds[i].replace(" Cap(s)", " 1 Cap(s)")

enumerate for: enumerate tuples, ( ) : (index, element). python : a,b = (1,2). a 1 b 2.

+1

, , "Cap (s)" "1 Cap (s)". , - , .

, , , , , , :

meds = [ "tuberculin Cap(s)", "tylenol Cap(s)", "tramadol 2 Cap(s)"]


for i in meds:
    # Check if a number is in the string
    if any(char.isdigit() for char in i): 
        print(i)
    else:
        i = i.replace(" Cap(s)", " 1 Cap(s)")
        print(i)

, "meds" , .

, , :

meds = ["tuberculin Cap(s)", "tylenol Cap(s)", "tramadol 2 Cap(s)"]

for med in meds:
    # Check if a number is in the string
    if any(char.isdigit() for char in med): 
        med = med.replace("Cap(s)", "Caps")
    else:
        med = med.replace("Cap(s)", "1 Cap")    
    print(med)
+1

Using List List Understanding

In [35]: meds
Out[35]: ['tuberculin Cap(s)', 'tylenol Cap(s)', 'tramadol 2 Cap(s)']

In [36]: new_meds=[ i.replace(" Cap(s)", " 1 Cap(s)") if any(char.isdigit() for char in i) == False  else i for i in meds]

In [37]: new_meds
Out[37]: ['tuberculin 1 Cap(s)', 'tylenol 1 Cap(s)', 'tramadol 2 Cap(s)']
0
source

You can use RegEx as follows:

import re
meds = [ "tuberculin Cap(s)", "tylenol Cap(s)", "tramadol 2 Cap(s)"]

for i in meds:
    if not re.match(".+\d.+", i):
        new_meds = i.replace(" Cap(s)", " 1 Cap(s)")
    else:
        new_meds = i
    print(new_meds)

Conclusion:

tuberculin 1 Cap(s)
tylenol 1 Cap(s)
tramadol 2 Cap(s)

The expression ".+\d.+"will find the element with "something + digit + something."

0
source

All Articles