0

So I have this class called Data And the class has two attributes; var_names and units is it possible to append list elements of the list (which belongs to attribute units) to each element in the list called var_names? So when I make a new object called myData, the bellow output is possible (so the sublist [1,2,3,4] would be associated to id element?

>>> myData=Data([[1,2,3,4],["a","b","c","d"],
[2.3,2.1,2.5,3.1]],var_names=["id","name","length"])
>>> print myData
id name length
1    a    2.3
2    b    2.1
3    c    2.5
4    d    3.1

Or that's not possible? And be only achieved visually (way of formatting the output)

4
  • So, basically, it's a namedtuple? Commented Dec 4, 2012 at 17:12
  • I don't think I understand... Commented Dec 4, 2012 at 17:13
  • namedtuple Commented Dec 4, 2012 at 17:13
  • I'm not sure if that's the case. Because I got this object call and output, now I have to reproduce the code based by the given info: var_names=["id","name","length"] and this doesn't seem to be a namedtuple? Commented Dec 4, 2012 at 17:19

2 Answers 2

2

You actually need namedtuple as others have suggested. Here is a possible implementation of what you would need

>>> class Data(object):
    def __init__(self,*args,**kwargs):
        args = args[0]
        var_names = kwargs['var_names']
        self.var_names = namedtuple('Data',var_names)
        self.units = [self.var_names(*e) for e in zip(*args)]
    def __repr__(self):
        fields = [e for e in vars(self.units[0])]
        fmt_string = '{:^10}'*len(fields)
        return '\n'.join(fmt_string.format(*units) for units in self.units)


>>> myData=Data([[1,2,3,4],["a","b","c","d"],
[2.3,2.1,2.5,3.1]],var_names=["id","name","length"])
>>> print myData
    1         a        2.3    
    2         b        2.1    
    3         c        2.5    
    4         d        3.1    
>>> 
Sign up to request clarification or add additional context in comments.

1 Comment

This sounds good... but what if I'd like to delete 'id' or 'name' Because i've been playing a while with this solution. But I am unable to make a function which would delete values of a selected var for example my_Data.del('lenght') so the out put would contain only id and name.
0

If I'm understanding you right I think using a dictionary in place of a list for var_names should do what you want. Use collections.OrderedDict if ordering is important.

>>> import collections
>>> var_names = collections.OrderedDict([('id',[1,2,3,4]), 
                                         ('name',['a','b','c','d']), 
                                         ('length',[2.3,2.1,2.5,3.1])])
>>> for name, value in var_names.items(): 
...     print name + ': ' + str(value)
id: [1, 2, 3, 4]
name: ['a', 'b', 'c', 'd']
length: [2.3, 2.1, 2.5, 3.1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.