简体   繁体   中英

how can i remove word from strings using def and for function?

Snippet of code:

def find_min(string):
    a = 'b1!!Ze44B&cC3#dd$$8'
    d=[]
    min_=10000
    for i in a:
        d.append(ord(i))
        for y in d:
            if min_>y:
                min_=y
                string=chr(min_)
                a=string
    return a 
a='b1!!Ze44B&cC3#dd$$8'
print(find_min(a))
    #output=!
def delete_element(string,element):
    t=''
    b=find_min(a)
    for i in range(len(a)):
        b = find_min(a)
        a=a.replace(a[i],'',i)
        a = delete_element(a,b)
        t= b+ t
        print(a)
        print(t)
        print()              
t=''
b=''
string=''
delete_element(a,b)

#output
'b1!Ze44B&cC3#dd$$8' ! 'b1Ze44B&cC3#dd$$8' !! 'b1Ze44B&cC3dd$$8' #!!

What should I do to finish the code? I would like the def function to return the extracted element (def min) as well as the string indicating which element was removed.

'b1!Ze44B&cC3#dd$$8'
!
'b1Ze44B&cC3#dd$$8'
!!
'b1Ze44B&cC3dd$$8'
#!!

I wanted the code output to be like this....

If I understand what you're trying to do correctly then this should work for you:

def delete_element(string):
  return string.replace(sorted(string)[0], '')

print(delete_element('b1!!Ze44B&cC3#dd$$8'))

...will output b1Ze44B&cC3#dd$$8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM