Home > Software engineering >  How to get value in Python?
How to get value in Python?

Time:12-23

['time=139', 'time=102', 'time=120', 'time=105', 'time=116', 'time=183', 'time=153', 'time=100', 'time=151', 'time=123', 'time=155', 'time=135']

how to get only number from these list?

I tried to get the value from this list, I don't how to just get the number?

CodePudding user response:

You can use split() and get the value after = sign.

numbers = [int(string.split('=')[1]) for string in ['time=139', 'time=102', 'time=120', 'time=105', 'time=116', 'time=183', 'time=153', 'time=100', 'time=151', 'time=123', 'time=155', 'time=135']]

CodePudding user response:

import re
numbers = re.findall(r'\d ', ''.join(['time=139', 'time=102', 'time=120', 'time=105', 'time=116', 'time=183', 'time=153', 'time=100', 'time=151', 'time=123', 'time=155', 'time=135']))
#['139', '102', '120', '105', '116', '183', '153', '100', '151', '123', '155', '135']

To convert them to int type:

[int(x) for x in numbers]

CodePudding user response:

from re import split
lst=['time=139', 'time=102', 'time=120', 'time=105', 'time=116', 'time=183', 'time=153', 'time=100', 'time=151', 'time=123', 'time=155', 'time=135']
string="".join(lst)
A=split("time=",string) #splited buy "time="
lst=A[1:]
print(lst)

CodePudding user response:

Fetch while iterating over a list and write values to a new list or convert to a dictionary to user in further code.

Sample code:

a_lst = ['time=139', 'time=102', 'time=120', 'time=105', 'time=116', 'time=183', 'time=153', 'time=100', 'time=151', 'time=123', 'time=155', 'time=135']
a_values = []
for item in a_lst:
    a_values.append(int(item.split("=")[1]))
print(a_values)

List comprehension way:

a_values = [ int(item.split("=")[1]) for item in a_lst]
  • Related