Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63062 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2014-01-04 02:37 +1100 |
| Last post | 2014-01-04 02:57 +1100 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Creating a list with holes Chris Angelico <rosuav@gmail.com> - 2014-01-04 02:37 +1100
Re: Creating a list with holes Roy Smith <roy@panix.com> - 2014-01-03 10:51 -0500
Re: Creating a list with holes Chris Angelico <rosuav@gmail.com> - 2014-01-04 02:57 +1100
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-04 02:37 +1100 |
| Subject | Re: Creating a list with holes |
| Message-ID | <mailman.4853.1388763434.18130.python-list@python.org> |
On Sat, Jan 4, 2014 at 2:19 AM, Larry Martell <larry.martell@gmail.com> wrote: > I think I know the answer is no, but is there any package that allows > creating a list with holes in it? E.g. I'd want to do something like: > > x[10] = 12 > x[20] = 30 > > I'm thinking of something like defaultdict but for lists (I know > that's very different, but ... ) Depending on what exactly you need, it's probably worth just using a dict. In what ways do you need it to function as a list? You can always iterate over sorted(some_dict.keys()) if you need to run through them in order. Alternatively, if you expect to fill in most of the elements, it's possible you'd be happier working with a subclass of list that auto-expands by filling in the spare space with a singleton meaning "no element here". The code probably exists somewhere, but if not, it wouldn't be hard to write. Then it'd be a list, but you can start with it empty and assign as you describe above. What's the use case? I expect that one or the other of those options would cover most cases, but maybe yours is different. ChrisA
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-01-03 10:51 -0500 |
| Message-ID | <roy-6F59F6.10514703012014@news.panix.com> |
| In reply to | #63062 |
In article <mailman.4853.1388763434.18130.python-list@python.org>, Chris Angelico <rosuav@gmail.com> wrote: > Alternatively, if you expect to fill in most of the elements, it's > possible you'd be happier working with a subclass of list that > auto-expands by filling in the spare space with a singleton meaning > "no element here". And, if you know ahead of time the maximum number of elements you will ever have: x = [None] * max_count will preallocate them all with a minimum of fuss.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-04 02:57 +1100 |
| Message-ID | <mailman.4859.1388764686.18130.python-list@python.org> |
| In reply to | #63068 |
On Sat, Jan 4, 2014 at 2:51 AM, Roy Smith <roy@panix.com> wrote: > In article <mailman.4853.1388763434.18130.python-list@python.org>, > Chris Angelico <rosuav@gmail.com> wrote: > >> Alternatively, if you expect to fill in most of the elements, it's >> possible you'd be happier working with a subclass of list that >> auto-expands by filling in the spare space with a singleton meaning >> "no element here". > > And, if you know ahead of time the maximum number of elements you will > ever have: > > x = [None] * max_count > > will preallocate them all with a minimum of fuss. Yes, as I use in the trivial example in the subsequent post. But as a general solution this is usually insufficient. (Whether or not None is valid as a sentinel is, of course, quite orthogonal to the discussion. I avoided assuming that it was, the OP's now shown that it does seem to be.) ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web