+ 4
Variable ReInitialisation within a oneliner?[solved]
I thought about oneliners and the only thing I cannot put in one line is the reintialisation of a variable. Example: [[[set x to 3 here],print(x)] for x in [5]] >>Output 3 So I would like to find a way how this is possible. If you can help me I would be very thankful! https://code.sololearn.com/ca7h1Q9erva4/?ref=app
12 Réponses
+ 10
You could use "the walrus operator" ( := ) instead of the regular equal sign ( = ) to assign to variables within an expression.
So the code would look something like this:
[[[x:=3], print(x)] for x in [5]]
Visit the link below for more details:
https://www.python.org/dev/peps/pep-0572/
+ 11
Alexander Thiem , good to see that you could solve your 'problem'. I only wanted to mention, that the := operator is available from python 3.8, so it will not work with all versions before.
+ 5
Your welcome. Glad you liked the answer!
+ 4
Ali Shah Jindani
Although your special code extract gave:
SyntaxError: assignment expression cannot rebind comprehension iteration variable 'x'
I know have a solution for my proplem!
[x:=5,[print(x),[x:=3],print(x),]]
makes variable reinitialisation in oneline possible. Thank you very much!
+ 4
Sorry, another rant incoming.
I have just read the Walrus PEP. First impression: Oh my God, what have you done to Python. Whatever happened to the Zen? Simple is better than complex?
Walrus is all about creating side effects, all the while having many of these very specific rules about where you cannot use it, where you can but shouldn't use it.. Blows my mind. I get it that it can sometimes simplify a few lines.. But at what cost? It represents the exact opposite of what I value and adore in Functional Programming.
No wonder Guido jumped ship after this.
/rant off
+ 3
pRoGRammer py Of course:
If you have a code like:
for i in range(int(input())):
if i>10:
print(i)
else:
print(„ „+str(i))
you can also write it in one line:
[[[print(i)] if i>10 else [print(„ „+str(i))]] for i in range(int(input())]
This is possible for:
1. for loops
2. function calls
3. if conditions
and know appearently for
4. assignments
too
+ 3
Alexander Thiem do you have any specific problem in mind where reassignment seems inevitable?
I haven't used the walrus all that much yet, but I am a little at odds with it. I would probably look for a more functional solution instead, involving map or reduce.
+ 3
No I have no specific problem in mind. I just thought about whether every code is convertable in a oneliner and how. Variable reinitialisation was a problem, but know that is solved too. I will netherless look at map and reduce!
+ 3
Tibor Santa Moreover using functional solution, you are using a different and maybe more difficult solution, instead of just formatting the normal code you wrote in a oneliner.
And as C++ was and is my major programming language I have not much experience with map and reduce and so on. One more reason to look at map and reduce....
+ 3
Tibor Santa I totally agree! It maked things to complex! why arent they changing it to: There is only the = operator, but it works as the walrus operator if necessary and works therefore everywhere, without limitations
+ 2
As you cannot manipulate a single index of a List with the Walrus operator:
x[3]=1 does not work
You have to conpletely renew the List to change only a single index!
https://code.sololearn.com/cl1FENMv4R8v/?ref=app
0
Can you explain more briefly?