0

Is it possible to turn a single value list (e.g. [5]) index (e.g. 4) into an integer when enumerating it? I'm trying to make a program that creates a random username with given words and numbers, and I want to delete a word if it has already been used before:

import random

# data (words/numbers)
words = ['Cool', 'Boring', 'Tall', 'Short']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

# words
word_1 = random.choice(words)
selected_word = [i for i,x in enumerate(words) if x == word_1]
words.pop(selected_word)
word_2 = random.choice(words)

# numbers
number_1 = random.choice(numbers)
number_2 = random.choice(numbers)

# printing username
print(word_1+word_2+number_1+number_2)
3
  • 1
    What exactly are you asking here? If you have a single-value list lst=[5], and you index it with lst[4], that's going to be an IndexError, because there aren't that many values. Commented Apr 29, 2018 at 4:56
  • Or are you just asking how to turn a string into an int, so in for number in numbers:, you can do something with 4, 5, etc. instead of '4', '5', etc.? If so, you just use int(number) in the code inside the loop instead of number. Commented Apr 29, 2018 at 4:57
  • FWIW, deleting stuff from the middle of a list isn't very efficient, so it's generally better to use different logic when you have a choice. Also, what happens if you want to create more than one username? I recommend abarnert's approach: use sample. And I'd probably use randrange to generate a 2 digit number, and then convert that to a string, rather than choosing from a list of digit strings. Commented Apr 29, 2018 at 5:29

2 Answers 2

1

Looking at your code… I'm not sure what it's supposed to be doing, but I can make some guesses.

First you pick a random word. Then you look up the indices of all words that match that word. Then you want to use that list of indices with pop.

Well, you can fix that:

for idx in reversed(selected_word):
    words.pop(idx)

(The reversed is important so you pop the rightmost ones first.)

But there's no need for that, because there should only ever be one copy of word_1 in words, and therefore only one index in selected_word. So you can just do this:

words.pop(selected_word[0])

But in that case, the comprehension is unnecessary. Getting all matches and taking the first does the same thing as taking the first match, and lists already have a method for that: index.

words.pop(words.index(word_1))

But really, instead of picking a word and then looking it up to get the index, you could just get the index:

index = random.randrange(len(words))
word_1 = words.pop(index)

Or, most simply of all, just replace the whole thing with:

word_1, word_2 = random.sample(words, 2)
Sign up to request clarification or add additional context in comments.

Comments

0

May be you wanted this (without deleting any word)?:

>>> import random
>>> words = ['Cool', 'Boring', 'Tall', 'Short']
>>> numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> selected_word_indices = set()
>>> def select_word():
        if len(selected_word_indices) == len(words):
            raise Exception("No More Unique Word Left")
        while True:
            chosen_index = random.randint(0,len(words)-1)
            if chosen_index not in selected_word_indices:
                chosen = words[chosen_index]
                selected_word_indices.add(chosen_index)
                return chosen

>>> word_1 = select_word()
>>> word_2 = select_word()

>>> number_1 = random.choice(numbers)
>>> number_2 = random.choice(numbers)

>>> print(word_1+word_2+number_1+number_2)

With del selecting will be simpler but a copy of words will be required (if you want the original list, words unchanged):

>>> words_copy = words.copy()
>>> def select_word():
        if len(words_copy) == 0:
            raise Exception("No More Unique Word Left")
        chosen_index = random.randint(0,len(words_copy)-1)
        chosen_word = words_copy[chosen_index]
        del words_copy[chosen_index]
        return chosen_word

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.