Včasih se lahko med delom z nizi Python pojavi težava, ko moramo zamenjati vse pojavitve podniza z drugim.
Vnos: test_str = geeksforgeeks s1 = geeks s2 = abcd
Izhod: test_str = abcdforarabcd Pojasnilo: vse pojavitve s1 zamenjamo s s2 v test_str.
Vnos: test_str = geeksforgeeks s1 = za s2 = abcd
Izhod: test_str = geeksabcdgeeks
Pristop 1
Za zamenjavo vseh pojavitev podniza lahko uporabimo vgrajeno funkcijo replace, ki je prisotna v python3.
centriranje slike v css
Izvedba z uporabo vgrajene funkcije:-
Python3
#Python has inbuilt function replace to replace all occurrences of substring.> input_string>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> input_string>=> input_string.replace(s1, s2)> print>(input_string)> |
>
>Izhod
ponavljanje zemljevida v Javi
abcdforabcd>
Časovna zapletenost: O(n)
Pomožni prostor: O(n)
Pristop 2:
Uporablja se razdelitev niza po podnizu in nato zamenjava z novo funkcijo string.split().
Python3
#code for replacing all occurrences of substring s1 with new string s2> test_str>=>'geeksforgeeks'> s1>=>'geeks'> s2>=>'abcd'> #string split by substring> s>=>test_str.split(s1)> new_str>=>''> for> i>in> s:> >if>(i>=>=>''):> >new_str>+>=>s2> >else>:> >new_str>+>=>i> #printing the replaced string> print>(new_str)> #contributed by Bhavya Koganti> |
>
>Izhod
abcdforabcd>
Časovna in prostorska kompleksnost za vse metode sta enaki:
Časovna zapletenost: O(n)
Pomožni prostor: O(n)
kako razkriti skrite aplikacije
3. način: Drug pristop za zamenjavo vseh pojavitev podniza v nizu je uporaba re.sub() funkcijo iz modula re v pythonu.
Python3
import> re> def> replace_substring(test_str, s1, s2):> ># Replacing all occurrences of substring s1 with s2> >test_str>=> re.sub(s1, s2, test_str)> >return> test_str> # test> test_str>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> print>(replace_substring(test_str, s1, s2))> |
>
>Izhod
letomesec
abcdforabcd>
Časovna zapletenost: O(n), kjer je n dolžina vhodnega niza. To je zato, ker funkcija re.sub() ponovi skozi celoten vhodni niz in izvede ujemanje regularnega izraza za vsak znak, da najde vse pojavitve podniza. Število ponovitev je premo sorazmerno z dolžino vhodnega niza.
Pomožni prostor:Novo
4. način: Uporaba enostavne iteracije
Ideja tega pristopa je iteracija skozi vhodni niz znak za znakom in preverjanje, ali se vsak podniz dolžine m ujema s podnizom, ki ga želimo zamenjati. Če se, dodamo nadomestni podniz našemu rezultatu in premaknemo kazalec naprej za m znakov. Če se ne ujema, rezultatu dodamo trenutni znak in premaknemo kazalec naprej za 1 znak.
Python3
sortiranje mehurčkov
def> replace_substring(test_str, s1, s2):> ># Initialize an empty string to store the result> >result>=> ''> ># Initialize a variable to keep track of our position in the string> >i>=> 0> ># Loop through the string one character at a time> >while> i <>len>(test_str):> ># Check if the current substring matches the substring we want to replace> >if> test_str[i:i>+>len>(s1)]>=>=> s1:> ># If it does, add the replacement substring to the result and move the pointer forward> >result>+>=> s2> >i>+>=> len>(s1)> >else>:> ># If it doesn't, add the current character to the result and move the pointer forward> >result>+>=> test_str[i]> >i>+>=> 1> ># Return the final result> >return> result> # test> test_str>=> 'geeksforgeeks'> s1>=> 'geeks'> s2>=> 'abcd'> print>(replace_substring(test_str, s1, s2))> |
>
>Izhod
abcdforabcd>
Časovna zahtevnost: O(nm), kjer je n dolžina vhodnega niza in m dolžina podniza, ki ga je treba zamenjati.
Pomožni prostor: O(n), saj ustvarjamo nov niz za shranjevanje rezultata.