## python. counting in nested list the max of 3rd element of each element according to the second element

I know the title is bit hard to understand. Sorry for that, let me explain. for example: we have a nested list consist of 4 lists each with 3 elements, the first two are strings(*not char*) and the third one is an int:

LLList = [ ["A", "B", 30], ["C", "B", 30], ["D", "B", 20], ["D", "L", 60] ]

I want to count the number or return all indexes of elements in the "LLList" (elements in the "LLList" are the triple-element lists like ["A", "B", 30]) where the third element is greatest where the second element is "B".

A naive way would be to find all elements whose second elements is "B" and put it in a list. in this case would be:

TempList = [] for w in LLList if w[1] == "B" TempList.append(w)

The result would be:

TempList = [ ["A", "B", 30], ["C", "B", 30], ["D", "B", 20] ]

Then find the max value among the 3rd elements in TempList in this case would be 30

MaxB = max(TempList, key = lambda x:x[2])[2] # MaxB = 30

And count the occurrence of MaxB in the 3rd column of TempList

[p[2] for p in TempList].count(MaxB)

In this case would be 2

This is a overly naive method, too much to code, need too much RAM and time to complete, I know it, but I cannot think of anything better than this. I know there must be an elegant and effective way to solve this problem, please help me, thank you very much!

Simple way using numpy.amax and list comprehension.

This will find your values:

import numpy as np maxB = [x for x in LLList if x[1]=='B' and x[2] == np.amax([x[2] for x in LLList if x[1]=='B'])]

It finds all the values that have B in x[1], and x[2] is equal to the maximum of the values with the B.

After that you just need to find the indexes:

[LLList.index(x) for x in maxB]

**How to use Python's min() and max() with nested lists – dbader.org,** Sometimes this is referred to as a nested list or a lists of lists. We want Python to select the minimum and maximum element based on each item's weight stored (in the sense that it'll give an answer that we intuitively expect; like 3 > 2 ). Sequence objects may be compared to other objects with the same sequence type. In this article we will discuss different ways to count number of elements in a flat list, lists of lists or nested lists. Count elements in a flat list. Suppose we have a list i.e. # List of strings listOfElems = ['Hello', 'Ok', 'is', 'Ok', 'test', 'this', 'is', 'a', 'test'] To count the elements in this list, we have different ways.

dict_ans = {} for sub in LLList: if sub[1] == 'B': dict_ans[sub[2]] = dict_ans.get(sub[2], 0) + 1 print (dict_ans) #{30: 2, 20: 1} print (max(value for key, value in dict_ans.items()) #2

**Python,** Given list of lists in Python, write a Python program to find maximum value of list each Python code to Find maximum of list in nested list Sort a list according to the second element in sublist · Python | Multiply each Average of Cubes of first N natural numbers · COVID-19 Data Visualization using matplotlib in Python I posted I need the min of the first element and max of the second, should have added that is in the same pair. In the list [0,36], 0 is min of the first element and 36 would be the max for second in the pairs. When my statement posted above runs it gets the 0 but doesn't get the max value paired with it, it returns 18.

You can also use `collections.Counter`

:

from collections import Counter LLList = [ ["A", "B", 30], ["C", "B", 30], ["D", "B", 20], ["D", "L", 60] ] a = Counter([i[2] for i in LLList if i[1]=="B"]) print (a.most_common(2)) #[(30, 2), (20, 1)] print (sorted(a.items(),key=lambda x: x[0],reverse=True)) #or get highest value #[(30, 2), (20, 1)]

**Python,** a nested list. Given a list of lists, write a Python program to find the list with maximum length. This is a brute force method in which we iterate through each list item(list) and find the list with maximum length. Similarly Approach #3 : Using lambda operator first_page Python | Insert after every Nth element in a list. Next. The syntax of the list count function is. list_name.count(list_item) The python count function counts the total number of times the item repeated in a given list. The below code count 10 and 20 in an integer list.

**A Primer on Scientific Programming with Python,** Construction a.remove(e) 'run.py' in a a.count(v) len(a) min(a) max(a) sum(a) sorted(a) to an element's value test if a value is contained in the list count how many reversed sorted version of list a b[3][0][2] nested list indexing isinstance(a , list) is An object a containing an ordered collection of other objects such that a [i] Python List count() The count() method returns the number of times the specified element appears in the list.

**Programming for the Puzzled: Learn to Program While Solving Puzzles,** On the other hand, if we had declared sched = [[6, 8], [6, 12], . Line 8 invokes a function that fills in a list count that will contain, for each time between start and Python provides a function max to find the maximum element of a list. If we have b = a[0:3], this means that the first three elements of a, namely a[0], a[1], and Negative List Indexing In a Nested List. You can access a nested list by negative indexing as well. Negative indexes count backward from the end of the list. So, L[-1] refers to the last item, L[-2] is the second-last, and so on. The negative indexes for the items in a nested list are illustrated as below:

**Python Nested List,** Learn to create a nested list in Python, access change and add nested list items, find nested list length, Negative indexes count backward from the end of the list . So, L[-1] refers to the last item, L[-2] is the second-last, and so on. When you want to insert an item at a specific position in a nested list, use insert() method. List Comprehensions are one of the most amazing features of Python. It is a smart and concise way of creating lists by iterating over an iterable object. Nested List Comprehensions are nothing but a list comprehension within another list comprehension which is quite similar to nested for loops.