Python Forum
Unable to understand the meaning of the line of code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to understand the meaning of the line of code.
#1
Am unable to understand the meaning of the code on the r.h.s. of the below line of code, as given here.

lambda A: sum(2*(A[a]==a)-(A[A[a]]==a) for a in A)
The array 'A' here is: [0, 1, 2, 3], with 'a' taking any of the four values in 'A'.
So,
A[a]==a 
implies any of the four below situations, all of which are satisfied for the identity permutation.
1. 0123 : identity permutation,
2. 0213 : here A[0]=0, A[3]=3,
3. 0132 : here A[0]=0, A[1]=1,
4. 1023 : here A[2]=2, A[3]=3,
5. 1203 : here A[3]=3,
6. 1320 : here A[2]=2,
7. 2103 : here A[1]=1, A[3]=3,
8. 2013 : here A[3]=3,
9. 3120 : here A[1]=1, A[2]=2,
10. 3102 : here A[1]=1,

In fact, don't know how to calculate using combinatorics, how many such combinations are possible where at least for one value of 'a', have: A[a]==a.

But, that is not the subject here.

The main concern is finding out the meaning of the given code line.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,693 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Csv writer meaning of quoting mg24 2 1,163 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  meaning of -> syntax in function definition DrakeSoft 5 1,977 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 6,925 Mar-02-2022, 01:23 AM
Last Post: tomtom
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,415 Feb-13-2022, 07:13 PM
Last Post: menator01
  Operator meaning explanation Sherine 3 2,053 Jul-31-2021, 11:05 AM
Last Post: Sherine
  how long can a line of code be? Skaperen 2 2,220 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,713 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Don't understand example code from a textbook lil_fentanyl 1 1,844 Jan-25-2021, 07:02 PM
Last Post: nilamo
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,136 Dec-06-2020, 10:10 PM
Last Post: Aizou

Forum Jump:

User Panel Messages

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