Python专题, 语言

Python中关联多个属性的方法

某个对象有多个属性,例如原子有名称、编号、坐标、势能等属性,它们是相互关联在某个具体的对象上。Python 中关联这几种属性的方法有:类(Class)、字典(Dictionary)、组元(Tuple)等。其中,类和字典是比较常用的方法,使用起来会比较灵活。本篇给出具体的实现例子。

关于类的学习,可以参考这篇:Python中类和继承的使用

一、使用类实现关联

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/45135
"""

class Atom:
    def __init__(self, name='atom', index=0, x=0, y=0, z=0):
        self.name = name
        self.index = index
        self.x = x
        self.y = y
        self.z = z

atom_object_list = []
index = 0
for i0 in range(3):
    for j0 in range(3):
        atom = Atom(index=index, x=i0, y=j0)
        atom_object_list.append(atom)
        index += 1

print(atom_object_list)
for atom_object in atom_object_list:
    print([atom_object.name, atom_object.index, atom_object.x, atom_object.y, atom_object.z])

运行结果:

[<__main__.Atom object at 0x00000238D2A5BF10>, <__main__.Atom object at 0x00000238D2A5BEE0>, <__main__.Atom object at 0x00000238D2A5BE50>, <__main__.Atom object at 0x00000238D2A5BDC0>, <__main__.Atom object at 0x00000238D2A5BD60>, <__main__.Atom object at 0x00000238D2A5BD00>, <__main__.Atom object at 0x00000238D2A5BCA0>, <__main__.Atom object at 0x00000238D2A5BC40>, <__main__.Atom object at 0x00000238D2A5BBE0>]
['atom', 0, 0, 0, 0]
['atom', 1, 0, 1, 0]
['atom', 2, 0, 2, 0]
['atom', 3, 1, 0, 0]
['atom', 4, 1, 1, 0]
['atom', 5, 1, 2, 0]
['atom', 6, 2, 0, 0]
['atom', 7, 2, 1, 0]
['atom', 8, 2, 2, 0]

二、使用字典实现关联

"""
This code is supported by the website: https://www.guanjihuan.com
The newest version of this code is on the web page: https://www.guanjihuan.com/archives/45135
"""

atom_dict_list = []
index = 0
for i0 in range(3):
    for j0 in range(3):
        atom_dict= {
            'name': 'atom',
            'index': index, 
            'x': i0,
            'y': j0,
            'z': 0,
        }
        atom_dict_list.append(atom_dict)
        index += 1

print(atom_dict_list)
for atom_dict in atom_dict_list:
    print([atom_dict['name'], atom_dict['index'], atom_dict['x'], atom_dict['y'], atom_dict['z']])

运行结果:

[{'name': 'atom', 'index': 0, 'x': 0, 'y': 0, 'z': 0}, {'name': 'atom', 'index': 1, 'x': 0, 'y': 1, 'z': 0}, {'name': 'atom', 'index': 2, 'x': 0, 'y': 2, 'z': 0}, {'name': 'atom', 'index': 3, 'x': 1, 'y': 0, 'z': 0}, {'name': 'atom', 'index': 4, 'x': 1, 'y': 1, 'z': 0}, {'name': 'atom', 'index': 5, 'x': 1, 'y': 2, 'z': 0}, {'name': 'atom', 'index': 6, 'x': 2, 'y': 0, 'z': 0}, {'name': 'atom', 'index': 7, 'x': 2, 'y': 1, 'z': 0}, {'name': 'atom', 'index': 8, 'x': 2, 'y': 2, 'z': 0}]
['atom', 0, 0, 0, 0]
['atom', 1, 0, 1, 0]
['atom', 2, 0, 2, 0]
['atom', 3, 1, 0, 0]
['atom', 4, 1, 1, 0]
['atom', 5, 1, 2, 0]
['atom', 6, 2, 0, 0]
['atom', 7, 2, 1, 0]
['atom', 8, 2, 2, 0]
9 次浏览

【说明:本站主要是个人的一些笔记和代码分享,内容可能会不定期修改。为了使全网显示的始终是最新版本,这里的文章未经同意请勿转载。引用请注明出处:https://www.guanjihuan.com

2 thoughts on “Python中关联多个属性的方法”

    1. 嗯,用的少,但很多软件包都会有涉及到。用了类之后会复杂很多,每个类都有很多属性和方法,不方便记忆。我感觉对于部分经常用的对象,是可以考虑通过类来包装,当然也可以用字典或者其他方法来代替。

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code