Re: [Tutor] Why is an OrderedDict not sliceable?

2016-01-21 Thread Albert-Jan Roskam


> Date: Thu, 21 Jan 2016 12:00:29 +1100
> From: st...@pearwood.info
> To: tutor@python.org
> Subject: Re: [Tutor] Why is an OrderedDict not sliceable?
> 
> On Wed, Jan 20, 2016 at 01:33:17PM +, Albert-Jan Roskam wrote:
> > Hi,
> > 
> > Like the subject says: Why is an OrderedDict not sliceable? (From the 
> > collections library). Was that an intentional omission, or a mistake? 
> > [1]
> 
> Because slicing a dict makes no sense. A dict is a mapping, not a 
> sequence.

For a regular dict: yes, I agree that doesn't make sense, because a regular 
dict is unordered.
A collections.OrderedDict on the other hand..

> d = OrderedDict()
> d["cow"] = "a"
> d["fox"] = "b"
> d["ape"] = "c"
> d["dog"] = "d"
> d["bee"] = "e"
> 
> I can do a dict lookup on a key:
> 
> d["cow"]
> 
> What would a slice even mean? d[1:4] but 1, 2, 3 are not keys of the 
> dict.

Indexing would create ambiguity, e.g d[1] might mean
* return the value associated with key 1
* return the first value

But that's not true for slices, because they cannot be dictionary keys anyway:
>>> {slice(1, 2): None}
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unhashable type

With islice it is easy to do it anyway, but I still find it strange that 
OrderedDict.__getitem__ does not do that already (sorry!)
>>> from itertools import islice
>>> from collections import OrderedDict
>>> od = OrderedDict({"a": 1, "b": 2})
>>> list(islice(od, 1, 2))
['b']


  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is an OrderedDict not sliceable?

2016-01-21 Thread Ben Finney
Albert-Jan Roskam  writes:

> Why is an OrderedDict not sliceable?

Because slicing implies index access. The built-in ‘dict’ and
‘collections.OrderedDict’ both do not support indexed access::

>>> import collections
>>> foo = collections.OrderedDict([
... ('a', 1), ('b', 2), ('c', 3), ('d', 4)])
>>> foo[3]
Traceback (most recent call last):
  File "", line 1, in 
KeyError: 3

>>> bar = dict([
... ('a', 1), ('b', 2), ('c', 3), ('d', 4)])
>>> bar[3]
Traceback (most recent call last):
  File "", line 1, in 
KeyError: 3

Index access with ‘foo[index]’ syntax, and slicing with
‘foo[start_index:stop_index:step]’ syntax, collide with the existing
key-access meaning of ‘foo[key]’ for a mapping.

-- 
 \  “[I]t is impossible for anyone to begin to learn that which he |
  `\thinks he already knows.” —Epictetus, _Discourses_ |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is the name "self" optional instead of mandatory?

2016-01-21 Thread DiliupG
The answer to this will be an interesting read... :)

This
email has been sent from a virus-free computer protected by Avast.
www.avast.com

<#DDB4FAA8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Thu, Jan 21, 2016 at 11:10 AM, Cameron Simpson  wrote:

> On 20Jan2016 21:42, boB Stepp  wrote:
>
>> I'm whizzing along in "Python Crash Course" and am in the chapter on
>> classes.  Just to satisfy my suspicion that "self" is just a
>> placeholder for creating an object instance,
>>
>
> No, it is a placeholder for a _preexiting_ object instance.
>
> [...]
>
>> So I really only have one question:  Why not make Python's
>> *traditional* name, "self", mandatory?  Why give the programmer this
>> kind of choice?  [OK, that was two questions.]
>>
>
> Why make it mandatory? What benefit would it bring? Remember, one can
> write nonsense or impossible to read gibberish in any language; most people
> don't try to. So to repeat my question: why make it mandatory?
>
> Cheers,
> Cameron Simpson 
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Extract several arrays from a large 2D array

2016-01-21 Thread Ek Esawi
Hi All--



I have a 2D array (2000, 4); an example is given abelow.  On the 1st column
I have 15 variables, on the 2nd 4 variables. Ignore column 3 for now. I
want a code that generate 4 arrays for each variable on the 1st column;
each array contains all the values from column 4. Then I want to find the
average of each array.



For example; pick the 1st variable in column 1 which is 1; then pick the 1st
variable on the 2nd column which is 5. Now I want an array that contains
all the values on the 4th column that match variable 1 on the 1st and
variable 5 on the 2nd column. I need to get the average of each array.





A b c  d

1  5  3  4

1  3  2  7

2  5  7  5

3  2  8  5

2  3  2  3


Thanks in advance—EKE
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is the name "self" optional instead of mandatory?

2016-01-21 Thread Alan Gauld
On 21/01/16 03:42, boB Stepp wrote:

> So I really only have one question:  Why not make Python's
> *traditional* name, "self", mandatory?  Why give the programmer this
> kind of choice?  [OK, that was two questions.]

Because to do otherwise would introduce all sorts of extra
complexity into the standard function calling mechanism.
There is nothing super special about methods they are just
functions. You can call the parameters of a function anything
you like. To suddenly have the interpreter check the name
of the first positional parameter for the specific case
of a method makes no sense.

And it's very convenient to allow other names. When I'm
playing at the >>> prompt I often just substitute s for self
to save typing. When sharing Python code with C++ or Java
programmers I often use 'this' instead because they will
understand that.

Python gives us flexibility which we are expected to use
responsibly. It's a good thing.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extract several arrays from a large 2D array

2016-01-21 Thread Alan Gauld
On 21/01/16 04:22, Ek Esawi wrote:

> I have a 2D array (2000, 4); an example is given abelow.  On the 1st column
> I have 15 variables, on the 2nd 4 variables. 

Your terminology is a little confusing since variables in
Python normally means names. You have values in your columns.
So I suspect when you say variables you mean distinct values?

> want a code that generate 4 arrays for each variable on the 1st column;
> each array contains all the values from column 4. Then I want to find the
> average of each array.

This is very different to what you describe later in your example.

> For example; pick the 1st variable in column 1 which is 1; then pick the 1st
> variable on the 2nd column which is 5. Now I want an array that contains
> all the values on the 4th column that match variable 1 on the 1st and
> variable 5 on the 2nd column. I need to get the average of each array.

In this particular case you are describing a tuple key (colA,colB)
There may be more efficient solutions, but I would create a dictionary
using the tuple as key and appending the values on colD to a list.
You can then take the average of the list for each tuple.

So in pseudo code:

data = {}
for row in myArray:
data.get((row[0],row[1]),[]).append row[3]

for key in data:
print key,':',sum(data[key]/len(data[key])



HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extract several arrays from a large 2D array

2016-01-21 Thread Oscar Benjamin
On 21 January 2016 at 04:22, Ek Esawi  wrote:
> I have a 2D array (2000, 4);

Do you mean a numpy array? I'm going to assume that you do.

> an example is given abelow.  On the 1st column
> I have 15 variables, on the 2nd 4 variables. Ignore column 3 for now. I
> want a code that generate 4 arrays for each variable on the 1st column;
> each array contains all the values from column 4. Then I want to find the
> average of each array.
>
> For example; pick the 1st variable in column 1 which is 1; then pick the 1st
> variable on the 2nd column which is 5. Now I want an array that contains
> all the values on the 4th column that match variable 1 on the 1st and
> variable 5 on the 2nd column. I need to get the average of each array.
>
> A b c  d
>
> 1  5  3  4
>
> 1  3  2  7
>
> 2  5  7  5
>
> 3  2  8  5
>
> 2  3  2  3

I'll generate a numpy array with this data:

In [1]: import numpy as np

In [2]: M = np.array([[1,5,3,4],[1,3,2,7],[2,5,7,5],[3,2,8,5],[2,3,2,3]])

In [3]: M
Out[3]:
array([[1, 5, 3, 4],
   [1, 3, 2, 7],
   [2, 5, 7, 5],
   [3, 2, 8, 5],
   [2, 3, 2, 3]])

First we want to extract the rows where 1st and 2nd columns have 1 and
5 respectively:

In [5]: M[(M[:, 0] == 1) & (M[:, 1] == 5), :]
Out[5]: array([[1, 5, 3, 4]])

Then we want to get only the 4th column of that:

In [7]: M[(M[:, 0] == 1) & (M[:, 1] == 5), 3]
Out[7]: array([4])

And now we want the mean of that:

In [8]: M[(M[:, 0] == 1) & (M[:, 1] == 5), 3].mean()
Out[8]: 4.0

--
Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extract several arrays from a large 2D array

2016-01-21 Thread Peter Otten
Ek Esawi wrote:

> Hi All--

> I have a 2D array (2000, 4); an example is given abelow.  On the 1st
> column I have 15 variables, on the 2nd 4 variables. Ignore column 3 for
> now. I want a code that generate 4 arrays for each variable on the 1st
> column; each array contains all the values from column 4. Then I want to
> find the average of each array.

> For example; pick the 1st variable in column 1 which is 1; then pick the
> 1st variable on the 2nd column which is 5. Now I want an array that
> contains all the values on the 4th column that match variable 1 on the 1st
> and variable 5 on the 2nd column. I need to get the average of each array.
> 
> 
> A b c  d
> 
> 1  5  3  4
> 
> 1  3  2  7
> 
> 2  5  7  5
> 
> 3  2  8  5
> 
> 2  3  2  3

With pandas:

>>> df = pandas.DataFrame([
... [1, 5, 3, 4],
... [1, 5, 2, 2],
... [2, 5, 1, 2],
... [2, 3, 1, 2]],
... columns=["a", "b", "c", "d"])
>>> df.groupby(("a", "b"))["d"].mean()
a  b
1  53
2  32
   52
Name: d, dtype: int64


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extract several arrays from a large 2D array

2016-01-21 Thread Peter Otten
Alan Gauld wrote:

> So in pseudo code:
> 
> data = {}
> for row in myArray:
> data.get((row[0],row[1]),[]).append row[3]

Replace get() with setdefault() to go from pseudo to Python code.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why do I not get an error when I mistakenly type "humdrum.sigh_strenght" instead of the correct "humdrum.sigh_strength"?

2016-01-21 Thread Steven D'Aprano
On Wed, Jan 20, 2016 at 09:54:32PM -0800, Danny Yoo wrote:

> Just to be explicit: you are pointing out that:
> 
>  humdrum.sigh_strenght = 'high'
> 
> was a typo, and it would have been nice if it could be caught as an error.
> 
> If that's the case, then yes, I agree.  Unfortunately, this isn't a
> runtime error because Python allows us to add arbitrary attributes to
> objects: it's a deliberate design feature.

Danny is correct. And it is a useful feature too. For instance, we can 
add attributes to functions:

def spam(x, y):
...

spam.extra_info = "whatever"

Other advantages of this way of doing things include that you aren't 
forced to put all your initialisation code in a single, special place, 
you can factor parts of it out into alternative methods:

class X:
   def __init__(self, x):
   self.setup(x)
   def setup(self, x):
   process(x)
   self.attribute = x


It also means that Python doesn't require a magic "undefined" value, 
like Javascript has, or require declarations ahead of time, like static 
languages such as Pascal and C require.

However, this power does have a cost, as Danny points out:

> (As a personal note: I'm not entirely convinced of the merit of this
> particular freedom.  Misspelling is a really easy mistake to make, and
> I want the language to catch my simple mistakes, if it's not too
> onerous to do so.)

True, but I think that on average, the freedom and power of not needing 
declarations far outweighs the cost of dealing with typos. But if you 
type with your elbows and nose, you may disagree with me *wink*


[...]
> Furthermore, there actually *is* a way to turn it into a runtime
> error, though this is probably not beginner material.  If you're
> interested, see material on "__slots__".
> https://docs.python.org/3/reference/datamodel.html#slots.  I don't
> think it's intended to be used to catch typos though: its main purpose
> is to reduce memory usage, and it's use to catch misspellings is
> incidental.

Agreed. __slots__ is certainly not intended as a way of catching 
misspellings, or prohibiting the addition of new attributes. But if you 
don't mind the scorn of your fellow coders, you can use it for that 
purpose.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is an OrderedDict not sliceable?

2016-01-21 Thread Oscar Benjamin
On 21 January 2016 at 09:19, Ben Finney  wrote:
> Albert-Jan Roskam  writes:
>
>> Why is an OrderedDict not sliceable?
>
> Because slicing implies index access. The built-in ‘dict’ and
> ‘collections.OrderedDict’ both do not support indexed access::

According to a narrow definition of indexed access. I would say that
d[k] is index access even if d is a dict and k a key.

Albert-Jan I guess what you want is this:

from collections import OrderedDict

class SliceableOrderedDict(OrderedDict):
def __getitem__(self, sl):
if isinstance(sl, slice):
keys = list(self)
keyslice = slice(
None if sl.start is None else keys.index(sl.start),
None if sl.stop is None else keys.index(sl.stop),
sl.step
)
newitems = ((k, self[k]) for k in keys[keyslice])
return SliceableOrderedDict(newitems)
else:
return super().__getitem__(sl)

I guess that the authors of OrderedDict just didn't really consider
this to be very useful. Apart from having ordered iteration
OrderedDict is not really that deeply thought out. There's a thread on
python-ideas about the inconsistent behaviour of the keys and values
views and equality comparison of OrderedDicts on python-ideas at the
moment:

https://mail.python.org/pipermail/python-ideas/2015-December/037472.html

--
Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is the name "self" optional instead of mandatory?

2016-01-21 Thread Steven D'Aprano
On Wed, Jan 20, 2016 at 09:42:29PM -0600, boB Stepp wrote:

> So I really only have one question:  Why not make Python's
> *traditional* name, "self", mandatory?  Why give the programmer this
> kind of choice?  [OK, that was two questions.]

Why bother making it mandatory? That just makes more work for the 
compiler -- it has to decide that a function is inside a class, and 
therefore apply a restriction to the first argument. Most of the time, 
giving the programmer more freedom is less work.

And what happens if you do this?

class X:
pass

def func(this):
print("instance %r called method" % this)

X.method = func

And what are we supposed to do with classmethods and staticmethods? They 
shouldn't take a "self" argument at all, but they start off life as a 
regular function, just like ordinary instance methods.

And of course, there are Metaclasses. You might need a metaclass method 
that needs to deal with all three levels of the hierarchy: the 
metaclass, the class it creates, and the instance of that class.

class Meta(type):
def metamethod(meta, cls, self):
...



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is the name "self" optional instead of mandatory?

2016-01-21 Thread Francois Dion
On Thu, Jan 21, 2016 at 6:49 AM, Steven D'Aprano 
 wrote:

> On Wed, Jan 20, 2016 at 09:42:29PM -0600, boB Stepp wrote:
>
> > So I really only have one question:  Why not make Python's
> > *traditional* name, "self", mandatory?  Why give the programmer this
> > kind of choice?  [OK, that was two questions.]


[answers ommited]

All great answers. Ben mentioned the four space tab convention as a similar
thing:  Python has a ton of conventions but doesn't enforce them. That is
not the job of the interpreter, at least the Python one, since it has never
enforced any convention.

Yes, individual programmers can enforce these conventions in whichever way
they want. Some see this as a bad thing for coding standards, but that is
easily addressed by tools like pylint. In an enterprise setting, the
pipeline of going from writing code to deploying it will go through a few
phases, including checking for PEP8 compliance, Pylint compliance (the
rules the team cares about), code complexity etc. With pylint, if you try
it on your code with this instead of self, you'll get this in particular:
"Method should have "self" as first argument (no-self-argument)"

Francois


-- 
raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info -
@f_dion
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is an OrderedDict not sliceable?

2016-01-21 Thread Ben Finney
Oscar Benjamin  writes:

> According to a narrow definition of indexed access. I would say that
> d[k] is index access even if d is a dict and k a key.

An index implies the ordinal position in a sequence. In a mapping, the
key is *not* referring to the position in a sequence, so is not a key.

So accessing an item in a mapping by key is not indexed access.

-- 
 \ “When we pray to God we must be seeking nothing — nothing.” |
  `\—Francis of Assisi |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] How to call the path of an input file

2016-01-21 Thread John Villarraga
I would like to know what is the syntax to call the path of the input file.
Below, my code is calling the input file, but not the path.

Sorry for the inconvenience and thank you for your time.


import sys


path = sys.argv[1]


y = map(str.lower, path.split())
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is an OrderedDict not sliceable?

2016-01-21 Thread Ben Finney
Ben Finney  writes:

> Oscar Benjamin  writes:
>
> > According to a narrow definition of indexed access. I would say that
> > d[k] is index access even if d is a dict and k a key.

The sense of “index” implied is used consistently throughout Python
https://docs.python.org/3/glossary.html> to refer to the integer
ordinal position in a sequence.

It is not compatible with key access into a mapping.

> An index implies the ordinal position in a sequence. In a mapping, the
> key is *not* referring to the position in a sequence, so is not a key.

“the key … is not an index”, I mean.

> So accessing an item in a mapping by key is not indexed access.

-- 
 \ “Facts do not cease to exist because they are ignored.” —Aldous |
  `\Huxley |
_o__)  |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to call the path of an input file

2016-01-21 Thread Martin A. Brown

Greetings John,

>I would like to know what is the syntax to call the path of the 
>input file. Below, my code is calling the input file, but not the 
>path.

>import sys
>path = sys.argv[1]

Are you looking for os.path.abspath() [0]?

  import os
  import sys
  path = os.path.abspath(sys.argv[1])

>y = map(str.lower, path.split())

Next question:  What exactly are you trying to do with that third 
line?  It looks confused.

Good luck,

-Martin

 [0] https://docs.python.org/3/library/os.path.html#os.path.abspath

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to call the path of an input file

2016-01-21 Thread Ben Finney
John Villarraga  writes:

> I would like to know what is the syntax to call the path of the input
> file.

It's not clear to me what “call the path of the input file” would mean.

A filesystem path is not “callable” in a programming as you seem to be
using it.

> Below, my code is calling the input file, but not the path.

Can you please ensure your messages are plain text (not re-formatted or
marked up), to help your program code survive unmolested.

-- 
 \ “Men never do evil so completely and cheerfully as when they do |
  `\it from religious conviction.” —Blaise Pascal (1623–1662), |
_o__)   Pensées, #894. |
Ben Finney

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to call the path of an input file

2016-01-21 Thread Steven D'Aprano
Hi John, and welcome. My responses interleaved between yours below.


On Thu, Jan 21, 2016 at 11:51:09AM -0500, John Villarraga wrote:

> I would like to know what is the syntax to call the path of the input file.

Python only has one syntax for calling anything, and that is to put 
parentheses (round brackets) after it, with any arguments needed inside 
the parens. So this is how you would call the path:

path()

But that's not going to do you any good, since path is surely going to 
be a string, and strings aren't callable: you'll just get a TypeError:


py> path = "directory/file.txt"
py> path()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'str' object is not callable


So would you like to clarify what you actually mean by "call the path"?



> Below, my code is calling the input file, but not the path.
> 
> Sorry for the inconvenience and thank you for your time.
> 
> 
> import sys
> path = sys.argv[1]
> y = map(str.lower, path.split())


I *think* what you mean is that you have a python script, let's call it 
"script.py", and you run that script like this:

python script.py


and now you want to add a path (a path to what? a file?) as a command 
line argument:

python script.py /some/directory/file.txt


And then what is supposed to happen?

I'm going to guess that you want to take the argument given:

path = "/some/directory/file.txt"

and do something to it, not sure what. Perhaps normalise the case?


import os
path = os.path.normcase(path)


should do what you want. You can read up on the functions available in 
the os.path module here:

For version 2:

https://docs.python.org/2/library/os.path.html


For version 3:

https://docs.python.org/3/library/os.path.html


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is an OrderedDict not sliceable?

2016-01-21 Thread Steven D'Aprano
Further thoughts on your question...


On Wed, Jan 20, 2016 at 01:33:17PM +, Albert-Jan Roskam wrote:
> Hi,
> 
> Like the subject says: Why is an OrderedDict not sliceable? (From the 
> collections library). Was that an intentional omission, or a mistake? 
> [1]
> 
> Background: I do not use OrderedDict very often, but I thought I could 
> use it to look up street and city names using postcodes ([0-9]{4} 
> [a-z]{2} format). I needed it to be ordered because I also wanted to 
> be able to use bisect, which is needed when the postcode letters are 
> missing. In short: a fast dict lookup for complete postcodes and less 
> fast bisect lookup for in complete postcodes.

I'm not sure I understand your use-case here.

You have postcodes that look like this:

"1234az"

Correct? Why do you want them *ordered*? 

I think you are confusing OrderedDict for a "Sorted Dict". OrderedDict 
doesn't keep the keys in sorted order, it keeps them in the order that 
they were inserted. So unless you are super-careful to insert the 
postcodes in sorted order, the order of them in the dict will be 
whatever order you insert them:

py> from collections import OrderedDict
py> d = OrderedDict()
py> d['1234az'] = "1 Smith Street"
py> d['zz'] = "991203 Short Street"
py> d['3456mx'] = "24 Hour Lane"
py> for key in d:
... print(key, d[key])
...
1234az 1 Smith Street
zz 991203 Short Street
3456mx 24 Hour Lane


So even if OrderedDict supported slicing, that would not do what you 
think it does.


Also, you have a problem -- what happens if the incomplete postcode is 
missing the first digit? (I suppose for that case, you just have to do a 
slow linear search.) What about transposed digits or other errors? I'm 
glad I don't have to solve that problem!


Anyway, I suggest doing something like this:

(1) Keep the known postcodes in a regular dict, not an ordered dict.

(2) Once you have built the dict, then copy the keys and sort them:

postcodes = {
'1234az': "1 Smith Street",
'zz': "991203 Short Street",
'3456mx': "24 Hour Lane",
}

array = sorted(postcodes.keys())


(3) Each time you add a new postcode to the dict, use bisect to add it 
to the array as well. To ensure you never forget, use a helper function:

def insert(postcode, entry):
if postcode in postcodes:
# deal with duplicate/existing key
...
else:
postcodes[postcode] = entry
bisect.insort(array, postcode)


Same for deleting.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why is an OrderedDict not sliceable?

2016-01-21 Thread Alan Gauld
On 22/01/16 00:00, Steven D'Aprano wrote:

> Also, you have a problem -- what happens if the incomplete postcode is 
> missing the first digit? (I suppose for that case, you just have to do a 
> slow linear search.) 

Which is why a different solution may be better suited.
What about an in-memory SQLite table? Then you can use
a LIKE based select and let the library do the hard work.

Just a thought.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor