How can I remove counting numbers from the array?
array = ['1 1.60\n2 3.90\n3 5.40', '1 3.45\n2 3.10\n3 2.35', '1 4.70\n2 3.25\n3 1.85', '1 2.20\n2 3.00\n3 3.20']
So need to remove 1 at the start of the array, 2 in \n2, and 3 in \n3 for each. Results need to be
array = ['1.60\n3.90\n5.40', '3.45\n3.10\n2.35', '4.70\n3.25\n1.85', '2.20\n3.00\n3.20']
CodePudding user response:
With RegEx, for this particular case:
import re
yourarray = ['1 1.60\n2 3.90\n3 5.40', '1 3.45\n2 3.10\n3 2.35', '1 4.70\n2 3.25\n3 1.85', '1 2.20\n2 3.00\n3 3.20']
newarray = [re.sub(r"^[0-9] |[0-9] ", '', yourarray[i]) for i in range(len(yourarray))]
print (newarray)
Output:
['1.60\n3.90\n5.40', '3.45\n3.10\n2.35', '4.70\n3.25\n1.85', '2.20\n3.00\n3.20']
CodePudding user response:
import re
array = ['1 1.60\n2 3.90\n3 5.40', '1 3.45\n2 3.10\n3 2.35', '1 4.70\n2 3.25\n3 1.85', '1 2.20\n2 3.00\n3 3.20']
array1=[]
array1.extend(re.sub('\d ','',x) for x in array)
output
['1.60\n3.90\n5.40', '3.45\n3.10\n2.35', '4.70\n3.25\n1.85', '2.20\n3.00\n3.20']
CodePudding user response:
You can do:
>>> ['\n'.join(map(lambda x: x.split()[1], e.splitlines())) for e in array]
['1.60\n3.90\n5.40', '3.45\n3.10\n2.35', '4.70\n3.25\n1.85', '2.20\n3.00\n3.20']
Or,
>>> ['\n'.join([x.split(maxsplit=1)[1] for x in e.splitlines()]) for e in array]
# same