0

I am generating serials using random and a base. (see example below)

import random

charList = ["A", "B", "C", "D", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5"
"6", "7", "8", "9"]

def Z150(charList):
    return "1841GG001Z" + random.choice(charList) + "8" + " -> Z150 Speakerset"
for i in range(100):
    print(Z150(charList))

Now, there's a very likely chance that there will be duplicates, how would I find the duplicates and remove them?

I have seen ways to do this but nothing that checks the outputs of the prints and then deletes the duplicates (if that makes sense)

verbids
  • 55
  • 6
  • Add the results to a `set`. – meowgoesthedog Jun 10 '19 at 14:32
  • Do you need to maintain order...trying to pick the right dupe – pault Jun 10 '19 at 14:32
  • 1
    Possible duplicate of [Removing duplicates in Python lists](https://stackoverflow.com/questions/7961363/removing-duplicates-in-python-lists) or [How do you remove duplicates from a list whilst preserving order?](https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order) – pault Jun 10 '19 at 14:33

2 Answers2

1

One way using base python is to make a list of stuff you've already generated, and skip it:

printed = set()
for i in range(100):
    z150 = Z150(charList)
    if not z150 in printed:
        printed.add(z150)
        print(z150)

Or you can use a one-liner, and get a set in the first place, then print it afterwards:

snums = set(Z150(charList) for _ in range(100))
for s in snums: 
    print(s)
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
0

Use string module and shuffle

import random
import string

charList=list(string.ascii_uppercase+string.digits[1:])


def Z150(i):
    return  "1841GG001Z" + i + "8" + " -> Z150 Speakerset"
random.shuffle(charList)        
for i in charList[:20]:
    print(Z150(i))
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59