简体   繁体   中英

Assign a unique 4 digit number to a list of ids using python

I have a list of ids that i want to give a 4 digit unique number for each of them .

For example :

driver id = ['5c73d1238472750067e86c6d','5a0d5513494c09001491c118']
unique id = ['1234' , '5897']

Is there a package i can use for this?

You can use the zfill function of the string object like this

ids = [1, 2, 3, 4]
list(map(lambda x: str(x).zfill(4), ids))

And will result in:

['0001', '0002', '0003', '0004']
import random
import string

def get_id():
    return ''.join(random.choice(string.digits) for n in range(4))
driver_id = [1, 2, 3, 4]
unique_id=[]
while len(unique_id)!=len(driver_id):
    id = get_id()
    if id not in unique_id:
        unique_id.append(id)
print(unique_id)

Output:

['7518', '7806', '4654', '0677']

Most of the answers are on how to generate them, assuming 4 digit number are used as string . Let us examine the problem and come up with design, constraints and manage them.

If you are using numbers as numbers, not strings, then non zero numbers starting with non zero(s) are, 1000 to 9999, you can only address 8999 elements (drivers in your case). - Constraint

Example: If you consider 0123 as a number is actually 3 digit number, 123 .

Design:

Generate the numbers in a for loop using range(1000,10000) and keep them in a list(code below): Complexity : is constant O(c), happens only once.

>>>
>>> four_digit_pool = []
>>> for i in range(1000, 10000):
...     four_digit_pool.append(i)
...
>>>
four_digit_pool = [1000, 1001, ..., 9999]

when you encounter a an element, look up if key exists and add it to driver_id_map.

Add it to map and pop element from list (pool of ids). Do not use remove, use pop, pop is ALWAYS O(1). Note: In this case remove also is O(1), because it is sorted and you are removing from left one by one. But use pop, for surety.

driver_id_map = {
1000 : '5c73d1238472750067e86c6d'
1001 : '5a0d5513494c09001491c118'
...
}

You can also have above map reversed, if you want to look-up if driver is already in list.

driver_id_map = {
'5c73d1238472750067e86c6d' : 1000
'5a0d5513494c09001491c118' : 1001
...
}

Look-up is O(1) for maps.

If you loop through all drivers and assign it, Complexity is O(N)

Note: If you are using numbers as strings and considering non zero numbers which are starting with zero(s) are 1108 numbers. You get additional ~1000 more drivers can be addressed.

These numbers also get into the four_digit_pool. Complexity won't change much.

numbers starting with 0 - 999 possibilities, example: 0123

numbers starting with 00 - 99 possibilities, example: 0023

numbers starting with 000 - 9 possibilities, example: 0003

numbers starting with 0000 - 1 possibilities, example: 0000 (the only one)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM