2
$\begingroup$

I have a df which has a serial number generated with each new record. The serial number combines with some other part like state code, year of registration and category code. So it has a format like below:

| DOR    | Applicant's code |
|:-------|:--------------:|
|1-2-2018| MH2018-PAR-0689|
|1-2-2018| MH2018-PAR-0689|
|2-2-2018| MH2018-PAR-0690|
|2-2-2018| MH2018-OMC-0691|
|1-2-2018| UP2018-OMC-2461|
|1-2-2018| UP2018-FPR-2462|
|3-2-2018| UP2018-PAR-2463|
|1-2-2018| UP2018-OMC-2462|

Let's say 20 such records are generated in each state every month and there are 37 different state codes and 8 different category codes. I want to create a list of all possible Applicant's codes for next month which should be 37 x 1 x 8 x 20 possible values. I need guidance on how to code it with python and also if my approach is correct.

$\endgroup$
1
  • $\begingroup$ Have a look at the itertools library, more specifically the itertools.product function which allows you to create all possible combinations between the inputs. $\endgroup$
    – Oxbowerce
    Sep 20 at 18:05
2
$\begingroup$

Use itertools doc for this purpose. Without knowing your exact codes I just made some lists up:

import itertools as it

nums = [x for x in range(37)]
single = ["_"]
abc = list('abcdefgh')
codes = [f"123{x}" for x in range(20)]


len(abc) * len(nums) * len(codes) # 5920

list(it.product(abc, single, nums, codes)) # len(...) -> 5920

This gives you:

[...]
 ('a', '_', 0, '1232'),
 ('a', '_', 0, '1233'),
 ('a', '_', 0, '1234'),
 ('a', '_', 0, '1235'),
 ('a', '_', 0, '1236'),
[...]
 ('b', '_', 12, '12315'),
 ('b', '_', 12, '12316'),
 ('b', '_', 12, '12317'),
 ('b', '_', 12, '12318'),
 ('b', '_', 12, '12319'),
[...]
$\endgroup$
2
  • 1
    $\begingroup$ Thanks Albo, worked for me $\endgroup$
    – mis_swa
    Sep 21 at 15:47
  • $\begingroup$ If you find my answer useful, I'd appreciate it if you can accept it, thank you! $\endgroup$
    – Albo
    Sep 22 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.