Suppose I have this line:
s = "123(45)678"
How can I get this list?
l = ['123','(','45',')','678']
If you are only interested in '('or ')', then str.partitionit would be enough.
'('
')'
str.partition
Since you have several separators and you want to keep them, you can use re.splitwith a capture group:
re.split
import re s = "123(45)678" print(re.split(r'([()])', s)) # ['123', '(', '45', ')', '678']
You can use re.findall:
re.findall
import re s = "123(45)678" final_data = re.findall('\d+|\(|\)', s) print(final_data)
Conclusion:
['123', '(', '45', ')', '678']
re, :
re
s = "123(45)678" finalist = [] tempstring = '' for e in s: if e!='(' and e!=')': tempstring+=e else: finalist.append(tempstring) finalist.append(e) tempstring='' finalist.append(tempstring) print(finalist)
Source: https://habr.com/ru/post/1688364/More articles:How to disable Instant Run for Android Studio 3.0 - androidIgnore null records in sql - sqlHow to check if a directory with a specific name exists in C #? - c #https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1688362/upgrading-xamarin-forms-to-net-standard-2&usg=ALkJrhioRy-Q3BrkjIJZ4SxBqCkBNlE-OADebugger not working on Apple Watch Series 3 - debugging- operator with assembly using a non-standard estimate - rAssociation direction in usage diagrams - umlError using tidyeval quo () with the gather () command - rОшибка при запуске ионной 3 на iOS - iosafter submitting the edit form getting null when I check the console - angularAll Articles