对象列表 [英] List of Objects

查看:55
本文介绍了对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于愿意倾听的人来说,这是一个(可能)快速的问题。

我对列表和类有疑问;我有一个名为

" gazelle"有几个属性(颜色,位置等),我需要

来创建一群他们。我想模拟个人的运动,但是我不想通过手动更新

每只瞪羚的位置(可能会有更高的瞪羚) 50)。我打算计划创建这些瞪羚类的数组,然后我会用

迭代它来调整每只瞪羚的位置。不管怎么说,那是怎么用C进行的。但是,Python不支持指针

,我不太确定如何解决这个问题。任何你能提供的帮助

将不胜感激。

非常感谢!


-Ryan

解决方案

datamonkey.r ... @ gmail.com写道:


>

但是,Python不支持指针



据我所知,Python中的每个名字都是一个指针。


类瞪羚(对象):

def __init __(自我):

self.x = 0


g_list = []

for x in range(10):

g_list.append(Gazelle())


for g in g_list:

gx = 10

print g_list [2] .x

4月20日凌晨3:58,datamonkey.r ... @ gmail.com写道:


你好,一个(可能)快速提问任何愿意倾听的人。

我对列表和类有疑问;我有一个名为

" gazelle"有几个属性(颜色,位置等),我需要

来创建一群他们。我想模拟个人的运动,但是我不想通过手动更新

每只瞪羚的位置(可能会有更高的瞪羚) 50)。我打算计划创建这些瞪羚类的数组,然后我会用

迭代它来调整每只瞪羚的位置。不管怎么说,那是怎么用C进行的。但是,Python不支持指针

,我不太确定如何解决这个问题。任何你能提供的帮助

将不胜感激。

非常感谢!


-Ryan



类似于:


随机导入

class Gazelle(对象):

def __init __(self):

self.pos = 0,0


#创建实例列表

gazelles = [Gazelle ()for x in range(5)]


#更新瞪羚位置

deltaxmin,deltaxmax = -100,+ 100

deltaymin,deltaymax = -100,+ 100
$ g $ b for g gazelles:

g.pos =(g.pos [0] + random.randint(deltaxmin,deltaxmax) ),

g.pos [1] + random.randint(deltaymin,deltaymax))


以上是未经测试的。


- Paddy。


2007年4月19日星期四, da ************* @ gmail.com 写道:


你好,对任何人来说都是一个(可能)快速的问题愿意倾听。

我对列表和类有疑问;我有一个名为

" gazelle"有几个属性(颜色,位置等),我需要

来创建一群他们。我想模拟个人的运动,但是我不想通过手动更新

每只瞪羚的位置(可能会有更高的瞪羚) 50)。我打算计划创建这些瞪羚类的数组,然后我会用

迭代它来调整每只瞪羚的位置。不管怎么说,那是怎么用C进行的。但是,Python不支持指针

,我不太确定如何解决这个问题。您可以提供的任何帮助

将不胜感激。

非常感谢!



实际上,Python _only_支持指针:它们是对象的名称。所以

例如,如果我写x = Gazelle(...),那么我创建名称x

指向Gazelle的一个实例。实例的存储是由Python管理的魔法。如果我当时要说y = x,我也会有

名称y这指向同一个实例。


同样值得注意的是,包括整数,字符串和列表在内的所有东西都是

对象。因此,指向对象的指针是python中唯一的存储

类。因此,名称可以指向任何类型的任何对象。


结果,一个数组在Python中,它通常是一个列表,只是一个列表

的指针。他们可以指向字符串,整数,其他列表或任何东西。并且

因为它们存储指针,它们实际上可以包含它们自己!


要演示:


>> a = [1,''a'',[1,2,3]]
for i in a:print i



1

a

[1,2,3]


>> a.append(a)
for i in a:打印i



1

a

[1,2,3]

[1,'''',[1,2,3],[...]]

Python'聪明到不打印出来循环引用。


最后,值得指出的是,在这样的语言中,哪里有

没有任意指针(就像有的一样) C),指向对象的指针称为

引用。我只是使用了指针因为你做了;)。


Howdy, a (possibly) quick question for anyone willing to listen.
I have a question regarding lists and Classes; I have a class called
"gazelle" with several attributes (color, position, etc.) and I need
to create a herd of them. I want to simulate motion of individual
gazelles, but I don''t want to have to go through and manually update
the position for every gazelle (there could be upwards of 50). I was
planning to create an array of these gazelle classes, and I was going
to iterate through it to adjust the position of each gazelle. That''s
how I''d do it in C, anyway. However, Python doesn''t support pointers
and I''m not quite sure how to go about this. Any help you can provide
would be greatly appreciated.
Thanks a lot!

-Ryan

解决方案

datamonkey.r...@gmail.com wrote:

>
However, Python doesn''t support pointers

As I understand it, every name in Python is a pointer.

class Gazelle(object):
def __init__(self):
self.x = 0

g_list =[]
for x in range(10):
g_list.append(Gazelle())

for g in g_list:
g.x = 10

print g_list[2].x


On Apr 20, 3:58 am, datamonkey.r...@gmail.com wrote:

Howdy, a (possibly) quick question for anyone willing to listen.
I have a question regarding lists and Classes; I have a class called
"gazelle" with several attributes (color, position, etc.) and I need
to create a herd of them. I want to simulate motion of individual
gazelles, but I don''t want to have to go through and manually update
the position for every gazelle (there could be upwards of 50). I was
planning to create an array of these gazelle classes, and I was going
to iterate through it to adjust the position of each gazelle. That''s
how I''d do it in C, anyway. However, Python doesn''t support pointers
and I''m not quite sure how to go about this. Any help you can provide
would be greatly appreciated.
Thanks a lot!

-Ryan

Something like:

import random
class Gazelle(object):
def __init__(self):
self.pos = 0, 0

# create a list of instances
gazelles= [ Gazelle() for x in range(5)]

# update gazelle positions
deltaxmin, deltaxmax = -100, +100
deltaymin, deltaymax = -100, +100
for g in gazelles:
g.pos = (g.pos[0] + random.randint(deltaxmin, deltaxmax),
g.pos[1] + random.randint(deltaymin, deltaymax) )

The above is untested by the way.

- Paddy.


On Thursday 19 April 2007, da*************@gmail.com wrote:

Howdy, a (possibly) quick question for anyone willing to listen.
I have a question regarding lists and Classes; I have a class called
"gazelle" with several attributes (color, position, etc.) and I need
to create a herd of them. I want to simulate motion of individual
gazelles, but I don''t want to have to go through and manually update
the position for every gazelle (there could be upwards of 50). I was
planning to create an array of these gazelle classes, and I was going
to iterate through it to adjust the position of each gazelle. That''s
how I''d do it in C, anyway. However, Python doesn''t support pointers
and I''m not quite sure how to go about this. Any help you can provide
would be greatly appreciated.
Thanks a lot!

Actually, Python _only_ supports pointers: they''re the names of objects. So
for example, if I write x = Gazelle(...), then I create the name "x" that
points to an instance of Gazelle. The storage for the instance is
managed ''magically'' by Python. If I were then to say "y = x", I''d also have
a name "y" that points to the same instance.

It''s also worth noting that everything, including ints, strings and lists are
objects as well. Because of this, a pointer to an object is the only storage
class in python. Therefore, a name can point to any object of any type.

As a result, an "array" in Python, which is commonly a list, is simply a list
of pointers. They can point to strings, ints, other lists or anything. And
because they store pointers, they can actually include themself!

To demonstrate:

>>a=[1,''a'',[1,2,3]]
for i in a: print i

1
a
[1, 2, 3]

>>a.append(a)
for i in a: print i

1
a
[1, 2, 3]
[1, ''a'', [1, 2, 3], [...]]
Python''s clever enough to not print out the circular reference.

Finally, it''s worth pointing out that in a language like this, where there are
no arbitrary pointers (as there are in C), the pointer-to-object is called a
reference. I just used "pointer" because you did ;).


这篇关于对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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