Python Forum
[split] question about list comprehension
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] question about list comprehension
#11
please carefully read post #2. I have broken that line up into pieces to make it clearer.
The for i in i just means that you want to return value of i after performing rest of statement
If you carefully read post 2 and still don't understand, I'll think of a way to make it clearer.
Reply
#12
(Jan-13-2020, 11:22 AM)Armin Wrote: 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

This list comprehension is already described by Clunk_Head as overkill (and I agree with that assessment). There is no need for that, you can just split the input on comma and get the same result. As why it is written this way - you should consult the author.

Regarding list comprehension: usage of it could be justified if string is converted into float thus avoiding converting it later:

>>> y = [i for i in input('give me a number: ').split(',')]            # original
give me a number: 4,2
>>> y
['4', '2']
>>> y = input('give me a number: ').split(',')                         # code provided by Clunk_Head with same result                 
give me a number: 4,2
>>> y                                                               
['4', '2'] 
>>> y = [float(i) for i in input('give me a number: ').split(',')]     # with conversion to float  
give me a number: 4,2
>>> y                                                               
[4.0, 2.0]
If the task is to iterate over the numbers only once then the generator expression could be used.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#13
(Jan-12-2020, 05:11 PM)Armin Wrote: 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

a = "10"
print(a[i] for i in range(len(a)))
output:["1","0"]
Reply
#14
(Jan-12-2020, 05:11 PM)Armin Wrote:
a = "10"
print(a[i] for i in range(len(a)))
output:["1","0"]

This is unpythonic pattern (I call it javasque).

Python way is:

[char for char in a]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#15
I didn't know you can do that! I asked my teacher if there was a char data type in python and he said no. only string.

is there a char trap in string.__iter__?
Reply
#16
There is no char datatype. Char is temporary name which is used during iteration. This can any name which is compliant with Python naming convention. For better readability it’s common to use descriptive names “char” or “letter” if iterating over letters, “i” or “num” while iterating over integers etc.

Readability-wise it should be quite obvious what is going on:

[char for char in word]        
[num * num for num in nums]
[word for word in sentence]    
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#17
ok yes that makes sense but I don't think I will use char as a variable name because that would totally confuse me. so basically because a string.__iter__ exists, it can be used in this way.
Reply
#18
(Jan-13-2020, 11:22 AM)Armin Wrote: 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

Wall You don't understand what it's doing because it's not doing anything. It's poorly written, redundant code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 506 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 470 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,244 Apr-02-2023, 06:31 PM
Last Post: tester_V
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,507 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  list comprehension 3lnyn0 4 1,394 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  Split string using variable found in a list japo85 2 1,295 Jul-11-2022, 08:52 AM
Last Post: japo85
  [split] [split] New to the forum, how to post a question? karnik 2 1,273 Feb-12-2022, 03:45 PM
Last Post: deanhystad
  List comprehension used differently coder_sw99 3 1,710 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,823 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,234 Jun-08-2021, 08:29 AM
Last Post: cametan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020