【问题标题】:Python - List of dictionaries - Typeerror: 'int' object is not subscriptablePython - 字典列表 - 类型错误:'int' 对象不可下标
【发布时间】:2018-02-27 17:26:19
【问题描述】:

下一个代码创建一个字典列表。字典的键只是整数,由 random 函数生成,对应的值由 ord 函数生成。当我生成小列表(大约 10、20 或 30)时,它工作得非常好,但是当我创建大列表(大约 100 个元素)时,我总是遇到同样的错误,这是我需要做的。 在添加 如果对 [0] == 键: TypeError: 'int' 对象不可下标

代码是这样的:

import random
import math
def makeArray(Size):
    Arr = [None] * Size
    return(Arr)

def getNumericKey(key):
    hash = 0
    for char in str(key):
        hash += ord(char)
    return(hash)

def H(keyN,m):
    return(keyN % m)

def search(key, Size, table): 
    key_hash=H(getNumericKey(key),Size)
    if table[key_hash] is not None:
        for pair in table[key_hash]:
            if pair[0]==key:
                return(pair[1])
            else:
                for j in range(Size):
                    keyh=(key_hash+j)%Size
                    if keyh==(Size-1):
                        break
                    for pair1 in table[keyh]:
                        if pair1[0]==key:
                            return(pair1[1])
    return(None)

def add(key, value, table, Size):
    key_hash = H(getNumericKey(key), Size)
    pairkeyvalue = [key, value]
    if table[key_hash] is None:
        table[key_hash] = list([pairkeyvalue])
        return(True)
    else:
        for pair in table[key_hash]:
            if pair[0] == key:
                pair[1] = value
                return(True)
        for j in range(Size):
            keyh = (key_hash + j) % Size
            if keyh == (Size - 1):
                print('table llena', key_hash)
                break
            else:
                if table[keyh] is None:
                    table[keyh] = list(pairkeyvalue)
                    return(True)

a=int(input("Size of table: "))
table = makeArray(a)
lst = [0] * math.floor(a/2)
for i in range(math.floor(a/2)):
    lst [i] = random.randint(1,100)
print(lst)
for n in lst:
    add(n, getNumericKey(n), table, len(table))
print(table)

【问题讨论】:

  • 请发布回溯
  • 把int转成字符串
  • lst = [0] * math.floor(a/2)--这行代码也是错的,不能用float乘list
  • 其实 floor 函数从运算中取最接近的整数。如果 a = 100 楼层需要 50 如果 a = 5 楼层需要 2

标签: python list dictionary typeerror


【解决方案1】:

add 中,您想将table[keyh] = list(pairkeyvalue) 行更改为table[keyh] = list([pairkeyvalue])table[keyh] = [pairkeyvalue]pairkeyvalue 已经是列表,所以 list(pairkeyvalue) 什么都不做,但您希望 table[keyh] 成为包含列表的列表。

def add(key, value, table, size):
    key_hash = H(getNumericKey(key), size)
    pairkeyvalue = [key, value]
    if table[key_hash] is None:
        table[key_hash] = [pairkeyvalue]
        return True
    else:
        for pair in table[key_hash]:
            if pair[0] == key:
                pair[1] = value
                return True
        for j in range(size):
            keyh = (key_hash + j) % size
            if keyh == (size - 1):
                print('table llena', key_hash)
                break
            else:
                if table[keyh] is None:
                    table[keyh] = [pairkeyvalue]
                    return True

【讨论】:

  • 非常感谢 Jeremy McGibbon 解决了问题
  • 没问题,很高兴为您提供帮助!
猜你喜欢
  • 2023-04-10
  • 2012-04-04
  • 2012-01-03
  • 2022-01-05
  • 2017-04-15
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多