请考虑下面的Python代码:
def allot():
dict2 = {'1': 1, '2': 1, '3': 0, '4': , '5': 1}
allotted_id = None
for k in dict2:
usr_id = 3
if (str(usr_id) != k):
continue;
if ((str(usr_id) == k) and (dict2[str(usr_id)] == 1)):
print("\n user Requested Id : ", usr_id)
print("\n user Requested Id is available ! ")
allotted_id = k
break;
else:
print("\n user Requested Id : ", usr_id)
print("\n user Requested Id is not available ! ")
usr_id = usr_id + 1
if (dict2[str(usr_id)] == 1):
allotted_id = usr_id
break;
print('So allotted dict2 Id', allotted_id)
allot()
在的“dict2”,if values == 1 then it is active or if values == 0
中,它处于非活动状态。在这里,将usr_id
与dict2
中的键id匹配
案例1:dict2 = {'1': 1, '2': 1, '3': 1, '4': 1, '5': 1}
。现在usr_id==3,dict2键'3'==1。如此分配id=3
案例2:dict2 = {'1': 1, '2': 1, '3': 0, '4': 1, '5': 1}
。现在usr_id==3
和dict2键“3”==0。然后分配下一个活动id。所以分配的id=4
案例3:dict2 = {'1': 1, '2': 1, '3': 0, '4': 0, '5': 1}
。现在usr_id==3
和dict2键'3'&;'4'==0 . 因此,距离usr_id
要分配的下一个最近的活动id(只不过是键id“2”)。如何操作
为我的案例3场景提供指导。提前谢谢
我会使用numpy:
我使用的最小用户id为0(只是一个cs的东西…),但您可以很容易地将其更改为1
假设您不希望对代码进行效率更改,那么您将覆盖
for k in dict2
循环中的usr\u id您可以编写此代码来执行更多检查,并更多地使用字典结构
我建议采用不同的方法(不知道你的方法有多灵活)——
像这样存储数据
现在,对于任何传入的用户id
相关问题 更多 >
编程相关推荐