Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
1  
You'll never add anything to RanksList since it's initialized to [] at the beginning. But your if condition suppose your RanksList is not empty –  Sam Bruns 3 hours ago
    
duplicate_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 ago
    
@SamBruns: I just assumed that I needed to initialize it to zero so that elements can be added later on. Even on removing it, I am unable to specifically check the 3rd element in every nested list with others (the ones I have called using i. –  quarters 3 hours ago
    
@das-g : Thanks for your response; being a newbie I wasn't aware of that. Is there another way I can compare the elements of the nested lists with its own? Thanks. –  quarters 3 hours ago
    
@das-g: Got it. I sliced it. –  quarters 3 hours ago

1 Answer 1

up vote 1 down vote accepted

Python for doesn't need a variable for the indices. In the for the element of the current iteration will be available in the variable before the in. Thus, to access the inner lists's third element, you'll want something like

for m in A:
    for n in A:
        if (n[3] > m[3]): # no i
            # (increase score for m if n in RankList)

In the end RankList shall have an entry for each entry in A. But if we fill those into RankList from the beginning, your condition in the score calculation would be meaningless, so I guess that's not what you want. However, you'll have to fill in the values somewhen, and you cannot make that dependant on already present values, or it will never happen.

Maybe adding the value of the outer iteration once we've dealt with it is what you wanted:

RankList = []
for m in A:
    for n in A:
        if (n[3] > m[3]): # no i
            # (increase score for m if n in RankList)
    RankList.append(m)

Oops, syntax highlighting thinks RankList is a class, because it starts with a capital letter and continues mixed case. (The naming convention for python classes. Not enforced by the language, just a convention.) Let's rename it:

rank_list = []
for m in A:
    for n in A:
        if (n[3] > m[3]):
            # (increase score for m if n in rank_list)
    rank_list.append(m)

Now we can use it in the condition:

rank_list = []
for m in A:
    for n in A:
        if (n[3] > m[3]) and (n in rank_list):
            # (increase score for m)
    rank_list.append(m)

You wanted the lists in rank_list to have the score as the 5th element (i.e. at index 4).

rank_list = []
for m in A:
    score = 0
    for n in A:
        if (n[3] > m[3]) and (n in rank_list):
            score += 1
    rank_list.append(m + [score])

But wait, n in rank_list will now always be false, because rank_list will contain different lists. Let's work with two separate lists, thus:

rank_list = []
already_scored = []
for m in A:
    score = 0
    for n in A:
        if (n[3] > m[3]) and (n in already_scored):
            score += 1
    rank_list.append(m + [score])
    already_scored.append(m)
share|improve this answer
    
I really love the step by step way you explained. It understood it well, and it worked perfectly. Thanks! –  quarters 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.