Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom data structure
#1
New to python, from .Net. I know python does not support arrays.

This class

class myStructure:
    """ My custom data structure for arrays """

    def __init__(self, date1=dt.datetime(2000,12,31), num1=0, str1="Nothing"):
        self.date1 = date1
        self.num1 = num1
        self.str1 = str1
I can do this ONCE
# Class Instance
res = my.myStructure()
res.str1 = "Yes"
res.num1 = 50
res.date1 = my.dt.datetime.strptime("05/01/1950","%m/%d/%Y")
Question: How can I use myStructure to store many records of data (like an array), or may be I cant?
Can I convert my class structure into a dictionary or something ??

Please advise
Reply
#2
I'm not sure what exactly your looking for. You could do a list of your object:

data = []
data.append(my.myStructure(dt.datetime(1950, 5, 1), 50, 'Yes'))
data.append(my.myStructure(dt.datetime(1970, 2, 25), 0, 'Up'))
data.append(my.myStructure(dt.datetime(1980, 8, 10), 108, 'Eno'))
Now you have a list of three instances of your class. Or you could implement a list in myStructure:

class myStructure(object):

    def __init__(self):
        data = []

    def append(self, date=dt.datetime(2000,12,31), num=0, text="Nothing"):
        data.append((date, num, text))
Now you have an object that has a list of tuples, with the data previously stored in myStructure. You could then override the other list methods, and magic methods like __getitem__, to make it act more like a list. Or you could sub-class one of the classes in collections.abc, which would probably be easier.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Great that works, make the class the tuples( or array type). That acts like a structured array found in .Net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I add certain elements in this 2d data structure and calculate a mean TheOddCircle 3 1,543 May-27-2022, 09:09 AM
Last Post: paul18fr
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 2,793 Apr-23-2021, 08:09 AM
Last Post: sx999
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,279 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  what data structure to use? Winfried 4 2,815 Mar-16-2021, 12:11 PM
Last Post: buran
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,715 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  How to use Bunch data structure moish 2 2,902 Dec-24-2020, 06:25 PM
Last Post: deanhystad
  Correct data structure for this problem Wigi 13 4,618 Oct-09-2020, 11:09 AM
Last Post: buran
  Plotting 3D Data with Custom Colorbar Gates666 0 1,690 Jul-09-2020, 10:56 AM
Last Post: Gates666
  ctypes and custom data type (structures, pointer to structures) python_user_n 0 2,652 Jul-08-2020, 05:52 PM
Last Post: python_user_n
  difficulties to chage json data structure using json module in python Sibdar 1 2,080 Apr-03-2020, 06:47 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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