我正在做一个项目,我需要写一个函数来为我们公司的每个客户生成ID。已经有一个现有的客户列表,其中一些客户的数字ID为5到6位,范围为40000到200000。还有其他现有客户没有ID,我希望与现有的ID号保持一致(例如43606或125490)
因此,为了保持类似的格式,我创建了一个Exclusion_List
,其中包含所有现有的ID号。然后我将使用np.random.uniform(low=40000, high=200000)
编写一个函数,以便在该范围内生成一个看起来与其他ID号类似的数字
我的问题是,我不知道如何设置一个循环来检查随机生成的ID是否已经在排除列表中,如果已经在排除列表中;然后生成一个新的
这就是我到目前为止所做的:
exclusions = [43606,125490,...]
def ID_Generator(new_clients): # This is a list of new client
new_client_IDs = []
for client in new_clients:
ID = int(np.random.uniform(low=40000, high=200000))
while ID not in exclusions:
new_client_IDs.append(ID)
当随机生成的数字在排除列表中时,我不确定如何处理这种情况。我希望该函数能够输出一个数据帧,在一列中包含客户机名称,在第二列中包含ID号
谢谢你在这方面的帮助
我现在能想到的简单方法是
40000-200000
生成一个列表李>exclusions
李>import random exclusions = [43606,125490] all = range(40000,200000) remaining = [x for x in all if x not in exclusions] random.choice(remaining)
与Niranjan的答案类似,但不需要列表理解
给
相关问题 更多 >
编程相关推荐