|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:21 on Mar 13, 2017 |
# ? Dec 24, 2009 01:25 |
|
|
# ? May 29, 2024 17:12 |
|
GregNorc posted:So I'm trying to create a function that returns True if the list is already orted, and false if it isn't. Wouldn't it be faster to check if the list is sorted in O(n) time (i.e. a single loop pass), rather than sorting it and comparing, taking O(n log n) time? Also, calling a function requires (), otherwise you're just expressing the function object. i.e. if you have a function def f(): return 1 a = f b = f() a contains the "value" of the function f, b contains the value 1.
|
# ? Dec 24, 2009 01:25 |
|
x.sort() returns None. sorted(x) returns a sorted copy of the list. if x.sort() returned the sorted list, your method would be a little odd. .sort() changes the list in place. edited to add: "I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question." code:
code:
tef fucked around with this message at 01:42 on Dec 24, 2009 |
# ? Dec 24, 2009 01:27 |
|
chips posted:Wouldn't it be faster to check if the list is sorted in O(n) time (i.e. a single loop pass), rather than sorting it and comparing, taking O(n log n) time? Yes. Regardless, unless you're working with huge amounts of data and you don't know anything about the ordering but there's a good chance the data is already sorted (I can't think of a situation in which these circumstances would arise), it's better to just assume the list isn't sorted and sort it. It's kind of pointless to check to see if a list is sorted because if it isn't, you'll have to sort it anyway. MaberMK fucked around with this message at 01:41 on Dec 24, 2009 |
# ? Dec 24, 2009 01:38 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:21 on Mar 13, 2017 |
# ? Dec 24, 2009 01:41 |
|
GregNorc posted:[insert facepalm here] Don't get carried away, read tef's post. As he pointed out, calling x.sort() will sort x and return None. sorted(x) will return x, sorted.
|
# ? Dec 24, 2009 01:50 |
|
...
maskenfreiheit fucked around with this message at 06:21 on Feb 19, 2010 |
# ? Dec 24, 2009 01:51 |
|
That question does indicate that you should probably not be using the .sort() or sorted() functions, but rather loop through and check that each element is in order. It is more efficient to do it that way, though it would work with the given conditions to do your method. Also since your code will be in python and sort() is presumably in C, it might be slower for any reasonable number of elements anyway.
|
# ? Dec 24, 2009 02:05 |
|
Does python have an easy way to make dictionaries with many to one mapping? Like (çookies,milk,reindeer) -> santa but if i call cookies or whatever by itself it returns santa
|
# ? Dec 25, 2009 11:26 |
|
code:
|
# ? Dec 25, 2009 13:23 |
|
Sylink posted:Does python have an easy way to make dictionaries with many to one mapping? Chips' approach will work for reading but will break once you reassign to any single item, which I'm guessing you don't want? It's pretty easy to subclass dict to make it work this way; check out the docs for __setitem__ and __getitem__. Main issue is that you'd need to add a mechanism for telling these dict-like objects what keys are to be tied together in this way, e.g. myinstance.link_keys('cookies', 'milk', 'reindeer').
|
# ? Dec 26, 2009 00:59 |
|
bitprophet posted:Chips' approach will work for reading but will break once you reassign to any single item, which I'm guessing you don't want? It's pretty easy to subclass dict to make it work this way; check out the docs for __setitem__ and __getitem__. Main issue is that you'd need to add a mechanism for telling these dict-like objects what keys are to be tied together in this way, e.g. myinstance.link_keys('cookies', 'milk', 'reindeer').
|
# ? Dec 26, 2009 01:19 |
|
chips posted:
That defeats the purpose of not having to type dozens of different individual entries in a dictionary which is why I wanted a many to one method in the first place.
|
# ? Dec 26, 2009 02:16 |
|
Sylink posted:That defeats the purpose of not having to type dozens of different individual entries in a dictionary which is why I wanted a many to one method in the first place. Show us the syntax you want to be legal, and we'll show you how to write a function that gets you close to that syntax. It's not clear what you are imagining, and that's why people aren't answering your question.
|
# ? Dec 26, 2009 02:46 |
|
How else would you express many-to-one? Having a "linked" mechanism doesn't add anything over references as you still need to be able to remap one of a currently linked set to an unrelated item. Any many-to-one collection must ultimately be a dictionary of references as I described, since you still have to explicitly specify each item. Linking 'b' to 'a' is just code:
code:
code:
|
# ? Dec 26, 2009 02:53 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:21 on Mar 13, 2017 |
# ? Dec 26, 2009 04:20 |
|
I think the problem is that pop() works like a queue, not like a loop. Try this: code:
|
# ? Dec 26, 2009 04:42 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:21 on Mar 13, 2017 |
# ? Dec 26, 2009 04:51 |
|
GregNorc posted:It should output 3 and 4 (the only two which repeat) This is not what "only the unique elements in a list" means. If you're doing exercise 10.6 from Think Python, then Glyn's solution is correct.
|
# ? Dec 26, 2009 04:59 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:21 on Mar 13, 2017 |
# ? Dec 26, 2009 05:02 |
|
GregNorc posted:This one is supposed to be a function that takes a list, and returns a new list consisting of only the unique elements from the original list. code:
|
# ? Dec 26, 2009 05:38 |
|
Scaevolus posted:
Doesn't maintain order. Edit: O(n) solution is code:
That returns duplicate items. Removing those is an excersise to the reader king_kilr fucked around with this message at 06:35 on Dec 26, 2009 |
# ? Dec 26, 2009 06:31 |
|
king_kilr posted:Doesn't maintain order.
|
# ? Dec 26, 2009 08:15 |
|
The unique items thing reminds me of one of the first things I ever did in Python that I thought at the time was clever of me.code:
|
# ? Dec 26, 2009 18:34 |
|
Thermopyle posted:The unique items thing reminds me of one of the first things I ever did in Python that I thought at the time was clever of me. Heh, that is a little clever. I used the same technique for making a list of the distinct colours (as rgb tuples) in an image before I learned how sets worked in python.
|
# ? Dec 26, 2009 22:50 |
|
Scaevolus posted:Problem statement doesn't mention order. Well maybe it should have /last word
|
# ? Dec 27, 2009 00:36 |
|
My current project is going to require a GUI. Can anyone recommend good GUI modules for python? They can be windows only, I just want something simple and easy to make some text boxes and get input from them. I don't really like Tkinter but I wanted a different opinion.
|
# ? Dec 27, 2009 11:10 |
|
Sylink posted:My current project is going to require a GUI. PyQt or wxPython. I prefer PyQt.
|
# ? Dec 27, 2009 11:25 |
|
I haven't tried PyQt myself but I've used wxPython on a few projects and it worked really well. I picked it up easily using this tutorial
|
# ? Dec 27, 2009 11:41 |
|
I like PyGTK though it's a little trickier to get up and running on Windows.
|
# ? Dec 27, 2009 17:04 |
|
I like the PyGTK API best, but it's not so fun (or pretty) for windows users.
|
# ? Dec 27, 2009 19:29 |
|
king_kilr posted:I like the PyGTK API best, but it's not so fun (or pretty) for windows users. It can be if you install Pidgin, or whatever version of GTK comes with it. It gets you a bunch of decent themes. I use Clearlooks on Windows, which really pretties up the handful of PyGTK-based tools I've built and use on a day-to-day basis.
|
# ? Dec 27, 2009 20:21 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:21 on Mar 13, 2017 |
# ? Dec 27, 2009 23:37 |
|
GregNorc posted:So I am in the think python chapter on tuples. you aren't using the sum function correctly, the first argument should be an iterable http://docs.python.org/library/functions.html#sum
|
# ? Dec 27, 2009 23:42 |
|
GregNorc posted:Sum by dault takes two values. So for example, sum(1, 2) returns 3 GregNorc posted:This is really confusing me... I know tuples are immutable, but I am not modifying the tuple.
|
# ? Dec 27, 2009 23:44 |
|
if you replace sum(i, total) with i + total it will work. As it is sum(args) will work as you intend. Further, there is no reason for a while loop here. Tuples are iterable. Edit: if you're not trying this stuff out in the python shell, you're doing it wrong.
|
# ? Dec 27, 2009 23:44 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:22 on Mar 13, 2017 |
# ? Dec 27, 2009 23:50 |
|
Edit: doublepost
maskenfreiheit fucked around with this message at 01:22 on Mar 13, 2017 |
# ? Dec 27, 2009 23:51 |
|
quote:Traceback (most recent call last): You need to learn to read these. It says your sum_all function was called and inside it an error happened on line 6 where you call sum. So you open up the interactive interpreter and try a few things until you get to sum(1, 2) and see that that is the error.
|
# ? Dec 28, 2009 00:27 |
|
|
# ? May 29, 2024 17:12 |
|
GregNorc posted:
Slightly unrelated, but you can iterate over items in a list using the for loop. It's shorter and easier: code:
code:
|
# ? Dec 28, 2009 00:42 |