Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99179

Re: How To Create A Endles List Of Lists In Python...???

From srinivas devaki <mr.eightnoteight@gmail.com>
Newsgroups comp.lang.python
Subject Re: How To Create A Endles List Of Lists In Python...???
Date 2015-11-20 23:58 +0530
Message-ID <mailman.15.1448044148.2291.python-list@python.org> (permalink)
References <n2miu8$fl4$1@ls237.t-com.hr> <mailman.510.1448009596.16136.python-list@python.org> <n2mugp$kt1$1@ls237.t-com.hr> <564F1AC0.5040106@shopzeus.com> <CAPTjJmpJoktL=OdaCOw3KGukxUKBu+nW+cUFvwv47KCczAKXsg@mail.gmail.com>

Show all headers | View raw


On Fri, Nov 20, 2015 at 6:39 PM, Chris Angelico <rosuav@gmail.com> wrote:
> My crystal ball suggests that defaultdict(list) might be useful here.
>
> ChrisA

I used something similar to this for some problem in hackerrank,
anyway i think this is what you want.

class defaultlist(object):
    def __init__(self, factory, data=None):
        self.factory = factory
        self.list = []
        self.data = data

    def __getitem__(self, x):
        if x >= len(self.list):
            self.list.extend([self.factory() for _ in
range(len(self.list), x + 1)])
        return self.list[x]

    def __repr__(self):
        return str(self)

    def __str__(self):
        if len(self.list) == 0:
            return '(' + str(self.data) +  ')[...]'
        return ''.join(['(', str(self.data), ')['] + map(str,
self.list) + [', ...]'])


    def __setitem__(self, x, v):
        if x >= len(self.list):
            self.list.extend([self.factory() for _ in
range(len(self.list), x + 1)])
        self.list[x] = v

def main():
    factory = lambda: defaultlist(factory)
    list_of_lists = defaultlist(factory)
    print (list_of_lists[0])
    list_of_lists[0][10].data = 20
    print (list_of_lists[0])


main()

Gist: https://gist.github.com/c0c2ee1e7c6535ef8c3d

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How To Create A Endles List Of Lists In Python...??? "HKRSS" <hkrss@gmail.com> - 2015-11-20 08:43 +0100
  Re: How To Create A Endles List Of Lists In Python...??? "HKRSS" <hkrss@gmail.com> - 2015-11-20 09:13 +0100
  Re: How To Create A Endles List Of Lists In Python...??? Peter Otten <__peter__@web.de> - 2015-11-20 09:53 +0100
    Re: How To Create A Endles List Of Lists In Python...??? "HKRSS" <hkrss@gmail.com> - 2015-11-20 12:00 +0100
      Re: How To Create A Endles List Of Lists In Python...??? Nagy László Zsolt <gandalf@shopzeus.com> - 2015-11-20 14:06 +0100
        Re: How To Create A Endles List Of Lists In Python...??? robert.bralic@si.t-com.hr - 2015-11-20 05:29 -0800
      Re: How To Create A Endles List Of Lists In Python...??? Chris Angelico <rosuav@gmail.com> - 2015-11-21 00:09 +1100
      Re: How To Create A Endles List Of Lists In Python...??? Grant Edwards <invalid@invalid.invalid> - 2015-11-20 15:58 +0000
      Re: How To Create A Endles List Of Lists In Python...??? srinivas devaki <mr.eightnoteight@gmail.com> - 2015-11-20 23:58 +0530
      Re: How To Create A Endles List Of Lists In Python...??? srinivas devaki <mr.eightnoteight@gmail.com> - 2015-11-21 00:05 +0530
  Re: How To Create A Endles List Of Lists In Python...??? Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-20 17:14 +0000
    Re: How To Create A Endles List Of Lists In Python...??? robert.bralic@si.t-com.hr - 2015-11-20 10:16 -0800
      Re: How To Create A Endles List Of Lists In Python...??? Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-20 11:29 -0700
      Re: How To Create A Endles List Of Lists In Python...??? Chris Angelico <rosuav@gmail.com> - 2015-11-21 05:30 +1100
      Re: How To Create A Endles List Of Lists In Python...??? sohcahtoa82@gmail.com - 2015-11-20 11:26 -0800

csiph-web