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
  Difference and meaning in this scrips Udbytossen 4 923 Jun-29-2025, 03:41 PM
Last Post: Gribouillis
  Unable to understand the function string.split() Hudjefa 8 4,809 Sep-16-2024, 04:25 AM
Last Post: Pedroski55
  Printing the code line number arbiel 6 2,484 Jun-30-2024, 08:01 AM
Last Post: arbiel
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 3,561 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Csv writer meaning of quoting mg24 2 2,671 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  meaning of -> syntax in function definition DrakeSoft 5 4,721 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
Photo Visual studio code unable to color syntax on python interpreter tomtom 4 16,029 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 4,214 Feb-13-2022, 07:13 PM
Last Post: menator01
  Operator meaning explanation Sherine 3 3,508 Jul-31-2021, 11:05 AM
Last Post: Sherine
  how long can a line of code be? Skaperen 2 3,437 Jun-09-2021, 06:31 PM
Last Post: Skaperen

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.