我如何使数组全局化 [英] how do i make an array global

查看:60
本文介绍了我如何使数组全局化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def fn():

for i in range(l)

全球统计

count [i] = ....


如果它是一个数组,我怎么声明count是全局的


随后我应该访问或定义count作为数组


错误:

全局名称''count''未定义


谢谢

-a

def fn():
for i in range(l)
global count
count[i]= ....

how do i declare count to be global if it is an array

subsequently i should access or define count as an array

error:
global name ''count'' is not defined

thanks
-a

推荐答案

a写道:
a wrote:
def fn():
对于我在范围内(l)
全局计数
count [i] = ....
如果它是一个数组,如何将count声明为全局
def fn():
for i in range(l)
global count
count[i]= ....

how do i declare count to be global if it is an array




count = [...]


def fn():

全局计数

for i in range(l):

count [i] = ...


-

Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/

美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis

每个人都有寻找解决方案的问题。

- Ashley Montagu



count = [...]

def fn():
global count
for i in range(l):
count[i] = ...

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Every human being is a problem in search of a solution.
-- Ashley Montagu


a写道:
def fn():
我在范围内(l)


l未定义 - 这里你应该有一个错误。

全局计数
count [i] = ....
如果它是一个数组,我如何将count声明为全局


就像它是一个整数

随后我应该访问或定义count作为数组


你需要在之前定义计数。 />
错误:
全局名称''count''未定义
def fn():
for i in range(l)
l is not defined - you should have an error here.
global count
count[i]= ....

how do i declare count to be global if it is an array
Just like it was an integer
subsequently i should access or define count as an array
You need to define count before.
error:
global name ''count'' is not defined




他...


*但是*

你可能不应该这样做。全球是邪恶的*。并且

函数修改全局变量是最糟糕的事情。这里有很少的机会你需要*全球。


此外,和FWIW:

- Python有列表,不是数组(在某些数字中有一个数组类型

包,但那是另一个野兽)

- ''l''非常糟糕姓名

- ''count''是列表的坏名字 - ''count''会更好(当我

看到名字''count'' ,我想一个整数)


-

bruno desthuilliers

python -c" print''@'' .join([''。''。join([w [:: - 1] for p in p.split(''。'')])for

p in''o ** ** @ xiludom.gro''。split(''''')])"



He...

*but*
You probably should not do that anyway. Globals are *evil*. And
functions modifying globals is the worst possible thing. There are very
few chances you *need* a global here.

Also, and FWIW:
- Python has lists, not arrays (there''s an array type in some numerical
package, but that''s another beast)
- ''l'' is a very bad name
- ''count'' is a bad name for a list - ''counts'' would be better (when I
see the name ''count'', I think of an integer)

--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


Erik Max Francis写道:
Erik Max Francis wrote:
a写道:
a wrote:
def fn():
我在范围内(l)
全局计数
count [i] = ....

如果它是一个数组,我如何将count声明为全局
def fn():
for i in range(l)
global count
count[i]= ....

how do i declare count to be global if it is an array



def = [...]
def fn():
全局计数
我在范围(l):
count [i] = ...



count = [...]

def fn():
global count
for i in range(l):
count[i] = ...




不需要全局在这里。


Georg



No need for "global" here.

Georg


这篇关于我如何使数组全局化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆