简体   繁体   中英

Single value list index into integer in Python?

Is it possible to turn a single value list (eg [5]) index (eg 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)

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM