How to use a range for dict keys?

后端 未结 2 1424
野趣味
野趣味 2021-01-23 17:19

I have a program that scans Google for links, it verifies how many links you\'ve found and then tries to find a success right for your search:

def check_urls_for         


        
2条回答
  •  醉话见心
    2021-01-23 17:42

    range() in Python 2 is merely a function that returns a list of integers, it is not itself a type of object. And you wouldn't want to use lists here, no, because they contain all integers in the range.

    You could use xrange() objects instead, these are hashable, and only store the start and end values. However, unless you plan to use other xrange() objects to test these keys, a dictionary with such keys is not very useful, you'd have to loop over the dictionary to test your rate against each xrange object manually.

    Your success rate dictionary could more simply be replaced by maths; just round your numbers up to the nearest multiple of 10 (simply using floor division):

    success_rate = ((amount_of_urls // 10) + 1) * 10
    

    Do test if that resulting value is between 10 and 100:

    10 <= success_rate <= 100
    

提交回复
热议问题