Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63058 > unrolled thread
| Started by | Larry Martell <larry.martell@gmail.com> |
|---|---|
| First post | 2014-01-03 10:19 -0500 |
| Last post | 2014-01-04 02:46 +1100 |
| Articles | 9 — 5 participants |
Back to article view | Back to comp.lang.python
Creating a list with holes Larry Martell <larry.martell@gmail.com> - 2014-01-03 10:19 -0500
Re: Creating a list with holes eneskristo@gmail.com - 2014-01-03 07:30 -0800
Re: Creating a list with holes Larry Martell <larry.martell@gmail.com> - 2014-01-03 10:41 -0500
Re: Creating a list with holes Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-03 18:07 +0000
Re: Creating a list with holes Larry Martell <larry.martell@gmail.com> - 2014-01-03 19:15 -0500
Re: Creating a list with holes Roy Smith <roy@panix.com> - 2014-01-03 20:18 -0500
Re: Creating a list with holes Denis McMahon <denismfmcmahon@gmail.com> - 2014-01-04 02:03 +0000
Re: Creating a list with holes Roy Smith <roy@panix.com> - 2014-01-03 10:38 -0500
Re: Creating a list with holes Chris Angelico <rosuav@gmail.com> - 2014-01-04 02:46 +1100
| From | Larry Martell <larry.martell@gmail.com> |
|---|---|
| Date | 2014-01-03 10:19 -0500 |
| Subject | Creating a list with holes |
| Message-ID | <mailman.4852.1388762356.18130.python-list@python.org> |
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 ... ) Thanks! -larry
[toc] | [next] | [standalone]
| From | eneskristo@gmail.com |
|---|---|
| Date | 2014-01-03 07:30 -0800 |
| Message-ID | <c37ad1f1-1367-4de0-9a2e-bc60355abea3@googlegroups.com> |
| In reply to | #63058 |
On Friday, January 3, 2014 4:19:09 PM UTC+1, Larry....@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 ... ) > > > > Thanks! > > -larry Hello Larry! The thing is, where to put the holes? A costum function can be made if you want the hole to be placed for example: 1. In random 2. Every nth hole(Or with another sequence) 3. In the beginning or end. Please tell me how do you want them, and I will try my best to help! - Enes
[toc] | [prev] | [next] | [standalone]
| From | Larry Martell <larry.martell@gmail.com> |
|---|---|
| Date | 2014-01-03 10:41 -0500 |
| Message-ID | <mailman.4856.1388763689.18130.python-list@python.org> |
| In reply to | #63059 |
On Fri, Jan 3, 2014 at 10:30 AM, <eneskristo@gmail.com> wrote: > On Friday, January 3, 2014 4:19:09 PM UTC+1, Larry....@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 ... ) > > Hello Larry! > > The thing is, where to put the holes? A costum function can be made if you want the hole to be placed for example: > 1. In random > 2. Every nth hole(Or with another sequence) > 3. In the beginning or end. > > Please tell me how do you want them, and I will try my best to help! The holes would be between the items I put in. In my example above, if I assigned to [10] and [20], then the other items ([0..9] and [11..19]) would have None.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-03 18:07 +0000 |
| Message-ID | <la6u8e$lf9$3@dont-email.me> |
| In reply to | #63064 |
On Fri, 03 Jan 2014 10:41:21 -0500, Larry Martell wrote:
> The holes would be between the items I put in. In my example above, if I
> assigned to [10] and [20], then the other items ([0..9] and [11..19])
> would have None.
>>> dic = { 10:6, 20:11}
>>> dic.get(10)
6
>>> dic.get(14)
>>> dic.get(27,"oh god there's nothing here")
"oh god there's nothing here"
>>> dic.get(99,None)
>>> dic.get(168,False)
False
>>> dic.get(20,"Boo Yah")
11
>>>
So a standard dictionary does this returning None (or any other default
value you care to pass it) as long as you use the dict.get(key[,default])
method rather than dict[key] to return the value.
See also:
http://stackoverflow.com/questions/6130768/return-none-if-dictionary-key-
is-not-available
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Larry Martell <larry.martell@gmail.com> |
|---|---|
| Date | 2014-01-03 19:15 -0500 |
| Message-ID | <mailman.4871.1388794533.18130.python-list@python.org> |
| In reply to | #63079 |
On Fri, Jan 3, 2014 at 1:07 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> On Fri, 03 Jan 2014 10:41:21 -0500, Larry Martell wrote:
>
>> The holes would be between the items I put in. In my example above, if I
>> assigned to [10] and [20], then the other items ([0..9] and [11..19])
>> would have None.
>
>>>> dic = { 10:6, 20:11}
>>>> dic.get(10)
> 6
>>>> dic.get(14)
>>>> dic.get(27,"oh god there's nothing here")
> "oh god there's nothing here"
>>>> dic.get(99,None)
>>>> dic.get(168,False)
> False
>>>> dic.get(20,"Boo Yah")
> 11
>>>>
>
> So a standard dictionary does this returning None (or any other default
> value you care to pass it) as long as you use the dict.get(key[,default])
> method rather than dict[key] to return the value.
>
> See also:
>
> http://stackoverflow.com/questions/6130768/return-none-if-dictionary-key-
> is-not-available
Thanks, but I know all that about dicts. I need to use a list for
compatibility with existing code.
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-01-03 20:18 -0500 |
| Message-ID | <roy-06CDBC.20180603012014@news.panix.com> |
| In reply to | #63090 |
In article <mailman.4871.1388794533.18130.python-list@python.org>, Larry Martell <larry.martell@gmail.com> wrote: > > Thanks, but I know all that about dicts. I need to use a list for > compatibility with existing code. Generalizing what I think the situation is, "A dict is the best data structure for the parsing phase, but I need a list later to hand off to legacy interfaces". No problem. Parse the data using a dict, then convert the dict to a list later. I haven't been following all the details here, but just wanted to point out that using different data structures to hold the same data at different phases of a program is a perfectly reasonable approach.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-01-04 02:03 +0000 |
| Message-ID | <la7q51$lf9$7@dont-email.me> |
| In reply to | #63093 |
On Fri, 03 Jan 2014 20:18:06 -0500, Roy Smith wrote:
> In article <mailman.4871.1388794533.18130.python-list@python.org>,
> Larry Martell <larry.martell@gmail.com> wrote:
>
>
>> Thanks, but I know all that about dicts. I need to use a list for
>> compatibility with existing code.
>
> Generalizing what I think the situation is, "A dict is the best data
> structure for the parsing phase, but I need a list later to hand off to
> legacy interfaces".
>
> No problem. Parse the data using a dict, then convert the dict to a
> list later. I haven't been following all the details here, but just
> wanted to point out that using different data structures to hold the
> same data at different phases of a program is a perfectly reasonable
> approach.
Indeed, assuming the requirement is to have a list of some length n units
representing integer keys into a range of values from start to end, and
he creates a dict initially:
list = [ dict.get(x,None) for x in range(start,end + 1) ]
Examples:
>>> dic = {1:"fred", 9:"jim", 15:"susan", 25:"albert" }
>>> l = [ dic.get(x,None) for x in range(1,20) ]
>>> l
['fred', None, None, None, None, None, None, None, 'jim', None, None,
None, None, None, 'susan', None, None, None, None]
>>> l = [ dic.get(x,None) for x in range(-10,50) ]
>>> l
[None, None, None, None, None, None, None, None, None, None, None, 'fred',
None, None, None, None, None, None, None, 'jim', None, None, None, None,
None, 'susan', None, None, None, None, None, None, None, None, None,
'albert', None, None, None, None, None, None, None, None, None, None,
None, None, None, None, None, None, None, None, None, None, None, None,
None, None]
>>> len(l)
60
then the value of l[offset] will either be None or some string depending
on the offset into the list
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-01-03 10:38 -0500 |
| Message-ID | <roy-2829D4.10383403012014@news.panix.com> |
| In reply to | #63058 |
In article <mailman.4852.1388762356.18130.python-list@python.org>, 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 Whenever you ask, "What data structure do I want", you need to be able to answer, "What operations do I want to perform?, and, "What constraints do I have on memory use?" Why do you want holes? Is the issue that you're storing sparse data and don't want to waste memory on unused keys? If so, a dictionary should do you fine. Do you need to be able to read the values back out in a specific order? You can still do that with a dictionary if you're willing to re-sort the keys; that's O(n log n) on the number of keys, but if n is small, it doesn't matter.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-04 02:46 +1100 |
| Message-ID | <mailman.4857.1388763968.18130.python-list@python.org> |
| In reply to | #63063 |
On Sat, Jan 4, 2014 at 2:38 AM, Roy Smith <roy@panix.com> wrote:
> Why do you want holes? Is the issue that you're storing sparse data and
> don't want to waste memory on unused keys? If so, a dictionary should
> do you fine.
>
> Do you need to be able to read the values back out in a specific order?
> You can still do that with a dictionary if you're willing to re-sort the
> keys; that's O(n log n) on the number of keys, but if n is small, it
> doesn't matter.
There's another factor, which is iteration time. Maybe you don't care
about the memory usage, but compare these:
foo = [None]*1000
foo[123] = 234
foo[543] = 432
for key,value in enumerate(foo):
if value: print("Slot %d is %d"%(key,value))
# versus
foo = {}
foo[123] = 234
foo[543] = 432
for key in sorted(foo.keys()):
value = foo[key]
if value: print("Slot %d is %d"%(key,value))
Which one's going to be faster? The dictionary, by far, in this
example. I'm not sure how populated the list would have to be to beat
it (as the dict will get slower on O(n log n) on keys, as you mention,
while the list will run at O(n) on the highest element, which may well
be a constant), but it's something to consider.
ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web