Reference to Part of List - Python

后端 未结 4 1155
暖寄归人
暖寄归人 2020-12-11 02:01

If I have a list in python, how can I create a reference to part of the list? For example:

myList = [\"*\", \"*\", \"*\",  \"*\", \"*\", \"*\", \"*\", \"*\",         


        
相关标签:
4条回答
  • 2020-12-11 02:23

    Use a slice object or an islice iterator?

    http://docs.python.org/library/functions.html#slice

    0 讨论(0)
  • 2020-12-11 02:30

    You can write a list view type. Here is something I have written as experiment, it is by no means guaranteed to be complete or bug-free

    class listview (object):
        def __init__(self, data, start, end):
            self.data = data
            self.start, self.end = start, end
        def __repr__(self):
            return "<%s %s>" % (type(self).__name__, list(self))
        def __len__(self):
            return self.end - self.start
        def __getitem__(self, idx):
            if isinstance(idx, slice):
                return [self[i] for i in xrange(*idx.indices(len(self)))]
            if idx >= len(self):
                raise IndexError
            idx %= len(self)
            return self.data[self.start+idx]
        def __setitem__(self, idx, val):
            if isinstance(idx, slice):
                start, stop, stride = idx.indices(len(self))
                for i, v in zip(xrange(start, stop, stride), val):
                    self[i] = v
                return
            if idx >= len(self):
                raise IndexError(idx)
            idx %= len(self)
            self.data[self.start+idx] = val
    
    
    L = range(10)
    
    s = listview(L, 2, 5)
    
    print L
    print s
    print len(s)
    s[:] = range(3)
    print s[:]
    print L
    

    Output:

    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    <listview [2, 3, 4]>
    3
    [0, 1, 2]
    [0, 1, 0, 1, 2, 5, 6, 7, 8, 9]
    

    You may assign to indices in the listview, and it will reflect on the underlying list. However,it does not make sense to define append or similar actions on the listview. It may also break if the underlying list changes in length.

    0 讨论(0)
  • 2020-12-11 02:33

    I think that it is impossible. It would lead to many possible errors, for example: what when you append the list which is reference to part of a bigger list? Shall next element in big list be replaced, or inserted?

    As far as I know, silce is internal mechanism for getting elements of the list. They do not create new list object, referencing to parts of older list object. Islice just iterates over the elements given by slice, it is also not the reference, but the actual object - changing it doesn't affect original list. Or am I mistaken?

    As in comment, and that solution contributes really to Mr. Bastien, you can do:

    sliceobject = slice(0,7,3)
    for i in xrange(sliceobject.start, sliceobject.stop, sliceobject.step)
        myList[i] = whatever
    

    That way you can access each specified element of your list by reference.

    0 讨论(0)
  • 2020-12-11 02:36

    There's nothing in python that really does what you want. Basically you want to write some sort of proxy object.

    0 讨论(0)
提交回复
热议问题