Home > database >  For help, a nested loop problem
For help, a nested loop problem

Time:11-14

Why is the output of the 42? Only when j1 output is 5, only j3 when output is 9, how did this cycle?

CodePudding user response:

Loop, the first layer is in accordance with the len (J) the length of the cycle, such as J contains 1 to 6, length is eight, then I is 0,1,2,3,4,5
The number of the second loop, num finally add 1 is len (j [I] [x]), I=0 loop out when is len (j1 [x]) the value of the
So,
This function is calculated, all the dict J, length and finally + J x yuan length
Len (j1 [x])=4
Len (j2 [x])=4
Len (j3 [x])=8
Len (j4 [x])=5
Len (j5 [x])=7
Len (j6 [x])=8

If only the j1, len (J) + len (j1 [x])=1 + 4=5
If only the j3, len (J) + len (j1 [x])=1 + 8=9
J have 1 to 6, len (J) is 6, len (J) + len (j1). + len (j6)=6 + 4 + 4 + 5 + 7 + 8 + 8=42

CodePudding user response:

sunny flowers quote 2 floor response:
layer 1 cycle, is according to len (J) the length of the cycle, such as J contains 1 to 6, length is eight, then I is 0,1,2,3,4,5
The number of the second loop, num finally add 1 is len (j [I] [x]), I=0 loop out when is len (j1 [x]) the value of the
So,
This function is calculated, all the dict J, length and finally + J x yuan length
Len (j1 [x])=4
Len (j2 [x])=4
Len (j3 [x])=8
Len (j4 [x])=5
Len (j5 [x])=7
Len (j6 [x])=8

If only the j1, len (J) + len (j1 [x])=1 + 4=5
If only the j3, len (J) + len (j1 [x])=1 + 8=9
J have 1 to 6, len (J) is 6, len (J) + len (j1). + len (j6)=6 + 4 + 4 + 5 + 7 + 8 + 8=42

J length is 8 is why?

CodePudding user response:

sunny flowers quote 2 floor response:
layer 1 cycle, is according to len (J) the length of the cycle, such as J contains 1 to 6, length is eight, then I is 0,1,2,3,4,5
The number of the second loop, num finally add 1 is len (j [I] [x]), I=0 loop out when is len (j1 [x]) the value of the
So,
This function is calculated, all the dict J, length and finally + J x yuan length
Len (j1 [x])=4
Len (j2 [x])=4
Len (j3 [x])=8
Len (j4 [x])=5
Len (j5 [x])=7
Len (j6 [x])=8

If only the j1, len (J) + len (j1 [x])=1 + 4=5
If only the j3, len (J) + len (j1 [x])=1 + 8=9
J have 1 to 6, len (J) is 6, len (J) + len (j1). + len (j6)=6 + 4 + 4 + 5 + 7 + 8 + 8=42

Why to add x '?
For I in range (len (J) :
For j in range (len (j '[I] [x]) :
Print (J [I] [' x '] [J])
Output is why all the x value output

CodePudding user response:

J length is the number of element
If only 1 J dic, length is 1
If there is a j1 - j6 J, J length is 6.

CodePudding user response:

Len (A [I] [' x ']) why do x quotes?

CodePudding user response:

Why to want to add x "'
Dic because x is an element name, is itself a list, you can test it, do not add 'is an
J [I] [' X '] [J] output is the value of X,
One layer to see
Such as I=0, j=0
J [0] is the zeroth element J, namely j1
J1 [' X '] get is,4,4,4 [4]
,4,4,4 [4] the zeroth element is 4
You can add the knowledge of the sequence, here is a little round, but as long as see layers do not have what problem

The length of the sequence is the number of elements;;; Don't confuse
  • Related