将值附加到对象内的数组(循环对象) [英] Appending values to an array within an object (looping over objects)

查看:40
本文介绍了将值附加到对象内的数组(循环对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 这个 问题.因为我改了很多所以提出了一个新问题

我试图从一个较长的数组 solution 中获取某些值,并将它们放入一个较小的数组中,在一个对象中.这段代码应该将solution数组的前半部分放入m1中的x_histm1中的后半部分>solution 数组并将其放入 m2 内的 x_hist 中.相反,它似乎将所有 solution 数组放入 x_hist 中,用于两个对象.有谁知道为什么会这样?我是不是不小心矢量化了代码?

类质量:x_hist = []m1 = 质量()m2 = 质量()毫秒 = [m1,m2]解 = [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0]对于我在范围内(len(ms)):对于范围内的 k(int(len(sol)/len(ms))):ms[i].x_hist.append(解决方案[k+8*i])打印(m1.x_hist)打印(m2.x_hist)

输出为:

[1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0][1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0]

我正在尝试获得以下输出:

[1, 2, 3, 4, 5, 6, 7, 8][0, 0, 0, 0, 0, 0, 0, 0]

解决方案

x_hist 属性是静态类属性

在类内部声明的变量是与实例上下文不同的静态变量,这意味着,

<预><代码>>>>s = M()>>>秒<__main__.M 对象在 0x7f9ffb4a6358>>>>s.i3>>>米3>>>s.i = 4>>>米3>>>s.i4班级质量:#x_hist = [] 所有类共享其静态def __init__(self):self.x_hist = []m1 = 质量()m2 = 质量()毫秒 = [m1,m2]解 = [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0]对于我在范围内(len(ms)):对于范围内的 k(int(len(sol)/len(ms))):ms[i].x_hist.append(解决方案[k+8*i])打印(m1.x_hist)打印(m2.x_hist)

对于静态方法和类方法检查这里

有关课程的好教程,请参阅此处

This is an uncluttered version of this question. Since I changed so much I made a new question

I am trying to take certain values from a longer array solution and put them into a smaller array, within an object. This code is supposed to take the first half of the solution array and put it into x_hist within m1, and the second half of the solution array and put it into x_hist within m2. Instead it appears to take all of the solution array and put it into x_hist for both objects. Anyone know why this may be the case? Have I accidentally vectorized the code?

class Mass:
    x_hist = []

m1 = Mass()
m2 = Mass()
ms = [m1,m2]

solution = [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0]

for i in range(len(ms)):
    for k in range(int(len(sol)/len(ms))):
        ms[i].x_hist.append(solution[k+8*i])

print(m1.x_hist)
print(m2.x_hist)

The output is:

[1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0]

I am trying to get an output of:

[1, 2, 3, 4, 5, 6, 7, 8]
[0, 0, 0, 0, 0, 0, 0, 0]

解决方案

x_hist property is static class property

variables Declared inside classes are static variables different from the context of instance which means,

>>> s = M()
>>> s
<__main__.M object at 0x7f9ffb4a6358>
>>> s.i
3
>>> M.i
3
>>> s.i = 4
>>> M.i
3
>>> s.i
4



class Mass:
    #x_hist = [] shared by all classes its static
    def __init__(self):
        self.x_hist = []

m1 = Mass()
m2 = Mass()
ms = [m1,m2]

solution = [1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0]

for i in range(len(ms)):
    for k in range(int(len(sol)/len(ms))):
        ms[i].x_hist.append(solution[k+8*i])

print(m1.x_hist)
print(m2.x_hist)

For static Method and ClassMethod check Here

For Nice Tutorial for Classes Refer Here

这篇关于将值附加到对象内的数组(循环对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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