0

I'm new to python know there is a better way to do this extremely simple task but I dont seem to be asking the right questions and I am hoping someone here can give me some advice on how to neaten up my code - I feel like I am writing way to much code to do what I am doing.

I'm tagging my recorded tv shows with tvdb_api. The api returns the banners in an interesting structure (dict nesting tuples nesting dicts) the basic structure is:

{"684x400":
    {"949520":
        {"_bannerpath": "http://tvdb.com/banners/somepath",
         "rating" : 9.000
         #etc...
        },
     "456520":
        {"_bannerpath": "http://tvdb.com/banners/somepath",
            "rating" : 9.000
            #and so on...
        }
    }
}

I loop over the items in the data structure, compare the rating values and get the _bannerpath of the highest rated banner. I do this by getting all of the rating and _bannerpath values from the structure and copying them into as nested lists into a list and then seperating out the rating values into another list and using max to get the highest value and returning the corresponding bannerpath!

There MUST be a better way than this!

posterList = []

for tup in bannerStructure:
            for key in tup[1]:
                    if key == "rating":
                            posterList.append([tup[1]['rating'],tup[1]['_bannerpath']])
        ratingList = []

        for rating in posterList:
            ratingList.append(rating[0])

        maxRating = max(ratingList)

        for lst in posterList:
            if lst[0] == maxRating:
                return lst[1]

thankyou for your help!

4
  • do you want to get _bannerpath with the highest rating for each nxm resolution? Commented Dec 1, 2013 at 8:26
  • You can [a] sort by the rating with sorted, or [b] keep a running max in your loop. Commented Dec 1, 2013 at 8:26
  • 5
    The sample data you've posted is not a valid python data structure. Commented Dec 1, 2013 at 8:27
  • @AshwiniChaudhary apologies J.F.Sebastian was correct updating question now Commented Dec 1, 2013 at 8:43

2 Answers 2

1

Assuming the actual structure is like this:

{"684x400":
    {"949520":
        {"_bannerpath": "http://tvdb.com/banners/somepath",
         "rating" : 9.000
         #etc...
        },
     "456520":
        {"_bannerpath": "http://tvdb.com/banners/somepath",
         "rating" : 9.000
         #and so on...
        }
    }
}

Then to find a banner with the highest rating for each resolution:

from operator import itemgetter

for resolution, banners in tvdb_api_result.items():
    bannerpath = max(banners.values(), key=itemgetter('rating'))['_bannerpath']
    print("%s %s" % (resolution, bannerpath))
Sign up to request clarification or add additional context in comments.

1 Comment

@leemo: make sure that rating is indeed a float in the input otherwise the comparison will be incorrect e.g., "2" > "10". You could use: key=lambda b: float(b['rating']) to parse it to get 2 < 10.
1

You can use comprehension and max function to get this

max([(k["_bannerpath"], k["rating"]) for v in data.values() for k in v.values()], key=lambda x:x[1])

1 Comment

elegant. +1 I will experiement with this method also... the Python docs dont work well with my brain unfortunately... are you about to briefly explain the function of lambda

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.