Home > Software engineering >  Explanation of Python code - integers split
Explanation of Python code - integers split

Time:10-16

RTX_number = [int(x) for x in input().split()]

Could someone explain this line of code to me step by step? I am having great difficulty understanding it. As far as I know, .split creates spaces between elements?

I saw this code on a forum and I am trying to get a better understanding of it because I think it might be helpful for a simulation project.

I heard this is called a list comprehension, but I am kind of lost as of for now.

CodePudding user response:

input().split()

Reads a line and breaks it where ever there is a space into a list of strings.

for x in input().split()

Takes this list, runs over it item by item, and binds this item to x.

int(x) for ...

Takes this x we bound, and runs int(x) on it and returns it.

[int(x) for x in input().split()]

Takes all these results and puts them into a list.

CodePudding user response:

The short version is that this:

RTX_number = [int(x) for x in input().split()]

is a short-form of this:

RTX_number = []
for x in input().split():
    RTX_number.append(int(x))

where input().split() returns the list of strings that you get from separating whatever input() returned on each whitespace (for example, "Hello World" becomes ["Hello", "World"].

The str.split() function can also be given an argument, such as ',', which it will split on instead of whitespace.


The general syntax of a comprehension is

(expression) for (element) in (iterable) if (condition)

For every element element in the iterable, if the condition resolves to True (note that the condition can be omitted entirely) the expression is evaluated and added to the returned list.

We usually use comprehensions as shorthand for full loops, since they often save space and complexity.

Note that list comprehensions aren't the only kind of comprehension - they can be used to make several different data structures:

# list comprehension - produces a list
[expression for element in iterable] 

# set comprehension - produces a set instead of a list
{expression for element in iterable} 

# dict comprehension - produces a dict with key-value pairs
{key:value for element in iterable}  

# generator comprehension - like a list comprehension, but each expression is not
# actually evaluated until something tries to read it. 
# The parentheses are technically optional, if the syntax isn't ambiguous
(expression for element in iterable) 

CodePudding user response:

This code is equivalent to:

# ask user input (it expected something like "1 2 34 5")
input_text = input()

# split on spaces
split_text_list = input_text.split()

list_of_integers = []

# for each string item in list
for item in split_text_list:
    # convert to integer
    number = int(item)
    # add to list
    list_of_integers.append(number)

But of course it avoids having all the unnecessary intermediate variables, so it is shorter. Also faster as it doesn't require to store the intermediate values.

  • Related