Reputation: 313
I have a list which contains the following string
element.
myList = ['120$My life cycle 3$121$My daily routine 2']
I perform a .split("$")
operation and get the following new list.
templist = str(myList).split("$")
I want to be able to store all the integer values from this templist
which are at even indexes after the split.I want to return a list of integers.
Expected output: [120, 121]
Upvotes: 0
Views: 378
Reputation: 4763
Updated version:
import re
myList = ['120$My life cycle 3$121$My daily routine 2']
myList = myList[0].split('$')
numbers = []
for i in range(0,len(myList),2):
temp = re.findall(r'\d+', myList[i])[0]
numbers.append(temp)
'''
.finall() returns list of all occurences of a pattern in a given string.
The pattern says map all digits in the string. If they are next to each
other count them as one element in final list. We use index 0 of the
myList as thats the string we want to work with.
'''
results = list(map(int, numbers)) # this line performs an int() operation on each of the elements of numbers.
print(results)
Why not just use re
?
re
is a library for regular expresions in python. They help you find patterns.
import re
myList = ['120$My life cycle 3$121$My daily routine 2']
numbers = re.findall(r'\d+$', myList[0])
'''
.finall() returns list of all occurences of a pattern in a given string.
The pattern says map all digits in the string. If they are next to each
other count them as one element in final list. We use index 0 of the
myList as thats the string we want to work with.
'''
results = list(map(int, numbers)) # this line performs an int() operation on each of the elements of numbers.
print(results)
Upvotes: 2
Reputation: 51643
You can split at $ and use a list comprehension with str.isdigit()
to extract numbers:
mylist = ['120$My life cycle$121$My daily routine','some$222$othr$text$42']
# split each thing in mylist at $, for each split-result, keep only those that
# contain only numbers and convert them to integer
splitted = [[int(i) for i in p.split("$") if i.isdigit()] for p in mylist]
print(splitted) # [[120, 121], [222, 42]]
This will produce a list of lists and convert the "string" numbers into integers. it only works for positive numbers-strings without sign - with sign you can exchange isdigit()
for another function:
def isInt(t):
try:
_ = int(t)
return True
except:
return False
mylist = ['-120$My life cycle$121$My daily routine','some$222$othr$text$42']
splitted = [[int(i) for i in p.split("$") if isInt(i) ] for p in mylist]
print(splitted) # [[-120, 121], [222, 42]]
To get a flattened list no matter how many strings are in myList
:
intlist = list(map(int,( d for d in '$'.join(myList).split("$") if isInt(d))))
print(intlist) # [-120, 121, 222, 42]
Upvotes: 5
Reputation: 8710
Do something like this?
a = ['100$My String 1A$290$My String 1B']
>>> for x in a:
... [int(s) for s in x.split("$") if s.isdigit()]
...
[100, 290]
Upvotes: 0
Reputation: 320
First off we split string with '$'
as separator. And then we just iterate through every other result from the new list, convert it into integer and append it to results.
myList = ['120$My life cycle 3$121$My daily routine 2']
myList = myList[0].split('$')
results = []
for i in range(0,len(myList),2):
results.append(int(myList[i]))
print(results)
# [120, 121]
Upvotes: 1