list2 = ['BIA-660', 'Web', 'Analytics']
Question: How to extract "660" from list2
list2 = ['BIA-660', 'Web', 'Analytics']
c=list2[0]
x=c.split("-")
print(x[1])
I am getting the answer but Wanted to know if there is another more efficient way of achieving the solution.
You could use Regex to get any digits from a string
import re
list2 = ['BIA-660', 'Web', 'Analytics']
for strg in list2:
found = re.findall('\d+', strg)
if found != []:
for num in found:
print(num)
Output:
660
This will return a list of numbers contained in the string:
list2 = ['BIA-660', 'Web', 'Analytics']
s = '-'.join(list2)
# extract numbers from string
result = [int(txt) for txt in s.split('-') if txt.isdigit()]
print(result)
[660]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With