【问题标题】:list index out of range-python列表索引超出范围-python
【发布时间】:2014-09-24 18:42:19
【问题描述】:

我正在尝试制作一个可以在其中输入特定单词的刽子手游戏。

我在尝试编写一个计算单词中相同字母数量的代码时遇到了一个问题。例如,'taste' 将有两个 ts。

该程序将计算将用于数学和的字母,因此该字母可以输入两次,但不能再输入。

我在下面的代码中遇到了列表索引超出范围的问题。

def nolettercheck():

    global list45 #list of variable within code (so they can be accessed)
    global list1
    global countonce
    global count0
    global count1
    global count2
    global count3
    global count4
    global count5
    global count6
    global count7
    global count8
    global count9
    if len(list1) == 1: #makes sure list is in range
        if list1.count(list1[0]) > 1: """count how many letter in word and if above 1 it continues
                                         (although it doesn't work perfectly yet BTW do not spoil 
                                          this answer for me unless
                                         you have to)."""
            count0 = list1.count(list1[0])#create variable for number of same letters
            list45.insert(0,list1[0])#insert the letter into another list for comparison
    if len(list1) == 2 or len(list1) < 2: """repeats with next letter until
                                             similar statement =false"""
        if list1.count(list1[1]) > 1:
            count1 = list1.count(list1[1])
            list45.insert(1,list1[1])
    if len(list1) == 3 or len(list1) < 3:
        if list1.count(list1[2]) > 1:
            count2 = list1.count(list1[2])
            list45.insert(2,list1[2])
    if len(list1) == 4 or len(list1) < 4:
        if list1.count(list1[3]) > 1:
            count3 = list1.count(list1[3])
            list45.insert(3,list1[3])
    if len(list1) == 5 or len(list1) < 5:
        if list1.count(list1[4]) > 1:
            count4 = list1.count(list1[4])
            list45.insert(4,list1[4])
    if len(list1) == 6 or len(list1) < 6:
        if list1.count(list1[5]) > 1:
            count5 = list1.count(list1[5])
            list45.insert(5,list1[5])
    if len(list1) == 7 or len(list1) < 7:
        if list1.count(list1[6]) > 1:
            count6 = list1.count(list1[6])
            list45.insert(6,list1[6])

代码继续,所以它最多可以写十个字母的单词。这段代码假设将字母的编号存储到一个变量中(稍后将转换为一个列表,尽管我可能会进一步更改)。它还应该存储字母(如果有多个字母)。

该错误特别关注与if list1.count(list[6]) &gt; 1: 相似的行。

我之前确实遇到过这个错误并且if len(list1) == 7 or len(list1) &lt;7: 工作(因为它停止检查列表中的虚值)。所以我对此感到很困惑。

【问题讨论】:

  • 哎呀,这很痛苦。您真的应该考虑制作一个包含 10 个计数而不是 10 个单独变量的列表,循环 range(1, 8) 而不是复制和粘贴相同的代码 7 次,并且很可能使用比您必须保留的列表更合适的数据结构打电话给count 和insert…
  • 另外,len(list1) == 7 or len(list1) &lt; 7 可以写成len(list1) &lt;= 7。
  • 您能否详细说明一下您想要实现的目标?据我了解,您只需要一个单词的字母计数(但将其限制为 2?)。我相信会有一种更简单的方法来做到这一点,因为你在这里看到的效率非常低。
  • 无论如何,您能否发布一个完整但精简的程序(和输入)来演示问题,并发布异常和回溯而不是试图描述它?请参阅帮助中的MCVE 以获得一些指导。
  • 我确实尝试过

标签: python list indexing window


【解决方案1】:

这是一种从单词中获取字母计数字典的方法。有更多的“pythonic”方法可以做到这一点,但如果你刚刚开始,这可能会对你有所帮助。

a = 'somestring'
letter_count_dict = {}
for letter in set(a):
    letter_count_dict[letter] = a.count(letter)

单词中不同的字母是

set(a) #set takes the distinct values.  If you want a list of distinct values, an easy way to do it is list(set(a)) but since sets are iterable you don't usually need to do this.

给定一个字母l,它出现的次数是

letter_count_dict[l]  #if l='s', then this would output 2

一旦你有了计数,你就可以选择如何处理它们……循环并找到出现次数超过一次的那些,等等……祝你好运。

【讨论】:

  • 谢谢!我希望避免为每个字母制作完整的字典,但它可能比我现在的效率略高。
猜你喜欢
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多