进口麻烦

发布于 2024-12-07 06:14:28 字数 935 浏览 0 评论 0原文

这是我的模块的一部分: gm.py

def avg_list(list): 
    sum = 0
    for num in list:
        sum += num

    avg = float(sum)/len(list)
    print avg



def median(list):
    i = len(list)
    if not i%2: # if i divided my 2 has no remainder

        return (list[(i/2)-1]+list[i/2])/2.0 # return the value of this block


    else:
        median = sorted(list)[len(list)/2] # otherwise, when the list is sorted, the index of len(s) / 2 is the middle number.
        return median

当我将其保存为“gm.py”并打开一个新的脚本页面以输入以下函数时:

import gm
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = median(list) # same for median
    return stats

当我运行此程序并输入 stats([2,3,4,5, 6])...我收到一条错误消息,指出全局变量 avg_list 未定义。我不确定我的导入是否正确? 我需要做类似... from gm import avg_list() 的事情吗?

Here is part of my module: gm.py

def avg_list(list): 
    sum = 0
    for num in list:
        sum += num

    avg = float(sum)/len(list)
    print avg



def median(list):
    i = len(list)
    if not i%2: # if i divided my 2 has no remainder

        return (list[(i/2)-1]+list[i/2])/2.0 # return the value of this block


    else:
        median = sorted(list)[len(list)/2] # otherwise, when the list is sorted, the index of len(s) / 2 is the middle number.
        return median

When I save this as 'gm.py' and open a new script page to input the following function:

import gm
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = median(list) # same for median
    return stats

When I run this program and type stats([2,3,4,5,6])... I get an error saying global variable avg_list not defined. I'm not sure if I'm doing the import correctly?
Do I need to do something like... from gm import avg_list() ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-12-14 06:14:28

引用模块对象上的函数:

import gm
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = gm.avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = gm.median(list) # same for median
    return stats

或将函数直接导入全局命名空间:

from gm import avg_list, median
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = median(list) # same for median
    return stats

请注意,您不应命名变量list。这会屏蔽内置的 list() 函数/类型,如果您需要使用它,可能会在以后导致混乱的错误。

您可以

stats = {}  # empty dict named stats
stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
stats['median'] = median(list) # same for median
return stats

按照

stats = {'average': avg_list(list), 'median': median(list)}
return stats # or just return the dict literal, no real need to give it a name.

我认为您应该查看 median 函数的第一个分支的方式编写。列表是否也需要在那里排序,就像在第二个分支中一样?

您的 avg_list 函数还屏蔽了一个内置函数 sum(),您可以在此处使用该函数,而无需手动添加:

def avg_list(list): 
    avg = float(sum(list))/len(list)
    print avg

最后,查看该函数的最后一行-- 它正在打印avg,但stats期望它返回avg代码>.两者并不相同。

Either reference the functions on the module object:

import gm
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = gm.avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = gm.median(list) # same for median
    return stats

or import the functions directly into the global namespace:

from gm import avg_list, median
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = median(list) # same for median
    return stats

Note that you should not name a variable list. This masks the built-in list() function / type and can cause confusing errors later if you need to use it.

You can write

stats = {}  # empty dict named stats
stats['average'] = avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
stats['median'] = median(list) # same for median
return stats

as

stats = {'average': avg_list(list), 'median': median(list)}
return stats # or just return the dict literal, no real need to give it a name.

I think you should look at the first branch of your median function. Does the list need to be sorted there too, like in the second branch?

Your avg_list function is also masking a built-in function, sum(), which you could use here instead of manually adding:

def avg_list(list): 
    avg = float(sum(list))/len(list)
    print avg

Finally, look at the last line of that function -- it's printing the avg, but stats is expecting it to return the avg. The two aren't the same.

怕倦 2024-12-14 06:14:28

您需要将模块名称放在前面(gm.avg_list()gm.median()),如下所示:

import gm
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = gm.avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = gm.median(list) # same for median
    return stats

一些参考链接和更多信息:

PEP 8 - Python 代码风格指南

Python 命名空间指南,了解 from blah import foo和<代码>导入废话

  • 导入一些模块

这是最简单的导入方法,通常推荐使用。你
如果您使用模块的名称空间,则可以访问该模块的名称空间
名称作为前缀。这意味着您可以在程序中使用名称
与模块中的相同,但您可以使用
他们俩。当您导入大量数据时,它也很有帮助
模块的数量,您可以看到特定名称属于哪个模块。

  • 从 SomeModule 导入 SomeName

这会从模块的名称中导入一个名称(或几个,用逗号分隔)
命名空间直接进入程序的。要使用您导入的名称,
您不再需要使用前缀,只需直接使用名称即可。这个可以
如果您确定只需要使用几个名称,那么这会很有用。
缺点是您不能将导入的名称用于某些内容
其他在你自己的程序中。例如,您可以使用 add() 而不是
Integer.add(),但是如果你的程序有add()函数,你就会输
访问 Integer 的 add() 函数。

You need to put the module name first (gm.avg_list() and gm.median()) like so:

import gm
def stats(list):

    stats = {}  # empty dict named stats
    stats['average'] = gm.avg_list(list) # Stats(key)[average] = mean of the list of numbers [values]
    stats['median'] = gm.median(list) # same for median
    return stats

Some Reference links and more info:

PEP 8 - Style Guide for Python Code

A guide to Python Namespaces on the differences between from blah import foo and import blah

  • import SomeModule

This is the simplest way to do imports and generally recommended. You
get access to the module’s namespace provided you use the module’s
name as a prefix. This means that you can have names in your program
which are the same as those in the module, but you’ll be able to use
both of them. It’s also helpful when you’re importing a large number
of modules as you see which module a particular name belongs to.

  • from SomeModule import SomeName

This imports a name (or a few, separated by commas) from a module’s
namespace directly into the program’s. To use the name you imported,
you no longer have to use a prefix, just the name directly. This can
be useful if you know for certain you’ll only need to use a few names.
The downside is that you can’t use the name you imported for something
else in your own program. For example, you could use add() instead of
Integer.add(), but if your program has an add() function, you’ll lose
access to the Integer’s add() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文