简体   繁体   English

我的python函数不会返回值

[英]My python function will not return value

I have a user-defined function that takes table-size (m) as input. 我有一个用户定义的函数,该函数将表大小(m)作为输入。 I implement an empty list (of pairs [key,count], essentially an array with two fields) of that size and use linear hashing to insert generated random numbers into the list. 我实现了一个该大小的空列表(成对的[key,count],本质上是一个具有两个字段的数组),并使用线性哈希将生成的随机数插入列表中。 So say the first random number (k) is 20 and the length of the list (m) is 9, the hash function (h0 = k%m) will return 2 (as that is the remainder when 20 is divided by 9). 因此,假设第一个随机数(k)为20,列表的长度(m)为9,则哈希函数(h0 = k%m)将返回2(因为20除以9就是余数)。 If that cell is empty, I want to insert the key value(k) into the first field and 1 (count of probes) into the second field. 如果该单元格为空,我想将键值(k)插入第一个字段,将1(探测数)插入第二个字段。 If that cell is not empty, I want to increment the index using the hash function (h0 = (h0 + i)%m) until an empty cell is found and insert there. 如果该单元格不为空,则我想使用哈希函数(h0 =(h0 + i)%m)递增索引,直到找到一个空单元格并将其插入。 I want to return the resulting list. 我想返回结果列表。 I have a return statement in my function and the documentation I've read says assigning a new value when I call the function and using a print statement should work. 我的函数中有一个return语句,并且我阅读的文档说在调用函数时分配一个新值并使用print语句应该可以。 I'm using PyCharm community edition. 我正在使用PyCharm社区版。 Could my IDE be the problem? 我的IDE可能是问题吗? (Below is my code) (下面是我的代码)

import random

def linear(m):

hTable = [[None, None] for j in range (m)]

for n in range (1100):
    k = random.randint(1,10000)

    for i in range(len(hTable)):
        for j in range(len(hTable[i])):
            h0 = k % m

            if hTable[h0] == [None,None]:
                hTable[h0][0] = k
                hTable[h0][1] = 1

            else:
                while hTable[h0] != [None, None]:
                    hTable[h0][1] += 1
                    h0 = (h0 + i)%m

                else:
                    hTable[h0][0] = k
                    hTable[h0][1] = 1

                    return hTable

                hash = linear(1223)
                print(hash)
>>> [None,None]*2
[None, None, None, None]

In Python, multiplying a list extends the list, it doesn't make it multidimensional. 在Python中,将列表相乘会扩展列表,但不会使其具有多维性。 You probably wanted something like [[None, None] for i in range(m) . 您可能想要[[None, None] for i in range(m)类似[[None, None] for i in range(m)东西。

(I'm also not sure what your code is doing in general, as on the first iteration everything will == [None, None] and cause your else block to be executed - which has a return in it, and thus the returned array will consist of hTable[h0] = [k, 1] and every other element [None, None] ...) (我也不确定您的代码总体上在做什么,因为在第一次迭代中,所有内容将== [None, None]并导致您的else块被执行-其中包含返回值,因此返回数组将由hTable[h0] = [k, 1]和每个其他元素[None, None]

Also, h0 will have the same value through every iteration (since it's calculated from values which aren't change)... and you probably want to return hTable , not [hTable] , since it's already a list. 另外, h0在每次迭代中都具有相同的值(因为它是根据不变的值计算得出的)...并且您可能想return hTable而不是[hTable] ,因为它已经是一个列表了。

The problem was the indentation of my return statement. 问题是我的退货单缩进了。 The following works: 以下作品:

import random

def linear(m):

    hTable = [[None, 0] for j in range (m)]

    nums = [random.randint(1,10001) for n in range(1100)]

    for i in range(len(nums)):
        h0 = nums[i] % m

        while hTable[h0] != [None, 0]:
            hTable[h0][1] += 1
            h0 = (h0 + 1)%m

        else:
            hTable[h0][0] = nums[i]
            hTable[h0][1] = 1

    count = sum(i[1] for i in hTable)
    print (count)

    return hTable

l = linear(1223)
print(l)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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