1

I have some basic data that I want to store and I'm looking for a better solution then what I've come up with. I have multiple entries of data with 4 fields per entry, name, url, currYear, availYears

I can solve this with a simple array of arrays like so:

data = [
    ['test-name', ['http://example.com', '2015', '2015,2014']]
    ['next-name', ['http://example.org', '1999', '1999']]
]

But this gets messy when trying to access data in each array. I end up with a for loop like this

for each in data:
    name = each[0]
    url = each[1][0]
    currYear = each[1][1]

I'd prefer to do something similar to a dict where I can reference what I want by a key name. This isn't valid syntax, but hopefully it gets the point across.

data = {'entry1': {'name': 'test-name'}, {'url': 'http://example.com'}, {'currYear': '2015'}, {'availYears': '2015,2014'}}

Then I could pull the url data for entryX.

EDIT: Several good responses. I decided to go with creating a class since 1) it satisfies my need 2) helps clean up the code by segregating functionality and 3) learn how packages, modules and classes work compared to Java (which I'm more familiar with). In addition to creating the class, I also created getters and setters.

class SchoolSiteData(object):
    def __init__(self, name, url, currYear, availYears):
        self.name = name
        self.url = url
        self.currYear = currYear
        self.availYears = availYears

    def getName(self):
        return self.name
    def getURL(self):
        return self.url
    def getCurrYear(self):
        return self.currYear
    def getAvailYears(self):
        return self.availYears

    def setName(self, name):
        self.name = name
    def setURL(self, url):
        self.url = url
    def setCurrYear(self, currYear):
        self.currYear = currYear
    def setAvailYears(self, availYears):
        self.availYears = availYears
5
  • Would a namedtuple fit your needs? Commented Nov 4, 2015 at 22:58
  • 3
    Why not this then? data = {'entry1': {'name': 'test-name', 'url': 'http://example.com', 'currYear': '2015', 'availYears': '2015,2014'}} Commented Nov 4, 2015 at 22:58
  • 1
    Are the names unique? If so, you could use that as the key: data = {'test-name': {'url': 'http://example.com', 'currYear': '2015', 'availYears': '2015,2014'}} Commented Nov 4, 2015 at 23:00
  • As @metatoaster suggested, you should use a dict whose values are dicts. Commented Nov 4, 2015 at 23:00
  • You can take a look at Python's Pandas lib. Commented Nov 4, 2015 at 23:02

5 Answers 5

3

A class may make this easier to use: eg:

    class Entry(object):
        def __init__(self, name, url, currYear, availYears):
            self.name = name
            self.url = url
            self.currYear = currYear
            self.availYears = availYears

    entry1 = Entry('test-name', 'http://example.com', '2015', '2015,2014')
    entry2 = Entry('next-name', 'http://example.org', '1999', '1999')

    data = [entry1, entry2]

    for entry in data:
        print entry.name
        print entry.url 
        print entry.currYear 
        print entry.availYears
        print
Sign up to request clarification or add additional context in comments.

1 Comment

All the answers submitted here are perfectly valid and will get the job done, but this one would best prepare the OP for expanding his coding skillset.
3

Use the names as the keys in a dictionary:

data = {'test-name': 
           {'url': 'http://example.com', 
            'currYear': '2015', 
            'availYears': '2015,2014'
           }
       }

Access like so:

data['test-data']['url']

1 Comment

This is clear and easy to use, but note that each dict adds something like 500 bytes of overhead; a namedtuple takes no extra memory.
2

You seem to have needlessly complicated things with the list-in-list solution. If you keep it a little flatter, you can just unpack the rows into variables:

data = [
    ['test-name', 'http://example.com', '2015', '2015,2014'],
    ['next-name', 'http://example.org', '1999', '1999']
]

for name, url, currYear, availYears in data:
    ....

1 Comment

There are a lot of perfectly valid solutions to the OP's problem, but this one is the simplest and most straightforward for a beginner.
1

The most light-weight solution for what you want is probably a namedtuple.

>>> from collections import namedtuple
>>> mytuple = namedtuple("mytuple", field_names="url currYear availYears")
>>> data = [ 'test-name': mytuple('http://example.com', '2015', '2015,2014'), ...
    ... ]
>>> print(data['test-name'])
mytuple(url='http://example.com', currYear='2015', availYears='2015,2014')

You can access members by numerical index or by name:

>>> x = data['test-name']
>>> print(x.currYear)
2015
>>> print(x[1])
2015

Comments

0
data = [
    {'name': 'test-name', 'url': 'http://example.com', 'currYear': '2015', 'availYears': '2015,2014'},
    {'name': 'next-name', 'url': 'http://example.org', 'currYear': '1999', 'availYears': '1999'}
]
for each in data:
    name = each['name']
    url = each['url']
    currYear = each['currYear']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.