【问题标题】:Single value list index into integer in Python?Python中的单值列表索引到整数?
【发布时间】:2018-10-09 13:03:42
【问题描述】:

枚举时是否可以将单个值列表(例如[5])索引(例如4)转换为整数?我正在尝试制作一个程序,该程序使用给定的单词和数字创建一个随机用户名,如果之前已经使用过一个单词,我想删除它:

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)

【问题讨论】:

  • 你到底在问什么?如果您有一个单值列表lst=[5],并使用lst[4] 对其进行索引,那将是IndexError,因为没有那么多值。
  • 或者你只是问如何把一个字符串变成一个int,所以在for number in numbers:,你可以用45等来代替'4'、@ 987654329@等?如果是这样,您只需在循环内的代码中使用int(number) 而不是number
  • FWIW,从列表中间删除东西效率不高,因此当您有选择时,通常最好使用不同的逻辑。另外,如果您想创建多个用户名会怎样?我推荐 abarnert 的方法:使用sample。我可能会使用randrange 生成一个 2 位数字,然后将其转换为字符串,而不是从数字字符串列表中选择。

标签: python list indexing integer


【解决方案1】:

查看您的代码……我不确定它应该做什么,但我可以做出一些猜测。

首先你选择一个随机的词。然后您查找与该单词匹配的所有单词的索引。然后,您想将该索引列表与pop 一起使用。

好吧,你可以解决这个问题:

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

reversed 很重要,所以你先弹出最右边的。)

但没有必要这样做,因为words 中应该只有一个word_1 的副本,因此selected_word 中只有一个索引。所以你可以这样做:

words.pop(selected_word[0])

但在这种情况下,理解是不必要的。获取所有匹配项并获取第一个匹配项与获取第一个匹配项执行相同的操作,并且列表已经有一个方法:index

words.pop(words.index(word_1))

但实际上,您可以直接获取索引,而不是选择一个单词然后查找它来获取索引:

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

或者,最简单的是,将整个内容替换为:

word_1, word_2 = random.sample(words, 2)

【讨论】:

    【解决方案2】:

    你可能想要这个(不删除任何单词)?:

    >>> 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)
    

    使用del 选择会更简单,但需要words 的副本(如果您想要原始列表,words 不变):

    >>> 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
    

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2010-12-27
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      相关资源
      最近更新 更多