Python Forum
class random var write to array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class random var write to array
#5
I tried to refactor your code in a way it makes sense from what i assume to understand you want to do.
Look and learn.
import numpy as np
 
class Data:
    
    def __init__(self, x, y, r, g, b):
        self.x = x
        self.y = y
        self.r = r
        self.g = g
        self.b = b
        # or to make in some way possible what you want to do
        self.data = {'x':x, 'y':y, 'R':r, 'G':g, 'B':b}
    
def randomi():
    return [np.random.randint(0, 100) for i in range(5)]
    

def main():
    arr = np.zeros((5), dtype='int64')
    print(arr)
    
    x, y, r, g, b = randomi()
    print(x, y, r, g, b)
    
    p = Data(x, y, r, g, b)
    
    for i, character in enumerate('xyRGB'):
        arr[i] = p.data[character]
        print(character, arr[i])
    
    print(arr)

    
if __name__ == "__main__":
    main()
Reply


Messages In This Thread
class random var write to array - by storzo - Aug-02-2019, 11:05 AM
RE: class random var write to array - by fishhook - Aug-02-2019, 11:20 AM
RE: class random var write to array - by ThomasL - Aug-02-2019, 11:58 AM
RE: class random var write to array - by storzo - Aug-02-2019, 12:22 PM
RE: class random var write to array - by ThomasL - Aug-02-2019, 02:21 PM
RE: class random var write to array - by storzo - Aug-02-2019, 03:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How Write Part of a Binary Array? Assembler 1 354 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  How do I write class objects to a file in binary mode? Exsul1 7 5,780 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Multi-Dimm Array or Class? PappaBear 2 2,269 May-02-2019, 07:04 PM
Last Post: PappaBear
  20 x 20 Integer array with random numbers harryk 3 3,340 Jul-28-2018, 03:38 PM
Last Post: harryk
  Write selected (random) columns to output-file anjohepa 0 2,347 Feb-27-2018, 02:19 PM
Last Post: anjohepa
  Best construct? Array, class, other? PappaBear 1 2,976 May-10-2017, 06:02 PM
Last Post: nilamo
  What work faster and take less memory array or class? Kamilbek 1 3,135 Apr-20-2017, 05:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020