Python list reference mapping

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Python list reference mapping

Postby Ptolom » Fri Aug 17, 2012 8:23 pm UTC

In Python, is there a way of aliasing several lists to the indexes a single list, so that the elements of the old lists and the new list refer to the same objects as the original lists. Something like
Code: Select all
>a=[1,2,3,4]
>b=[5,6,7,8]
>c=mapping_function(a,b)
>print c
[1,2,3,4,5,6,7,8]
>c[0]="foo"
>print a
['foo',2,3,4]

I have a shape class containing path objects which contain points and I want shape.points to be a list referring to the points in each path in shape.
It Should Be Real wrote:Fuck the wizard.
We're doing this manually.

http://www.hexifact.co.uk - Hacking blog: in which I take some things apart, and put other things together.
User avatar
Ptolom
 
Posts: 1428
Joined: Mon Mar 24, 2008 1:55 pm UTC
Location: The entropy pool (Leicester)

Re: Python list reference mapping

Postby WanderingLinguist » Sat Aug 18, 2012 12:57 am UTC

There's nothing built in, but you can make one:

Code: Select all
class mergelist:
    def __init__(self, *args):
        self.listlist=args
    def __getitem__(self,key):
        for list in self.listlist:
            if key < len(list):
                return list[key]
            key = key - len(list)
        raise IndexError
    def __setitem__(self,key,value):
        for list in self.listlist:
            if key < len(list):
                list[key]=value
                return
            key = key - len(list)
        raise IndexError
    def __len__(self):
        return sum([len(list) for list in self.listlist])
    def __iter__(self):
        for list in self.listlist:
            for item in list:
                yield item
    def __str__(self):
        s = "["
        for list in self.listlist:
            for item in list:
                s = s + str(item) + ", "
        return s[:-2] + "]"
    def __repr__(self):
        s = "["
        for list in self.listlist:
            for item in list:
                s = s + repr(item) + ", "
        return s[:-2] + "]"


It's probably missing a few bits; I threw it together really quick. It ought to have __reverse__ and a few other methods. It also may need type checking in some places (it should raise a TypeError if the arguments to the constructor aren't all lists). But it's a starting point.

Here's how you use it:

Code: Select all
>>> a = [0,1,2,3]
>>> b = [4,5]
>>> c = [6,7,8]
>>> d = [9,10,11]
>>> e = [12]
>>>
>>> x = mergelist(a,b,c,d,e)
>>> a
[0, 1, 2, 3]
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> x[2] = "Hello"
>>> x
[0, 1, Hello, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
>>> a
[0, 1, 'Hello', 3]
>>> x[12] = "stuff"
>>> x
[0, 1, Hello, 3, 4, 5, 6, 7, 8, 9, 10, 11, stuff]
>>> e
['stuff']
>>> c[2] = "12345"
>>> c
[6, 7, '12345']
>>> x
[0, 1, Hello, 3, 4, 5, 6, 7, 12345, 9, 10, 11, stuff]
>>> for item in x:
...     print item
...
0
1
Hello
3
4
5
6
7
12345
9
10
11
stuff
>>>
User avatar
WanderingLinguist
 
Posts: 147
Joined: Tue May 22, 2012 5:14 pm UTC
Location: Seoul

Re: Python list reference mapping

Postby Ptolom » Sat Aug 18, 2012 7:48 pm UTC

That's perfect, thank you. I thought it should be possible to create a class with its own list functions.
It Should Be Real wrote:Fuck the wizard.
We're doing this manually.

http://www.hexifact.co.uk - Hacking blog: in which I take some things apart, and put other things together.
User avatar
Ptolom
 
Posts: 1428
Joined: Mon Mar 24, 2008 1:55 pm UTC
Location: The entropy pool (Leicester)

Re: Python list reference mapping

Postby thedufer » Mon Aug 20, 2012 10:59 pm UTC

If you're looking for something quick and easy, you can wrap each element in a list as so:

Code: Select all
>>> a = [[1],[2],[3],[4]]
>>> b = [[5],[6],[7],[8]]
>>> c = a + b
>>> c
[[1],[2],[3],[4],[5],[6],[7],[8]]
>>> c[2][0] = "Hello"
>>> c
[[1],[2],["Hello"],[4],[5],[6],[7],[8]]
>>> a
[[1],[2],["Hello"],[4]]
User avatar
thedufer
 
Posts: 263
Joined: Mon Aug 06, 2007 2:11 am UTC
Location: Northern VA (not to be confused with VA)

Re: Python list reference mapping

Postby troyp » Tue Aug 21, 2012 2:45 am UTC

WanderingLinguist wrote:It's probably missing a few bits; I threw it together really quick. It ought to have __reverse__ and a few other methods. It also may need type checking in some places (it should raise a TypeError if the arguments to the constructor aren't all lists). But it's a starting point.

Ideally, the class should be a subclass of list. That'll give you all the methods by inheritance and also make the instances actual lists (rather than just sequences that print out like lists).
troyp
 
Posts: 398
Joined: Thu May 22, 2008 9:20 pm UTC
Location: Lismore, NSW


Return to Coding

Who is online

Users browsing this forum: Carnildo and 13 guests