473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Re: Equivalents of Ruby's "!" methods?



Paul McGuire wrote:
Pythonistically speaking, even though a dict is a mutable thing, I'm
learning that the accepted practice for methods like this is not so
much to update in place as it is to use generator expressions to
construct a new object.
I disagree. The convention is that mutation methods should return None.
For example, given a list of integers to 100,
instead of removing all of the even numbers (with the attendant
hassles of updating a list while iterating over it), just create a new
list of the numbers that are not even.
This is because each removal is O(n), making the entire process O(n*2),
whereas a new list is O(n) -- and the hassle of remembering to iterate
in reverse when doing removals.
Perhaps I am overgeneralizing from comments I have read here on c.l.py
about creating new lists instead updating old ones.
I think so. Removals and insertions are importantly different from
change in place. If I wanted to square each number in a list, and *did
not need the original list*, I would not hesitate to do it in place.

The newish sorted() and reversed() built-ins were meant to complement
list.sort and list.reverse, not replace them.

tjr

Aug 25 '08 #1
4 1103
On 25.08.2008, Terry Reedy <tj*****@udel.eduwroted:
The newish sorted() and reversed() built-ins were meant to complement
list.sort and list.reverse, not replace them.
BTW, is there a reason why sorted() on a list returns a list, while
reversed() on the same list returns an iterator?

GS
--
Grzegorz Staniak <gstaniak _at_ wp [dot] pl>
Aug 25 '08 #2
Grzegorz Staniak wrote:
BTW, is there a reason why sorted() on a list returns a list, while
reversed() on the same list returns an iterator?
the algorithm required to sort a sequence is something entirely
different from the algorithm required to loop over a sequence in
reverse. we went through this a few days ago.

</F>

Aug 25 '08 #3
On Mon, 25 Aug 2008 17:04:07 +0000, Grzegorz Staniak wrote:
On 25.08.2008, Terry Reedy <tj*****@udel.eduwroted:
>The newish sorted() and reversed() built-ins were meant to complement
list.sort and list.reverse, not replace them.

BTW, is there a reason why sorted() on a list returns a list, while
reversed() on the same list returns an iterator?
Until the day that somebody discovers how to sort a list without seeing
all the items first, there's no point in sorted() returning an iterator.
It has to generate a copy of the entire list to sort it, and so might as
well just return the list -- there's no advantage to turning it into an
iterator after you've already built the list.

On the other hand, reversed() can supply it's items lazily. Although it
does need access to the entire source, it doesn't need to return an
entire list. It can just return the items one at a time, starting from
the last one.

That however does mean there's one gotcha: if you mutate a list after
calling sorted() on it, the result of the sorted() doesn't change. But
the same doesn't hold for reversed():
>>L = range(5)
it = reversed(L) # expecting [4, 3, 2, 1, 0] as an iterator
it.next()
4
>>L[3] = 'mutate'
it.next()
'mutate'

The solution to that is simple: call list(reversed(L)). Or don't mutate
the original.

--
Steven
Aug 25 '08 #4
On Aug 26, 9:47*am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Mon, 25 Aug 2008 17:04:07 +0000, Grzegorz Staniak wrote:
On 25.08.2008, Terry Reedy <tjre...@udel.eduwroted:
The newish sorted() and reversed() built-ins were meant to complement
list.sort and list.reverse, not replace them.
BTW, is there a reason why sorted() on a list returns a list, while
reversed() on the same list returns an iterator?

Until the day that somebody discovers how to sort a list without seeing
all the items first, there's no point in sorted() returning an iterator.
To nitpick, this is not strictly true: sure, you're at best O(nlogn)
on sorting the entire list, but you could return the first element of
the 'sorted' list in O(n) (if you don't mind using a O(n^2) algorithm
for the whole sort). i.e. if you have a use case where you're only
likely to look at the first few elements of a sorted list, it would
make some sense to have an iterator.
Aug 26 '08 #5

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
13
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
10
by: Neal | last post by:
I'm beginning some experiments with Ruby in XHTML 1.1. I'm finding very odd results which surprise me. I'm using a PHP snippet which serves application/xml+xhtml and XHTML 1.1 to those browsers...
22
by: Francois | last post by:
I discovered Python a few months ago and soon decided to invest time in learning it well. While surfing the net for Python, I also saw the hype over Ruby and tried to find out more about it, before...
9
Niheel
by: Niheel | last post by:
I've used the following tutorials to help be get a better understanding of Ruby On Rails or ROR. Installing Ruby on Rails - Fedora / Lighthttpd Tutorial & Setup for Ruby on Rails Rolling with...
10
by: lawrence k | last post by:
I work mostly in PHP, but at the web design firm where I work we are thinking of switching to Ruby on Rails. Our lead designer recently installed Typo on a client's site and he said to us, with...
9
by: Erwin Moller | last post by:
Hi Group, This may seem a odd question in a PHP group, but I think this might be a good place to ask since I am mainly a PHP coder these days that maybe starts with Ruby. Situation: A client...
0
by: bruce | last post by:
hey guys... i managed to solve what i was attempting.. my goal was rather simple, to be able to have a python script, call a ruby app, and be able to return a value from the ruby (child) app to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.