Как из list сделать int python

от admin

Converting all strings in list to integers in Python

Sometimes we can have a list containing strings but the strings themselves are numbers and closing quotes. In such a list we want to convert the string elements into actual integers.

With int()

The int function takes in parameters and converts it to integers if it is already a number. So we design a for loop to go through each element of the list and apply the in function. We store the final result into a new list.

Example

Output

Running the above code gives us the following result −

With map and list

The map function can be used to apply int function into every element that is present as a string in the given list.

converting a list to integer on python

[’39’, ’40’, ’39’, ’38’, ’35’, ’38’, ’39’, ’39’, ’42’, ’37’, ’40’, ’41’, ’37’, ’39’, ’39’, ’40’, ’38’, ’40’, ’39’, ’40’]

[’39’, ’40’, ’39’, ’38’, ’36’, ’39’, ’40’, ’39’, ’42’, ’38’, ’40’, ’41’, ’38’, ’39’, ’39’, ’40’, ’38’, ’40’, ’39’, ’41’]

[’39’, ’40’, ’40’, ’38’, ’36’, ’39’, ’40’, ’39’, ’43’, ’38’, ’40’, ’41’, ’38’, ’39’, ’39’, ’40’, ’38’, ’40’, ’39’, ’41’]

i wrote this script to have a new file that looks like this

39 40 39 38 35 38 39 39 42 37 40 41 37 39 39 40 38 40 39 40

39 40 39 38 36 39 40 39 42 38 40 41 38 39 39 40 38 40 39 41

39 40 40 38 36 39 40 39 43 38 40 41 38 39 39 40 38 40 39 41

the script that i wrote is this one

the error that I get is this one

ValueError: invalid literal for int() with base 10: ‘[‘

Yu Hao's user avatar

4 Answers 4

The problem is that when you read in readline from your file, you will have a line

As you can see, the first item in your string is [ . So, you don’t actually have the numbers structured as you are expecting. Instead, since you seem to already have a list structure represented as a string, consider using literal_eval from ast :

Now you actually have a list of strings. Now you can proceed modifying that to your ints. As a simple process, you can then do something like this:

Note, when it comes to using map in Python 3 you get a map object, which returns an iterator. So, if you need the list, this is why list is called when printing. You can read about it here:

Area of Improvement

Based on the contents of the file you are reading, it would be best to not structure the data like this. Instead, what should be done is not setting the data as a list representation in to the file, but just the contents of the list. This avoids having to perform the above solution, and instead, a simple:

Как из list сделать int python

Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the entire list of strings to integers is quite common in the development domain. Let’s discuss a few ways to solve this particular problem.

Method 1: Using eval()

Python eval() function parse the expression argument and evaluate it as a python expression and runs Python expression(code), If the expression is an int representation, Python converts the argument to an integer.

Python3

Output:

Method 2: Naive Method

This is the most generic method that strikes any programmer while performing this kind of operation. Just looping over the whole list and converting each string of the list to int by type casting.

Python3

Output:

Method 3: Using list comprehension

This is just a kind of replica of the above method, just implemented using list comprehension, a kind of shorthand that a developer looks for always. It saves the time and complexity of coding a solution.

Python3

Output:

Method 4: Using map()

This is the most elegant, pythonic, and recommended method to perform this particular task. This function is exclusively made for this kind of task and should be used to perform them.

Python3

Output:

Method 5: List of strings with mixed integer representations

Here, we will first convert each string to a float first and then we will convert it into an integer by using the round() function, otherwise, it will through error.

Convert float to int Python + Examples

To convert a float to int in python, we will use the built-in int() function. This function will return the integer number part.

Example:

You can refer to the below screenshot to see the output for how to convert float to int python.

How to convert float to int python

This is how to convert float to int in Python.

How to transform float to int Python

Now, we will see how to transform float to int python

In this example, we will use trunc() function. The trunc() function returns the integer part of the number. It ignores everything after the decimal points.

Читать:
Как найти вершину кубической параболы

Example:

You can refer to the below screenshot to see the output for how to transform float to int python.

How to transform float to int python

The above Python code we can use to transform float to int in Python.

How to convert float list to int in Python

Here, we will see how to convert float list to int in python

To convert float list to int in python we will use the built-in function int and it will return a list of integers.

Example:

You can refer to the below screenshot to see the output for how to convert float list to int in python.

How to convert float list to int in python

The above code, we can use to convert float list to int in Python.

How to convert float to whole number python

Let’s see how to convert float to whole number python

In this example, we will use the built-in round() function which will round up the value and returns the integer value.

Example:

You can refer to the below screenshot to see the output for how to convert float to whole number python.

How to convert float to whole number python

The above code, we can use to convert float to whole number in Python.

Python convert float to int ceil

Let us see how to convert float to int ceil.

In this example, we will use ceil() function which will rounds up the next full integer value.

Example:

You can refer to the below screenshot to see the output for python convert float to int ceil.

Python convert float to int ceil

This is how to convert float to int ceil in Python.

Convert float array to int in python

Now, we will see how to convert float array to int in python.

To convert float array to int in python we will first import numpy as np and then we will use the function astype() and it will return the integer.

Example:

You can refer to the below screenshot to see the output for convert float array to int in python.

Convert float array to int in python

The above code, we can use to convert float array to int in python.

Convert float to int Python numpy

Here, we will see how to convert float to int python numpy.

In this example, we will import numpy as np and the built-in function astype() is used to convert float to int python numpy.

Example:

You can refer to the below screenshot to see the output for convert float to int python numpy.

Convert float to int python numpy

The above Python code, we can use to convert float to int Python numpy.

How to convert negative float value to int in python

Let’s see how to convert negative float value to int in python.

To convert the negative float value to int in python, we have to use the int() function. Also, we have to pass the float variable as the argument of the int() in python.

Example:

You can refer to the below screenshot to see the output for how to convert negative float value to int in python.

How to convert negative float value to int in python

This is how to convert negative float value to int in python.

How to convert float variable to int in python

Now, we will see how to convert float variable to int in python.

To convert float variable to int in python, we will use the int() function. Also, we have to pass the float variable as the argument of the int() in python, and to get the output use the print() function.

Example:

You can refer to the below screenshot to see the output for how to convert float variable to int in python.

How to convert float variable to int in python

The above code, we can use to convert float variable to int in python.

You may like the following Python tutorials:

In this Python tutorial, we have learned about how to convert float to int python. Also, we covered these below topics:

  • How to convert float to int python
  • How to transform float to int python
  • How to convert float list to int in python
  • How to convert float to whole number python
  • Python convert float to int ceil
  • Convert float array to int in python
  • Convert float to int python numpy
  • How to convert negative float value to int in python
  • How to convert float variable to int in python

Bijay Kumar MVP

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.

Related Posts