如何为字典键使用范围? [英] How to use a range for dict keys?
问题描述
我有一个程序可以扫描Google的链接,它会验证您找到了多少个链接,然后尝试为您的搜索找到成功的方法:
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_queries(self):
""" The returned URLS will be run through a query regex to see if
they have a query parameter.
http://google.com <- False
http://example.com/php?id=2 <- True
"""
filename = settings.create_random_filename()
print("File being saved to: {}".format(filename))
with open("{}\\{}.txt".format(settings.DORK_SCAN_RESULTS_PATH, filename),
"a+") as results:
for url in self.connect_to_search_engine():
match = settings.QUERY_REGEX.match(url) # Match by regex for anything
# that has a ?=<PARAM> in it
if match:
results.write(url + "\n")
amount_of_urls = len(open(settings.DORK_SCAN_RESULTS_PATH + "\\" + filename
+ ".txt", 'r').readlines())
return("\nFound a total of {} usable links with query (GET) parameters, urls"
" have been saved to {}\\{}.txt. This Dork has a success rate of {}%".
format(amount_of_urls, settings.DORK_SCAN_RESULTS_PATH, filename,
self.success_rate[amount_of_urls]
if amount_of_urls in self.success_rate.keys() else
'Unknown success rate'))
我想做的是使用每个dict键的范围来创建成功率:
What I would like to do is create the success rate using a range per dict key:
success_rate = {
range(1, 10): 10, range(11, 20): 20, range(21, 30): 30,
range(31, 40): 40, range(41, 50): 50, range(51, 60): 60,
range(61, 70): 70, range(71, 80): 80, range(81, 90): 90,
range(91, 100): 100
}
但是显然这是行不通的,因为列表不是可哈希的对象:
However obviously this doesn't work because lists are not hashable objects:
Traceback (most recent call last):
File "tool.py", line 2, in <module>
from lib.core import BANNER
File "C:\Users\Documents\bin\python\pybelt\lib\core\__init__.py", line 1, in <module>
from dork_check import DorkScanner
File "C:\Users\Documents\bin\python\pybelt\lib\core\dork_check\__init__.py", line 1, in <module>
from dorks import DorkScanner
File "C:\Users\\Documents\bin\python\pybelt\lib\core\dork_check\dorks.py", line 6, in <module>
class DorkScanner(object):
File "C:\Users\Documents\bin\python\pybelt\lib\core\dork_check\dorks.py", line 11, in DorkScanner
range(1, 10): 10, range(11, 20): 20, range(21, 30): 30,
TypeError: unhashable type: 'list'
有没有办法为dict键使用范围,以免自己必须从1至100生成键?
Is there a way I could use a range for a dict key to save myself from having to make a key from 1 – 100?
推荐答案
range()
仅仅是一个返回整数列表的函数,它本身并不是对象的类型.而且您不想在这里使用列表,不,因为它们包含范围内的所有整数.
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.
您可以使用 xrange()
对象,它们是可散列的,并且仅存储开始和结束值.但是,除非您打算使用 other xrange()
对象测试这些键,否则带有此类键的字典不是很有用,您必须遍历字典以针对每个xrange
手动对象.
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.
您的成功率字典可以更简单地用数学代替;只需将您的数字四舍五入到最接近的10的倍数即可(只需使用下限除法即可):
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
请测试结果值是否在10到100之间:
Do test if that resulting value is between 10 and 100:
10 <= success_rate <= 100
这篇关于如何为字典键使用范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!