Is there a simple and beautiful way to drop items into a list for different types?

I got a list of strings from the REST API. I know from the documentation that an element with index 0 and 2 is an integer, and an element with 1 and 3 is floats.

To perform any calculations with the data, I need to cast them to the appropriate type. Although you can apply values โ€‹โ€‹every time they are used, I prefer to distinguish the list to the correct type before starting calculations to clear the equations. The code below works, but is very ugly:

rest_response = ['23', '1.45', '1', '1.54'] first_int = int(rest_response[0]) first_float = float(rest_response[1]) second_int = int(rest_response[2]) second_float = float(rest_response[3]) 

Since I work with integers and floats in this particular example, one possible solution is to throw each element for a float. float_response = map(float, rest_response) . Then I can simply unzip the list to correctly name the values โ€‹โ€‹in the equations.

 first_int, first_float, second_int, second_float = float_response 

This is my current solution (but with better names), but after finding out that one of them I was curious, is there any good pythonic solution to this problem?

+5
source share
7 answers

Define a second list that matches your types, and write it to the list of values.

 rest_response = ['23', '1.45', '1', '1,54'] casts = [int, float, int, float] results = [cast(val) for cast, val in zip(casts, rest_response)] 
+15
source

this is a solution using itertools.cycle to loop itertools.cycle translation functions:

 from itertools import cycle first_int, first_float, second_int, second_float = [cast(f) for f, cast in zip(rest_response, cycle((int, float)))] 
+5
source

The existing answer is ideal if you know the types that you expect to receive. If, however, you do not know in advance if your values โ€‹โ€‹are int or float, then you can use the AST module to safely parse the string in the appropriate type:

 import ast 

Then you call:

 numbers = [ast.literal_eval(s) for s in strings] 
+2
source

If your list is in this each other's template, you can use modulo:

 >>> [int(x) if i % 2==0 else float(x) for i,x in enumerate(rest_response)] [23, 1.45, 1, 1.54] 

Or, if you want to use the tuple assignment for named variables, you can slice and display the desired type:

 first_int, second_int=map(int, rest_response[0::2]) first_float, second_float=map(float, rest_response[1::2]) 
+1
source

yep, given that everyone is a string, just write a regular expression that checks if the string has a '.' in it, and if so, throw it afloat. Otherwise for int

edit: regex is redundant, just in string enough.

here is what i will do:

 import re def convert_type(item): if re.match('^[^.]*$', item): item = int(item) else: item = float(item) return item 

Simplified function:

 def convert_type(item): if '.' in item: return float(item) else: return int(item) what = convert_type('2') print(what, type(what)) convert_this = ['21.44', '12'] converted_list = list(map(convert_type, convert_this)) list(map(type, converted_list)) 

But I'm glad to hear a simpler and more direct solution.

0
source

I think it's wise Pythonic try to convert to int, if that fails, use float,

 rest_response = ['23', '1.45', '1', '1.54'] float_response = [] for r in rest_response: try: float_response.append(int(r)) except ValueError: float_response.append(float(r)) 
0
source

Of all the proposed solutions, the one you provide in your question is the best. It has speed and clarity.

If you want to reduce the proposed solution, you can reduce it to one line:

 first, second, third, fourth = int(lst[0]), float(lst[1]), int(lst[2]), float(lst[3]) 

I don't know all the possible formats for the input, but if the float values โ€‹โ€‹will always have a decimal number, then another solution would be:

 first, second, third, fourth = [float(i) if '.' in i else int(i) for i in rest_response] 
0
source

Source: https://habr.com/ru/post/1270922/


All Articles