Python Forum

Full Version: [split] question about list comprehension
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello everyone I hope someone can explain this cod to me Confused there is the variable y [ ] – list how this code works. hope I'm not too boring. Here we have an input number but since Python is the default input string then it is a string and if i type 10 for example the result is 6 which is true but do not understand how y takes this number and also what does works hier “.split” bult-in functions and in the next line we attribute the same later to the variable ‘’d’. Thanks a lot in advance
# calculate Q
c = 50
h = 30
# use thi formule Q = square root of [(2*c*d)/h]
import math
x = []
y = [i for i in input('give me a number: ').split(',')]   # ?????
for d in y:
    x.append(str(int(round(math.sqrt((2*c*float(d)/h))))))
print(','.join(x))
program will remain in for loop until enter button pressed, then contents will be split.
Think of it in its expanded form
>>> i = input('Give me a number')
Give me a number1, 2, 3
>>> y = i.split(',')
>>> y
['1', ' 2', ' 3']
Message should specify that numbers should be separated by ',' and terminated on enter
Thanks for your reply what bothers me most about this code is line 7 this part y = [i for i in input ('give me a number:') i know this is list comprehensions but i don't understand it how this line of code number 7 works ??
(Jan-12-2020, 03:43 PM)Armin Wrote: [ -> ]Thanks for your reply what bothers me most about this code is line 7 this part y = [i for i in input ('give me a number:') i know this is list comprehensions but i don't understand it how this line of code number 7 works ??

[i for i in input('give me a number: ').split(',')]
number_string = input('give me a number: ') # "1, 2, 3"
numbers = number_string.split(',')          #[1, 2, 3]
It's really overkill
input('give me a number: ').split(',')
Is functionally equivalent
y = [i for i in input('give me a number: ').split(',')]
     ^this is the expression i that will replace every instance of i where the condition is true

y = [i for i in input('give me a number: ').split(',')]
       <------> setting up a for loop with i

y = [i for i in input('give me a number: ').split(',')]
                <------------------------------------> this is actually two things stuck together.
input() returns string or empty string always.
the .split method is something you throw on the end of an object such as a string.
the .split() method always returns string, a list of strings or empty string if there is no error from type mismatch or missing arguments.
because input() returns a string, input().split is totally fine just like "1,2,3".split(',')
also something = "1,2,3"
print(something.split(','))
output
1 2 3
for loops work on an object that is iterable. the list ['1', ' 2', ' 3'] is iterable. 1 is not iterable.
Thanks a lot for helping me understand this but now if I enter say 10 in this case the string it will be .split () will be output to '1' and '0' as then in the following line I assign y the value of 10 variables di then inside the formula I get result 6? I apologize in advance if I'm bored, but I've been messing with this code all day and can't handle it completely.
Thank you all very much
Quote:if I enter say 10 in this case the string it will be .split () will be output to '1' and '0'
That's not how split works.
split requires a delimiter. If none is supplied space is assumed.
the results of a split are returned in a list

remember, the best way to learn is to try.
bring up the python interpreter so you can try for yourself (just type python3 at command line)
some examples:

example 1, no delimiter specified, so default space is used
>>> string1 = "This is a string"
>>> string1.split()
['This', 'is', 'a', 'string']
example 2 specifing a delimiter:
>>> string2 = "This string has a hyphenated-word in it"
>>> string2.split("-")
['This string has a hyphenated', 'word in it']
>>>
useful for separating items delimited with special characters:
example3:
>>> data = "10/18/2016|ALA|16|970|827|540|2337"
>>> dlist = data.split('|')
>>> dlist
['10/18/2016', 'ALA', '16', '970', '827', '540', '2337']
>>>
Are you trying to split something like 752 into [7, 5, 2]?
If so then read about integer math.
You will find the modulo and integer division or particular interest.
Thank you very much
I understand split nouw but what i can't understand is this part of the code in line 7
“y = [i for i in input “
this part of the code and why this code is there because the program works without this part of the code if we put in line 7 above, for example
y = input('give me a number: ').split(',')
we get the same result. I understand that this is the work of codo o comprehensive list but not understood why it's there"i in i for input " in lijn 7.
Thanks in advance
Pages: 1 2