473,714 Members | 3,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about sort and dictionary

Got confused by the following code:
a [6, 3, 1] b [4, 3, 1] c {1: [[6, 3, 1], [4, 3, 1]], 2: [[6, 3, 1]]} c[2].append(b.sort( ))
c {1: [[6, 3, 1], [1, 3, 4]], 2: [[6, 3, 1], None]}
#why c can not append the sorted b?? b.sort()
b

[1, 3, 4]
Nov 22 '05
99 4644

Fredrik Lundh wrote:
bo****@gmail.co m wrote:
so what would an entry-level Python programmer expect from this
piece of code?

for item in a.reverse():
print item
for item in a.reverse():
print item

I would expect it to first print a in reverse then a as it was.

a=[1,2,3]

I expect it to print

3
2
1
1
2
3


really? wouldn't

3
2
1
3
2
1

make a lot more sense ?

I have no idea. That is my expectation. I don't know yours.

My interpretation of it is :

a got reversed then I consume it one by one
a got reversed again then I consume it one by one

Because I expect a being a mutable object, anything works on it(not
just object method, but even other functions) is by default has side
effect, unless otherwise stated, like copy/deepcopy.

Nov 22 '05 #11
bo****@gmail.co m wrote:
so what would an entry-level Python programmer expect from this
piece of code?

for item in a.reverse():
print item
for item in a.reverse():
print item

I would expect it to first print a in reverse then a as it was.

a=[1,2,3]

I expect it to print

3
2
1
1
2
3

really? wouldn't

3
2
1
3
2
1

make a lot more sense ?

</F>

Nov 22 '05 #12

Fredrik Lundh wrote:
bo****@gmail.co m wrote:
so what would an entry-level Python programmer expect from this
piece of code?

for item in a.reverse():
print item
for item in a.reverse():
print item

I would expect it to first print a in reverse then a as it was.

a=[1,2,3]

I expect it to print

3
2
1
1
2
3

really? wouldn't

3
2
1
3
2
1

make a lot more sense ?

Still don't see why even you ask it again. May be you can enlight me a
bit. If this is Haskell, I would expect the result you posted.

Nov 22 '05 #13
> Still don't see why even you ask it again.

fyi, I'm not " fr*****@pythonw are-dot-com.no-spam.invalid ", and I've
never, as far I know, posted from "readfreenews.n et"

</F>

Nov 22 '05 #14

Fredrik Lundh wrote:
Still don't see why even you ask it again.


fyi, I'm not " fr*****@pythonw are-dot-com.no-spam.invalid ", and I've
never, as far I know, posted from "readfreenews.n et"

I have no idea what you are talking about. I read this list through
Google's group and I saw two of the same post. Google unfortunately
just should your name "Fredrik Lundh", may be something went wrong with
the service.

Nov 22 '05 #15
Fredrik Lundh wrote:
bo****@gmail.co m wrote:
so what would an entry-level Python programmer expect from this
piece of code?

for item in a.reverse():
print item
for item in a.reverse():
print item

I would expect it to first print a in reverse then a as it was.

a=[1,2,3]

I expect it to print

3
2
1
1
2
3

really? wouldn't

3
2
1
3
2
1

make a lot more sense ?


I am not a complete newb at python, but I am still pretty new.
I too thought immediately that the output should be 3,2,1, 1,2,3.
I used reverse() and sort() a couple time and of course read
the docs before I did. I noted they do the change inplace, and
don't find rememering that to be a terrible burden. Actually, I
get rather annoyed by the comment that they return None "as
a reminder" that the change is inplace. How arrogant! While
I'm sure the designers had kindly intentions. my memory, though
bad, is not that bad, and I object to being forced to write code
that is more clunky than need be, because the designers thought
they needed to help me with my memory.

Nov 22 '05 #16
Fredrik Lundh wrote:
bo****@gmail.co m wrote:
> so what would an entry-level Python programmer expect from this
> piece of code?
>
> for item in a.reverse():
> print item
> for item in a.reverse():
> print item
>

I would expect it to first print a in reverse then a as it was.

a=[1,2,3]

I expect it to print

3
2
1
1
2
3


really? wouldn't

3
2
1
3
2
1

make a lot more sense ?


Yes. The unintuitive thing is that the list is sorted in place at
all.

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
Nov 22 '05 #17
On Tue, 22 Nov 2005 08:53:07 -0800, rurpy wrote:
I am not a complete newb at python, but I am still pretty new.
I too thought immediately that the output should be 3,2,1, 1,2,3.
What you are saying is that a.reverse() should *both* change a in place
*and* return a reference to the same list.

I used reverse() and sort() a couple time and of course read
the docs before I did. I noted they do the change inplace, and
don't find rememering that to be a terrible burden. Actually, I
get rather annoyed by the comment that they return None "as
a reminder" that the change is inplace. How arrogant! While
I'm sure the designers had kindly intentions. my memory, though
bad, is not that bad, and I object to being forced to write code
that is more clunky than need be, because the designers thought
they needed to help me with my memory.


Built-in methods with side-effects (sort, reverse, update, clear, etc.)
return None because every function must return something, not because it
is a reminder. Python is not Pascal, and there are no procedures.

There are four possibilities for a construction like list.sort():

(1) sort the list in place and return a reference to the same list;
(2) sort the list in place and return a copy of the same list;
(3) sort the list in place and return None;
(4) don't sort in place and return a sorted list.

No solution is always right, no solution is always wrong, but the most
flexible is a combination of (3) and (4). Python now has that with sort()
and sorted(). Prior to the addition of sorted() to the language, (3) was
considered the best solution because of a simple Python principle: never
duplicate objects unless explicitly told to.
--
Steven.

Nov 22 '05 #18

Steven D'Aprano wrote:
There are four possibilities for a construction like list.sort():

(1) sort the list in place and return a reference to the same list;
(2) sort the list in place and return a copy of the same list;
(3) sort the list in place and return None;
(4) don't sort in place and return a sorted list.

No solution is always right, no solution is always wrong, but the most
flexible is a combination of (3) and (4). Python now has that with sort()
and sorted(). Prior to the addition of sorted() to the language, (3) was
considered the best solution because of a simple Python principle: never
duplicate objects unless explicitly told to.

I don't see the reason that (3) and (4) are the most flexible.

Again, "never duplicate objects unless explicitly told to" combined
with "=" is name binding gives me a very strong message that
list.sort() it will change things in place and which is why it is quite
natural(for me at least)

3
2
1
1
2
3

for this language. Wether it is "best" or make more sense doesn't
really matter to me, though I am curious to know why.

But basically, I just use the language as it is, and the way I want to.
So long it solves my problem and gives me the result I want.

Nov 22 '05 #19

OKB (not okblacke) wrote:
Fredrik Lundh wrote:
bo****@gmail.co m wrote:
> so what would an entry-level Python programmer expect from this
> piece of code?
>
> for item in a.reverse():
> print item
> for item in a.reverse():
> print item
>
I would expect it to first print a in reverse then a as it was.

a=[1,2,3]

I expect it to print

3
2
1
1
2
3


really? wouldn't

3
2
1
3
2
1

make a lot more sense ?


Yes. The unintuitive thing is that the list is sorted in place at
all.

intuitive seems to be a very subjective matter, depends on once
background etc :-)

Nov 22 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1708
by: Raymond Arthur St. Marie II of III | last post by:
very Very VERY dumb ? about the new Set( ) 's Please be kind and read this like you know I've been up 33-34 hours reading PEP's but... Doc\ref 2.6 Delimiters show's three unused characters "@ $ ?". @ sort of looks like and sort of sounds like a set an $ well sort of obvious. I can imagine that the $ would be confused for money and @ is ugly.
39
3162
by: Marco Aschwanden | last post by:
Hi I don't have to talk about the beauty of Python and its clear and readable syntax... but there are a few things that striked me while learning Python. I have collected those thoughts. I am sure there are many discussions on the "problems" mentioned here. But I had this thoughts without looking into any forums or anything... it is kind of feedback.
22
1756
by: lechequier | last post by:
Let's say I define a list of pairs as follows: >>l = Can anyone explain why this does not work? >>h = {}.update(l) and instead I have to go: >>h = {} >>h.update(l) to initialize a dictionary with the given list of pairs?
90
3431
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
3
1289
by: erik gartz | last post by:
Hello, I'm new to python and I'm having difficulty understanding the following code. Why doesn't the variable a contain , ] instead. Doesn't {} allocate new memory for the dictionary each time? It almost appears as if the 2nd dictionary created overwrites the first one. Thanks for your help, Erik , ] a = {'x':i} , ]
1
2600
by: hn.ft.pris | last post by:
"Forward maximum match" mean that there is a dictionary contains thousands of items which are single words. For example, "kid", "nice", "best"... And here get a string "kidnicebs", the forward maximum match algorithm need to find the longest string that matches the string in dictionary. Because the longest string in the dictionary has a length of 4, so the algoritm pick the first four characters of the string(that's why it's called...
5
2608
by: Neil Chambers | last post by:
Hi All, I'm looking to see if it's feasible to use the SortObjectCommand included in the Microsoft.Powershell.Commands assembly. I have a Dictionary Dictionary<string, intd = new Dictionary<string, int>(); d = new int;
2
2841
by: kdt | last post by:
Hi, I need to perform some horrible functions in python I need to do, using sort in a similar way that Excel can. With a dictionary like: >>> d {8: (99, 99), 9: , 4: , 5: (67, 77)} I want to sort the entire dictionary based on the last values in each line. First for and then.
37
2183
by: Hilton | last post by:
Hi, for (int i = 0; i < list.Count; i++) has a hidden performance hit; i.e. list.Count gets evaluated each time, so we write something like: int listCount = list.Count; for (int i = 0; i < listCount; i++)
0
8814
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8713
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9079
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7957
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3160
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.