How to write the following in list comprehension in python -


Can I easily type in the Python for candidates in the

  list : (= [I]! = 'NA') sum = = int (line [i]) line .append (sum)   

here candidate is a list of candidates whose list contains candidates marks in each internal list What am I trying to do?

"I Internal Increasing one member in the list, which is the total number of each candidate, such as " candidate =" [[143050023,5,7,6, 8], [14305678,3,4,5,6]]

[[143050023,5,7,6,8,26], [14305678] 3,4,5,6,18]]

I am able to do this without using an understanding of the list, but with difficulty understanding the list is facing It is difficult to try that I am facing in writing. Value is how connects and adds it to the line after each inner loop.

When the understanding of the list is used, loop structure is better than normal for the structure?

Let's start with your last question:

Use the list When doing the loop structure better understanding than normal?

Two things must be true:

  • The purpose of your loop is to create a list.
  • It is more readable to understand than a clear loop.

    Note that you have two nested loops, this is the reason that may be section, or a nested understanding, But it is going to be transformed into a flat understanding, to be transformed into an understanding with two .

    There is a problem in your inner loop: one should not change anything to understand, and can not change anything with the assignment or other statement, then you can do it again To write is enough to find a way that makes his work irreversible.

    In this case, the clear answer is to be expressed as the value to be expressed, and then for a function iterable to pass, resulting in that range (1, in line (line)) For the buildin sum function:

      sum (int (line [i]) tells them if line [I]! = 'NA')   

    I used a generator expression instead of understanding the list because we actually did not leave the loop on something The list is not needed.

    Note that you can simplify it further. The only thing that you are ever using i (repeated) in expression is line [i] . Then why repeat on the indexed range, when you can be repeated on the straight line?

      yoga (value in line value = int = = NA ') =   

    There is a similar problem with your external loop: Want to change the code inside the loop, not a new framework. And instead it is difficult to come up with a good option, which instead fulfills the same thing by building a new structure.

    Of course, you can always work in two passes:

      sums = [sum (int (line [i]) for class I (1, Line (candidates) for the row in the candidates [[]] line = [row + [row], the sum in the zip (candidates, amount)]   

    and you two Once again, changing the passes to sums can be transformed into a generator expression instead of understanding the list, and instead you can change it to one-line instead of changing it in one place. To use the designated floating variable:

      Candidates [:] = [Row + [total] for line, total in zip (candidates, (zodiac (int (line [i] ]) I have the line (1, lane (candidates)) for the line in the candidates if line [ii]! = 'Na'))   

    But it is difficult to argue that this Also readable or even far from

    Also, note that I called the variable which is the sum of each total , sum . In Python (and indeed we are using this answer) a sum function has been made a very important one, so one of the same names It's a bad idea to hide it by creating variables. Even in this case, where it simply does not use LISTIN, Lists is alive within the scope of use, so it is not ambiguous for the interpreter, it is still unclear and the human reader is confused. (Thanks for pointing it out.)

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -