Python. How to iterate through a list of lists looking for a partial match

Posted by Becca Millard on Stack Overflow See other posts from Stack Overflow or by Becca Millard
Published on 2012-11-06T22:41:54Z Indexed on 2012/11/06 23:00 UTC
Read the original article Hit count: 304

Filed under:
|
|

I'm completely stuck on this, without even an idea about how to wrap my head around the logic of this.

In the first half of the code, I have successfully generation a list of (thousands of) lists of players names and efficiency scores: eg name_order_list = [["Bob", "Farley", 12.345], ["Jack", "Donalds", 14.567], ["Jack", "Donalds", 13.421], ["Jack", "Donalds", 15.232],["Mike", "Patricks", 10.543]]

What I'm trying to do, is come up with a way to make a list of lists of the average efficiency of each player. So in that example, Jack Donalds appears multiple times, so I'd want to recognize his name somehow and average out the efficiency scores. Then sort that new list by efficiency, rather than name.

So then the outcome would be like: average_eff_list = [[12.345, "Bob", "Farley"], [14.407, "Jack", "Donalds"], [10.543, "Mike", "Patricks"]]

Here's what I tried (it's kind of a mess, but should be readable):

total_list = []
odd_lines = [name_order_list[i] for i in range(len(name_order_list)) if i % 2 == 0]
even_lines = [name_order_list[i] for i in range(len(name_order_list)) if i % 2 == 1]

i = 0
j = i-1
while i <= 10650:
    iteration = 2
    total_eff = 0
    while odd_lines[i][0:2] == even_lines[i][0:2]:
        if odd_lines[i][0:2] == even_lines[j][0:2]:
            if odd_lines[j][0:2] != even_lines[j][0:2]:
                total_eff = even_lines[j][2]/(iteration-1)
                iteration -= 1 #account fr the single (rather than dual) additional entry
            else:
                total_eff = total_eff
        if iteration == 2:
            total_eff = (odd_lines[i][2] + even_lines[i][2]) / iteration
        else:
            total_eff = ((total_eff * (iteration - 2)) + (odd_lines[i][2] + even_lines[i][2])) / iteration
        iteration += 2
        i += 1
        j += 1
        if i > 10650:
            break
    else:
        if odd_lines[i][0:2] == even_lines[j][0:2]:
            if odd_lines[j][0:2] != even_lines[j][0:2]:
                total_eff = (odd_lines[i][2] + even_lines[j][2]) / iteration
            else:
                total_eff = ((total_eff * (iteration -2)) + odd_lines[i][2]) / (iteration - 1)
        if total_eff == 0: #there's no match at all
            total_odd = [odd_lines[i][2], odd_lines[i][0], odd_lines[i][1]]
            total_list.append(total_odd)
            if even_lines[i][0:2] != odd_lines[i+1][0:2]:
                total_even = [even_lines[i][2], even_lines[i][0], even_lines[i][1]]
        else:
            total = [total_eff, odd_lines[i][0], odd_lines[i][1]]
            total_list.append(total)

        i += 1
        if i > 10650:
            break
else:
    print(total_list)

Now, this runs well enough (doesn't get stuck or print someone's name multiple times) but the efficiency values are off by a large amount, so I know that scores are getting missed somewhere.

This is a problem with my logic, I think, so any help would be greatly appreciated. As would any advice about how to loop through that massive list in a smarter way, since I'm sure there is one...

EIDT: for this exercise, I need to keep it all in a list format. I can make new lists, but no using dictionaries, classes, etc.

© Stack Overflow or respective owner

Related posts about python

Related posts about while-loop