-7

The string:

"Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"

How do I delete using map or filter all names from a string?

str = filter( x.isdigit() , for x str)

But it does not seem to work properly.

ERJAN
  • 23,696
  • 23
  • 72
  • 146

3 Answers3

1

How about just using split:

str = "Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"
output_list = [line.split(";")[1:] for line in str.split("\n")]
output = [";".join(line) for line in output_list]
>>>['10;20;30', '20,24,25', '30;30;30', '']
Adam Dadvar
  • 384
  • 1
  • 7
0

You can split the string, slice the resulting list, and then re-join the sliced list into a string using the same delimiter:

s = "Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"
print('\n'.join(';'.join(l.split(';')[1:]) for l in s.splitlines()))

This outputs:

10;20;30
20,24,25
30;30;30
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

In your exact case you can use python's built-in re and list comprehension.

>>> import re
>>> text = "Jack;10;20;30\n Omar;20,24,25\n Carl;30;30;30\n"
>>> [number for number in re.split(';|,| |\n|', text) if number.isdigit()]
['10', '20', '30', '20', '24', '25', '30', '30', '30']

re.split splits string by ;, ,, and \n

List comprehension creates lists of elements that are numbers.

Josef Korbel
  • 1,168
  • 1
  • 9
  • 32