Python Forum
Decypher complex (to me) statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decypher complex (to me) statement
#1
I am a C/C++ programmer (I have an inkling of classes and OOD) who has just been given his first Python task. My former co-worker who enjoyed code obfuscation wrote the following line of code:

event_arr = [x for x in _485_trace.split(" ") if x]

This is applied to a string with the format:

UC_COMMAND,bOptionalArg1,bOptionalArgN\n

where UC_COMMAND is an all uppercase string.
It has 0 or more positive int arguments, separated by a comma and exactly one blank (b) and terminated with a newline.

It looks to me like an array is being created and I infer from the brackets a list is being created on the fly. I think I understand the for clause but not the preceding 'x' nor the trailing 'if x'/

Your assistance would be much appreciated.
Reply
#2
It is called list comprehension.
And it is equivalent to:
event_arr = []
for x in _485_trace.split(" "):
    if x:
        event_arr.append(x) # the first x in the expression
[x for x in _485_trace.split(" ") if x] appends x to event_arr if there is non-empty x.
str.split method splits the strin by a blank characters so the argument " " is not necessary.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Are you sure it isn't event_arr = [x for x in _485_trace.split(",") if x]? As wavic says, split with a space will split where there is a space. However, there are no spaces in the sample input you gave. It seems like comma separated values, so I'd think you would pass a comma to split to get ["UC_COMMAND", "bOptionalArg1", "bOptionalArgN\n"].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Jul-31-2018, 12:51 PM)ichabod801 Wrote: Are you sure it isn't event_arr = [x for x in _485_trace.split(",") if x]? As wavic says, split with a space will split where there is a space. However, there are no spaces in the sample input you gave. It seems like comma separated values, so I'd think you would pass a comma to split to get ["UC_COMMAND", "bOptionalArg1", "bOptionalArgN\n"].

I started to write more or less same question then I saw he uses b as a placeholder for blank:

(Jul-31-2018, 12:05 PM)PreservedKillich Wrote: 0 or more positive int arguments, separated by a comma and exactly one blank (b)

UC_COMMAND, OptionalArg1, OptionalArgN\n

So I guess they want to keep comma after the integer
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Btw, welcome to the forum! Smile
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Thanks everyone.
Reply


Forum Jump:

User Panel Messages

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