0

I have a Python 3.x script which only has the following function:

from random import random

def slicer(longitude):
    alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = ""
    x = 0

    for i in range(1, int(longitude)):
        x = int(62 * random())
        if x == 0:
            x += 1
        else:
            # get a random letter from alphabet, seem this is the problem > (alphabet[x:1]):
            string_section = alphabet[x:1]
            result = result + string_section
    return result

print(str(slicer(10)))  # get a 10 digit string

But this code doesn't work, and I'm stuck...

Is s[x:1] permitted, where x is a changing value from a for loop?

If not, is there another method that will accomplish the same result? Thanks for help.

5
  • remember that, in python slices, mylist[a:b] should have b > a if both a and b are non-negative. negative indexes x are mapped to (len(mylist)+x), so they count from the end. Commented Feb 18, 2014 at 15:50
  • The output should be a random string with n digits using the alphabet provided in the function. I know this could be realized with another method but I want use this method in particular. Curiosly, the script doesn't show any error, just a null result. Commented Feb 18, 2014 at 15:52
  • OH! so the problem, in effect was alphabet[x:1], this is solved changing it to: alphabet[x:x+1]...Thanks! I'm beginning with Python Commented Feb 18, 2014 at 15:57
  • null (None) or empty ("")? Commented Feb 18, 2014 at 15:57
  • Technically it is solved by using a length-1 slice (alphabet[x:x+1]) but this is a little misleading. You aren't trying to get a slice that happens to be length 1, you are trying to get a single element. It's probably better to code it that way, so that anyone reading the code sees from your choice that you meant just to get one item. Since alphabet[x] works, and it doesn't have the pitfalls of requesting a slice that falls outside of the range of indices of the iterable object, it's simpler and easier to understand in this case. Commented Feb 18, 2014 at 16:00

3 Answers 3

2

You may access a single character from the alphabet at random position x simply with alphabet[x]. Note that x must be in the range [0, len(alphabet)-1] to be a valid index, so you might want to use math.floor instead of int when turning the random value into an integer to serve as an index.

Alternatively, you can use random.choice for longitude number of times:

result = ''
for i in xrange(longitude):
    result += random.choice(alphabet)
return result
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that x is almost always less than 1, so [x:1] is a meaningless range. If I just do:

alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
alphabet[32:1]

I can see that python returns nothing. So your script is continually adding nothing to result -- what you need is alphabet[x] to take that position from the alphabet. And since alphabet[0] is a, you don't need to add 1 if x is 0. (In your implementation, you'll never return an "a" and "b" is about twice as likely to come up than any other letter).

from random import random

def slicer(longitude):
    alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = ""
    x = 0

    for i in range(1, int(longitude)):
        x = int(62 * random())
        string_section = alphabet[x]
        result = result + string_section
    return result

print(str(slicer(10)))  # get a 10 digit string

But ....

If you want to make sure you don't repeat letters, you can just use random.sample():

import random

def slicer(longitude):
    alphabet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = random.sample(alphabet,longitude)
    return result

print(''.join(slicer(10)))  # Produces a list, use .join() to print a string

Comments

0

If you're trying to get a single character you don't need slice, a simple indexing would do alphabet[x].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.