[pythonvis] Re: Sorted Funcitn

  • From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
  • To: <pythonvis@xxxxxxxxxxxxx>
  • Date: Sat, 20 Aug 2016 09:06:58 -0700

hHi John,

You are correct.

There are two dict methods for dealing with missing keys-
get returns a default value if key is missing
2 setdefault does the same, but also adds key to dict

Richard
-----Original Message----- From: John Ylioja
Sent: Friday, August 19, 2016 9:02 PM
To: pythonvis@xxxxxxxxxxxxx
Subject: [pythonvis] Re: Sorted Funcitn

Interesting. Thanks for posting this. Although the problem has already
been worked out I found this approach a good learning opportunity. This
would result in the cities being a list of strings, that could be sorted
or printed as desired. If I understand this correctly, the cities for an
entry could be printed with something like:
print ', '.join(dct[t])

and if he wanted the cities to be in alphabetical order the list could
be sorted first.
On 2016-08-16 8:04 PM, Richard Dinger wrote:

A more pythonic way to approach multi cities is to use the setdefault
method in dict.  If you have a list of temp, city pairs named data then:

dct = {}  # temp to cities dict
for t, c in data:
   dct.setdefault(t, []).append(c)

that is it nothing else.

if t is not in tct, t is added and the default is returned.   an empty
list in this case.

Recall that everything is an object, so the append method of the list
can add the new city.

if the temp is alreadyy in the dict, the current list is returned and a
city is appended.


-----Original Message----- From: Snowman
Sent: Wednesday, August 10, 2016 7:34 PM
To: pythonvis@xxxxxxxxxxxxx
Subject: [pythonvis] Re: Sorted Funcitn

Yep,  that's a reasonable approach.  That's a great how Too article.
What I chose to do in this case, was to obtain the temperature, and then
use
a Try/Except clause to see if that temperature already exists in the
dictionary, and thus has a city on it.  If it does,  I append the second
city to the item and store it back.
That actually worked better, considering how I wanted to use the data.
I want to see the temperature on the left, and all the cities that have
that
temperature on the right.
This was for my radio show, in which I read the U.S cities in order of
increasing temperature.  Nobody has ever done the weather that way.  so, I
had to do it.


----- Original Message ----- From: "John Ylioja" <john.ylioja@xxxxxxxxx>
To: <pythonvis@xxxxxxxxxxxxx>
Sent: Wednesday, August 10, 2016 6:17 PM
Subject: [pythonvis] Re: Sorted Funcitn


This comment is not directly sorting related but since dictionary keys
cannot have duplicates, you would want to make sure there would be no
duplicate temperatures or later values could overwrite earlier ones.
I'm relatively new to Python, but if there would be a possibility of
duplicates, my thought would be that another data structure may work
better, such as a list of tuples, each item containing (city,
temperature) for instance.  There's a sorting howto in the Python
documentation with examples of how to sort by different items of a
tuple or object. The one for Python 2 is at
https://docs.python.org/2/howto/sorting.html or there is one for
Python 3 depending on which version you are using.
On 2016-08-09 10:50 PM, Richard Dinger wrote:
if you add the print that I suggested it will print strings with quotes.

if the temps are strings sort like this

dctKeys = sorted(dct.keys(), key=int)


that will convert to int for comparrison during sorting
*From:* Snowman <mailto:snowman@xxxxxxxxxxxxxxxx>
*Sent:* Tuesday, August 09, 2016 2:11 PM
*To:* pythonvis@xxxxxxxxxxxxx <mailto:pythonvis@xxxxxxxxxxxxx>
*Subject:* [pythonvis] Re: Sorted Funcitn

Richard,

the key is actually a temperature value. The format of the dictionary is
"temperature" : "city".

back to the code fratment:

dctKeys = sorted(dct.keys())
# ok, I now have a list of sorted keys, but I don't knwo if they are
strings or integers.
for key in dctKeys:
# now get these keys one at a time from the sorted list of keys.

  city = dct[key] # get the city name for this temperature key
  rec = "{0}: {1}\n".format (city, key)
  fOut.write (rec)
#end loop
I think Johannes was onto it when he talks about string versus integer.
But, I'm not sure how to deal with it.

When I say:
dctKeys = sorted(dct.keys())
the variable dctKeys has not been mentioned yet.  So, it's type will be
whatever this dct.keys() method returns.
Is that a list of strings?
If I can make it a list of integers, the sort might  be more what I
want.  But, I don't know how to convert that.
But, I think that is the heart of the issue.
Any suggestions most welcome.


    ----- Original Message -----
    *From:* Richard Dinger <mailto:rrdinger@xxxxxxxxxx>
    *To:* pythonvis@xxxxxxxxxxxxx <mailto:pythonvis@xxxxxxxxxxxxx>
    *Sent:* Tuesday, August 09, 2016 3:25 AM
    *Subject:* [pythonvis] Re: Sorted Funcitn

    I am not getting my point across.  I think your keys are sorted

    aftter the line:
    dctKeys = sorted(dct.keys())

    print the keys with
    print [k for k in dctKeys]

    are they sorted?

    does the first key point to the first temperature?

    your code sorts city not temp


    *From:* Snowman <mailto:snowman@xxxxxxxxxxxxxxxx>
    *Sent:* Monday, August 08, 2016 7:54 PM
    *To:* pythonvis@xxxxxxxxxxxxx <mailto:pythonvis@xxxxxxxxxxxxx>
    *Subject:* [pythonvis] Re: Sorted Funcitn

    Richard,
    terific,  thanks for helping.  So, I have the dictionary all
    populated.  And, I want to print the results in ascending order. The
    code fragment is:

    dctKeys = sorted(dct.keys())
    for key in dctKeys:
      city = dct[key]
      rec = "{0}: {1}\n".format (city, key)
      fOut.write (rec)
    #end loop
    So, I think my loop is iterating through the list of sorted keys,
    and I expected that the iteratin would just process the list one at
    a time,  and that the sorted funciton created an ascending list to
    process.
    The key values are all 2 or 3 digit temperatures, strictly numeric.

    This generally works, except that those 3 character keys,
    those with values over 100, are turning up at the start of the list,
    ahead of the lower 2 digit keys.

    So, you see a sequence like
    100 105  23 45 67 89 etc.

    The basic question is, why is 105 less than 23 when it comes to
sorting?



        ----- Original Message -----
        *From:* Richard Dinger <mailto:rrdinger@xxxxxxxxxx>
        *To:* pythonvis@xxxxxxxxxxxxx <mailto:pythonvis@xxxxxxxxxxxxx>
        *Sent:* Monday, August 08, 2016 9:11 PM
        *Subject:* [pythonvis] Re: Sorted Funcitn

        sorry just saw your message.

        here is the problem: you are sorting the keys, but then fetching
        the item pointed to by the key and expecting the items to be
        sorted too!

        so what do you really want to do here?

        richard


        *From:* Snowman <mailto:snowman@xxxxxxxxxxxxxxxx>
        *Sent:* Wednesday, August 03, 2016 2:23 PM
        *To:* pythonvis@xxxxxxxxxxxxx <mailto:pythonvis@xxxxxxxxxxxxx>
        *Subject:* [pythonvis] Sorted Funcitn

        Hi Guys,
        I was surprised by the following, and wonder if someone can help
        me find a way around this.

        I have a dictionary, dct, and I wan to list each in the
        ascending order of it's keys.  So, I have the following code
        fragment:

        dctKeys = sorted(dct.keys())
        for key in dctKeys:
          city = dct[key]
           ... etc...

        The keys get process generally in ascending order.
        The surprise is that 3 digit keys are sorted as being less than
        the 2 digit keys.
        So, if you are looking at the keys themselvesthe following
        sequence is printed:
        100
        70
        80
        90

        Why is 100 considered less than any of these 2 digit values?

        Thanks for the help on this.




        Listen to The Snowman on MushroomFM.com, Saturday evenings, 8PM
        Eastern time.

List web page is //www.freelists.org/webpage/pythonvis

To unsubscribe, send email to pythonvis-request@xxxxxxxxxxxxx with
"unsubscribe" in the Subject field.



List web page is
//www.freelists.org/webpage/pythonvis

To unsubscribe, send email to
pythonvis-request@xxxxxxxxxxxxx with "unsubscribe" in the Subject field.
List web page is //www.freelists.org/webpage/pythonvis

To unsubscribe, send email to pythonvis-request@xxxxxxxxxxxxx with
"unsubscribe" in the Subject field.
List web page is
//www.freelists.org/webpage/pythonvis

To unsubscribe, send email to
pythonvis-request@xxxxxxxxxxxxx with "unsubscribe" in the Subject field.
List web page is //www.freelists.org/webpage/pythonvis

To unsubscribe, send email to pythonvis-request@xxxxxxxxxxxxx with "unsubscribe" in the Subject field.

Other related posts: