2

Hi I started learning python a week ago. I am trying to create a dictionary to replace characters. eg(dictionary["a"] = "%").

I have first created a list containng all characters order wise from ASCII 033 to 126. Then I tried to create a list of random characters in order to match the characters from the 1st list. But it is on an infinite loop. Does it take very long or is it my code?

Here is the code:

def replacement():
    s=0
    while s in range(93):
        rep.append(chr(randint(33,126)))
        if(letters[s] != rep[s]):
            k=0
            for replace in rep:
                if replace == rep[s]:
                    k+=1
            if(k<2):    
                s+=1
    print(rep)

letters = []
rep = []
i=33
while i <= 126:
    letters.append(chr(i))
    i+=1
replacement()
7
  • 1
    @ChihebNexus I don't think the stuff from letters = [] onwards is supposed to be inside the function. Commented Jun 2, 2017 at 15:00
  • 2
    @Sid, your code will be in an infinite loop because s will be always between range(93). Better using a for loop. Commented Jun 2, 2017 at 15:04
  • 3
    @WillDaSilva The idea is to fill rep with all the chars from chr(33) to chr(126) in a random order. Sid's algorithm currently has some flaws, but even when those flaws are fixed this approach is very slow. Commented Jun 2, 2017 at 15:05
  • 1
    A better approach would be to use the builtin random.shuffle() Commented Jun 2, 2017 at 15:07
  • 2
    It's cool that you thought of this way of creating a randomized list, but do you understand why it's slow? As rep gets longer, it takes longer and longer to scan through rep to check if the new char is already in it, and the odds of randomly generating a char that isn't in rep get lower and lower. Trying to randomly select the last couple of chars takes a very long time. Commented Jun 2, 2017 at 15:16

2 Answers 2

3

If you want to populate a list with chr(33) to chr(126) in a random order, a much better approach would be to use random.shuffle()

from random import shuffle
letters = [chr(x) for x in range(33, 127)]
shuffle(letters)

These three lines could replace everything you posted in your question. It's a much faster and easier to understand way of accomplishing the task as I understand it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer @WillDaSilva
2

while s in range(93): doesn't do what you think it does. It checks if s is in range 0..92 and loops as long as this condition is true; as s never changes its value from the initial 0, this condition never becomes false.

To iterate over a range 0..92 you should use:

for s in range(93):

Your other while loop also should be rewritten as:

for i in range(33,127):
    letters.append(chr(i))

or even better, replaced with a list comprehension:

letters = [chr(i) for i in range(33,127)]

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.