Post

Python Programming
Python Programming@PythonPr·
Python Trick: Square of List elements
Python Programming tweet media
English
5
12
91
4K
Sergei Kotov
Sergei Kotov@kotov_dev·
@PythonPr map() is cleaner than a manual loop. But list comprehension is even better: squares = [n**2 for n in List] One line. No extra function needed. Readable, fast, and Pythonic.
English
0
0
1
33
Asotsiaalne Autist
Asotsiaalne Autist@asotsiaalne·
@PythonPr I don't know what map is, looks too complex. The solution is even simpler. In [1]: list = [1, 2, 3, 4, 5] In [2]: squares = [x**2 for x in list] In [3]: print(squares) [1, 4, 9, 16, 25]
English
0
0
1
16
Bob Armstrong
Bob Armstrong@CoSyBobA·
@PythonPr i( 1 2 3 4 5 )i .. *i |>| 1 4 9 16 25 duplicating & multiplying /\ i( 1 2 3 4 5 )i 2 _i ^i |>| 1 4 9 16 25 actual squaring /\ FORTH.CoSy simplified & generalized from APL & open vocabulary to the hardware as only possible in FORTH .
English
0
0
0
28
Paylaş