Python 如何根据字典中的单个键创建多个值
Python How to create multiple values against a single key in dictionary
[Python, datastructres] 如何针对字典中的单个键创建多个值,如下所示
main_dict = { 'stats': IP_dict_name_1, 'Add': IP_dict_name_2}
IP_dict_name_1 = {uniq_ip_1 : [time, threshold_counter], unique_ip_2: [time, threshold_counter]}
IP_dict_name_1 = {uniq_ip_1 : [time, threshold_counter], unique_ip_2: [time, threshold_counter]}
实际上我想创建一个数据结构,我们可以在其中记录上述场景中的命令。例如,如果 'stats' 命令已经插入到字典中,那么它不会再次插入到字典中,如果不存在,则创建上面的数据结构。我很困惑如何以有效的方式做到这一点。简而言之,我想跟踪每个执行命令的 remote_ip 并设置命令的阈值,即 5 如果他想在一小时内执行 'stats' 命令超过 3 次,则不允许这样做。任何人都可以帮助这样做。任何粗略的想法表示赞赏?
您可以使用 if "stats" not in main_dict
检查密钥是否已存在于 main_dict
中,只需将“stats”替换为您要检查的密钥即可。这是一个简单的片段:
command = {
"uniq_ip_1" : ["time", "threshold_counter"],
"unique_ip_2": ["time", "threshold_counter"]}
main_dict = {}
if "stats" not in main_dict:
main_dict["stats"] = command
print(main_dict)
if "stats" not in main_dict:
main_dict["stats"] = command
print(main_dict)
您可以使用 (command, ip)
对作为日志字典的键。
此外,我推荐优秀的dictionary method dict.setdefault
。
exec_logs = {}
threshold_per_hour = 3
def exec_command(command_name, ip, current_hour):
previous_hour, n = exec_logs.setdefault((command_name, ip), (current_hour, 0))
if previous_hour == current_hour:
if n >= threshold_per_hour:
print('Command aborted: User {} cannot execute command {} more than {} times per hour.'.format(ip, command_name, threshold_per_hour))
return # do not execute command
else:
exec_logs[(command_name, ip)] = (current_hour, n+1)
print('Command {} successfully executed.'.format(command_name))
else:
exec_logs[(command_name, ip)] = (current_hour, 1)
print('Command {} successfully executed.'.format(command_name))
测试:
>>> exec_command('make coffee', 127, '1pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 127, '1pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 127, '1pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 127, '1pm')
Command aborted: User 127 cannot execute command make coffee more than 3 times per hour.
>>> exec_logs
{('make coffee', 127): ('1pm', 3)}
>>> exec_command('make coffee', 127, '2pm')
Command make coffee successfully executed.
>>> exec_logs
{('make coffee', 127): ('2pm', 1)}
>>> exec_command('make coffee', 139, '2pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 139, '2pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 139, '2pm')
Command make coffee successfully executed.
>>> exec_logs
{('make coffee', 127): ('2pm', 1), ('make coffee', 139): ('2pm', 3)}
[Python, datastructres] 如何针对字典中的单个键创建多个值,如下所示
main_dict = { 'stats': IP_dict_name_1, 'Add': IP_dict_name_2}
IP_dict_name_1 = {uniq_ip_1 : [time, threshold_counter], unique_ip_2: [time, threshold_counter]}
IP_dict_name_1 = {uniq_ip_1 : [time, threshold_counter], unique_ip_2: [time, threshold_counter]}
实际上我想创建一个数据结构,我们可以在其中记录上述场景中的命令。例如,如果 'stats' 命令已经插入到字典中,那么它不会再次插入到字典中,如果不存在,则创建上面的数据结构。我很困惑如何以有效的方式做到这一点。简而言之,我想跟踪每个执行命令的 remote_ip 并设置命令的阈值,即 5 如果他想在一小时内执行 'stats' 命令超过 3 次,则不允许这样做。任何人都可以帮助这样做。任何粗略的想法表示赞赏?
您可以使用 if "stats" not in main_dict
检查密钥是否已存在于 main_dict
中,只需将“stats”替换为您要检查的密钥即可。这是一个简单的片段:
command = {
"uniq_ip_1" : ["time", "threshold_counter"],
"unique_ip_2": ["time", "threshold_counter"]}
main_dict = {}
if "stats" not in main_dict:
main_dict["stats"] = command
print(main_dict)
if "stats" not in main_dict:
main_dict["stats"] = command
print(main_dict)
您可以使用 (command, ip)
对作为日志字典的键。
此外,我推荐优秀的dictionary method dict.setdefault
。
exec_logs = {}
threshold_per_hour = 3
def exec_command(command_name, ip, current_hour):
previous_hour, n = exec_logs.setdefault((command_name, ip), (current_hour, 0))
if previous_hour == current_hour:
if n >= threshold_per_hour:
print('Command aborted: User {} cannot execute command {} more than {} times per hour.'.format(ip, command_name, threshold_per_hour))
return # do not execute command
else:
exec_logs[(command_name, ip)] = (current_hour, n+1)
print('Command {} successfully executed.'.format(command_name))
else:
exec_logs[(command_name, ip)] = (current_hour, 1)
print('Command {} successfully executed.'.format(command_name))
测试:
>>> exec_command('make coffee', 127, '1pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 127, '1pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 127, '1pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 127, '1pm')
Command aborted: User 127 cannot execute command make coffee more than 3 times per hour.
>>> exec_logs
{('make coffee', 127): ('1pm', 3)}
>>> exec_command('make coffee', 127, '2pm')
Command make coffee successfully executed.
>>> exec_logs
{('make coffee', 127): ('2pm', 1)}
>>> exec_command('make coffee', 139, '2pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 139, '2pm')
Command make coffee successfully executed.
>>> exec_command('make coffee', 139, '2pm')
Command make coffee successfully executed.
>>> exec_logs
{('make coffee', 127): ('2pm', 1), ('make coffee', 139): ('2pm', 3)}