1

I can't believe how long I've been trying to figure out this really basic problem, and I can't seem to find the right answer here searching through the forum, so I'll just throw it out, as I'm sure this can be solved in a simple line of code:

I'm trying to generate some a generate a new column on my data frame with random unique identifiers of the form q + some 5 digit number. So for example, one such ID would be q12345.

My approach so far, as been to: (1) generate an array of numbers, and (2) a list of the same length of the q character, and then (3) try to combine them into a single array, which I would (4) add to my data frame. I've managed to do steps one and two quite easily, but I can't seem to get step 3 to work.

I'd be grateful for any tips on both (a) how to do step 3, as well as (b) an easier way to do this, which I'm sure I'm overlooking.

I tried the various approaches in Concatenate string and int in python 3.4 and could get it to work for individual elements, but not for the entire array.

So, far, I have:

sample_length = 10 #for example

values = np.random.randint(low=10000, high= 15000, size = sample_length)

q = ['q'] * sample_length
anguyen1210
  • 513
  • 5
  • 21
  • 1
    Possible duplicate of [Concatenate string and int in python 3.4](https://stackoverflow.com/questions/26090198/concatenate-string-and-int-in-python-3-4) – GPhilo Oct 18 '19 at 08:21

3 Answers3

6

You can combine them into a single array using list-comprehension, like so:

sample_length = 10
values = np.random.randint(low=10000, high= 15000, size = sample_length)
# Do this 
column = ['q' + str(i) for i in values ]

In case the code above is too much to unpack for you, see this:

sample_length = 10
values = np.random.randint(low=10000, high= 15000, size = sample_length)
columns = []
for i in values:
     columns.append('q' + str(i))

Both the code snippets do the same thing.
Hope this helps...!

insomniac_tk
  • 174
  • 1
  • 12
-1
import random as rand
print("q"+str(rand.randint(10000,100000)))
  • This answer is rather incomplete; if you intend to provide helpful answers you should take the time to work out applicable solutions, and explain them. – Murphy Oct 18 '19 at 14:22
-1

A nearly one liner would be:

sample_length = 10
coloumns = ["q" + "".join(uniqueNumbers) for uniqueNumbers in list(map(lambda generatedNumbers: [str(number) for number in generatedNumbers], [random.randint(0, 9, 5) for _ in range(sample_length)]))]

for a better readability:

sample_length = 10
coloumns = [
    "q" + "".join(uniqueNumbers) for uniqueNumbers in list(
        map(
            lambda generatedNumbers: [
                str(number) for number in generatedNumbers
            ],
            [
                random.randint(0, 9, 5) for _ in range(sample_length)
            ]
        )
    )
]

And in this case the id could start with a 0.

A better approach would be as follows because i guess that every number should be unique:

sample_length = 10
def getNumbers():
    return "".join(list(map(lambda number: str(number), random.randint(0, 9, 5))))
numbers = []
while len(numbers) != sample_length:
    newNumbers = getNumbers()
    if newNumbers not in numbers:
        numbers.append(newNumbers)
coloumns = ["q" + "".join(uniqueNumbers) for uniqueNumbers in numbers]
Sofien
  • 478
  • 3
  • 12