Nested list A = [[1, 110, 150, 3], [5, 95, 155, 1], [4, 115, 195, 4], [2, 105, 205, 2]]
. I am trying to give a score to each of these individual lists which is calculated as score = number of individual nested lists that have ( higher 3rd element value (3, 1, 4, 2) AND a presence in RanksList)
; where Rankslist = []
initially.
So, basically an element needs to be added in each of the individual lists and RanksList
becomes RanksList = [[1, 110, 150, 3, x], [5, 95, 155, 1, x], [4, 115, 195, 4, x], [2, 105, 205, 2, x]]
; where x
is the calculated score of each of the individual lists. This is what I have so far, and I am stuck:
m = 0
n = 0
i = 0
#RanksList = []
A = [[1, 110, 150, 3], [5, 95, 155, 1], [4, 115, 195, 4], [2, 105, 205, 2]]
duplicate_list = A[:]
for m in A:
for n in duplicate_list:
if (n[i][3] < m[i][3]) and (n in RanksList):
RanksList.append(m)
i = i + 1
print(len(RanksList))
print(RanksList)
I have tried to duplicate the original list A
and compare the third element in each of the nested list with the original nested list individually to fill in RanksList
. I am a newbie to Python, and still shaky on lists. I am trying to do it without numpy. Thanks.
RanksList
since it's initialized to[]
at the beginning. But yourif
condition suppose yourRanksList
is not empty – Sam Bruns 3 hours agoduplicate_list = A
merely produces an alias: A new variable (i.e. just a new name in your program) referencing the same list, not a copy of it. If you alter it via one name, you'll see the altered list via the other name, too. – das-g 3 hours agoi
. – quarters 3 hours ago