0

How to use re.sub on lists? Is it possible?

a = ['bdf', 1, 2] a = re.sub('\d+', '', a[a:]) Above code giving error. TypeError: expected string or bytes-like object

24th May 2020, 1:44 AM
Med D
Med D - avatar
5 odpowiedzi
+ 6
No, you can't. There are various ways to remove all ints from a list. E.g. Using filter: a = [*filter(lambda x: type(x) != int, a)] But what do you want to do? It's not clear. Please give more details and help us help you.
24th May 2020, 2:31 AM
Kevin ★
+ 3
How to use re.sub on lists? Is it possible? a = ['bdf', 1, 2] a = [s for s in a if s.isalpha()]
24th May 2020, 6:38 AM
Oma Falk
Oma Falk - avatar
+ 3
You should review the regular expressions("re") module of Python tutorial. https://www.sololearn.com/learn/Python/2475 I recommend you to go through the comment section of each lesson too. sub, as many other "re" methods is used to manipulate strings, NOT LISTS. """ re.sub(pattern, repl, string, count=0) ... This method replaces all occurrences of the pattern in string with repl, substituting all occurrences, unless count provided. This method returns the modified string. """ ---CopyPasted from Python tutorial. By the way Oma Falk. Your code is showing me error for non-string types.
25th May 2020, 4:16 AM
Kevin ★
+ 1
Thanks Kevin
25th May 2020, 1:45 PM
Med D
Med D - avatar
0
But what do you want to do? It's not clear. Please give more details and help us help you. Just want to know if re.sub is usable in lists. Thats it. Want to understand re.sub
24th May 2020, 4:04 PM
Med D
Med D - avatar