简体   繁体   English

如何在Python类对象中创建唯一列表?

[英]How to make unique lists across Python class objects?

I am trying to make a python class that keeps a list of uniformly distributed random numbers per class but the numbers must be unique to that class. 我正在尝试制作一个python类,该类保留每个类的均匀分布的随机数列表,但是数字对于该类必须是唯一的。 However the list in each new object is copied exactly as is from the first object and if I manually append new values to the contents of the second objects list, the same change is applied to the first object's list and for every new class I create the list is again copied and it appends however many values I specified. 但是,每个新对象中的列表都是从第一个对象中复制的,而如果我手动将新值附加到第二个对象列表的内容中,则将相同的更改应用于第一个对象的列表,并且对于每个新类,我都会创建一个列表再次被复制,并附加我指定的许多值。

The code is as follows: 代码如下:

import random as random

class myClass:
    myRandomList = []

    def __init__(self, lengthOfList):
        for i in range(0,lengthOfList):
            self.myRandomList.append(random.uniform(-1.0,1.0))

a = myClass(2) #make a list with 2 random numbers
b = myClass(1) #make a list with 1 random number
print(a.myRandomList)
print(b.myRandomList)

The output of the above code gives the following: 上面的代码输出如下:

[0.3640932307078235, 0.8858169279430881, 0.18712932281033723]
[0.3640932307078235, 0.8858169279430881, 0.18712932281033723]

When it should be giving something like this rather: 什么时候应该给出这样的东西:

[0.3640932307078235, 0.8858169279430881]
[0.18712932281033723]

How can I make it so that when a new class is created it creates an entirely new list of values for that class only without copying the list from a previously instantiated object and without affecting previous object's lists? 我怎样才能做到,当创建一个新类时,它只为该类创建一个全新的值列表,而不会从先前实例化的对象中复制该列表,又不会影响先前对象的列表?

I think what you want is for every instance of a class (ie a and b in your code) to have its own list of random numbers, matching the length you specified when you created the instance. 我认为您想要的是每个类实例 (即代码中的a和b)都有自己的随机数列表,与创建实例时指定的长度匹配。 To do this, you should set self.myRandomList within init . 为此,您应该在init中设置self.myRandomList。 Here's the change to your original code: 这是对原始代码的更改:

import random as random

class myClass:

  def __init__(self, lengthOfList):
    self.myRandomList = []
    for i in range(0,lengthOfList):
      self.myRandomList.append(random.uniform(-1.0,1.0))

a = myClass(2) #make a list with 2 random numbers
b = myClass(1) #make a list with 1 random number
print(a.myRandomList)
print(b.myRandomList)

What you were doing before was setting myRandomList as a class variable, which will always be shared by all instances of the class, rather than an instance variable. 您之前所做的是将myRandomList设置为类变量,该变量将始终由该类的所有实例共享,而不是由实例变量共享。 See here for more details: https://www.digitalocean.com/community/tutorials/understanding-class-and-instance-variables-in-python-3 详情请参阅这里: https : //www.digitalocean.com/community/tutorials/understanding-class-and-instance-variables-in-python-3

Hope that helps! 希望有帮助!

Simply declaring self.myRandomList in __init__ , rather than in the class declaration does the trick. 只需在__init__而不是在类声明中声明self.myRandomList

class myClass:
    def __init__(self, lengthOfList):
        self.myRandomList = []
        for i in range(0,lengthOfList):
            self.myRandomList.append(random.uniform(-1.0,1.0))

If you want to ensure that each object has unique numbers across all instances then you'll need to have each instance check the generated values against a class level set to ensure uniqueness. 如果要确保每个对象在所有实例中都有唯一的编号,则需要让每个实例根据setclass级别检查生成的值以确保唯一性。

import random as random

class MyClass:

    class_random_set = set()

    def __init__(self, lengthOfList):
        self.my_random_set = set()
        for _ in range(0, lengthOfList):
            while True:
                num = random.uniform(-1.0, 1.0)
                if num not in self.class_random_set:
                    self.class_random_set.add(num)
                    self.my_random_set.add(num)
                    break


a = MyClass(2) #make a set with 2 random numbers
b = MyClass(1) #make a set with 1 random number
print(a.my_random_set)
print(b.my_random_set)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM