Re: [Tutor] Sorting tuples

2012-06-20 Thread Dave Angel
On 06/20/2012 07:05 PM, Mike Nickey wrote: > Hey all, > > I'm working through the google classes to increase my python > understanding and I'm stuck on sorting tuples. > What is being asked is to sort tuples based on the second element of > the tuple in ascending order. > > Given a list of non-empt

Re: [Tutor] Sorting tuples

2012-06-20 Thread Steven D'Aprano
Mike Nickey wrote: While i'm still not sure what lamda is or represents, I've found a solution. Unfortunately, you haven't. It's only by accident that your not-quite-solution gives the correct value for the example data. Try it with this data instead: [(1, 7), (1, 3), (3, 4, 8), (2, 2)] re

Re: [Tutor] Sorting tuples

2012-06-20 Thread Alan Gauld
On 21/06/12 00:27, Mike Nickey wrote: While i'm still not sure what lamda is or represents, lambda is a function without a name. def sort_last(tuples): # +++your code here+++ print sorted(tuples, key=lamda tuples: tuples[-1]) return The lamda above is identical to doing: def f(

Re: [Tutor] Sorting tuples

2012-06-20 Thread Mike Nickey
While i'm still not sure what lamda is or represents, I've found a solution. Thank you all for your assistance, past, present and future. :) This seems to work: def sort_last(tuples): return sorted(tuples, key=myDef) def myDef(s): return s[-1] On Wed, Jun 20, 2012 at 4:05 PM, Mike Nicke

[Tutor] Sorting tuples

2012-06-20 Thread Mike Nickey
Hey all, I'm working through the google classes to increase my python understanding and I'm stuck on sorting tuples. What is being asked is to sort tuples based on the second element of the tuple in ascending order. Given a list of non-empty tuples, return a list sorted in increasing order by the

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Prasad, Ramit
Please do not top post. > Sorry for the double-post. > > I don't think I am wrong either. Replace doesn't return a list of words in > the way that my code sample was setup to do. > How would you solve it with replace? Maybe an example prove the point > better. > > -Mario > On Wed, Jun 20, 2012 a

Re: [Tutor] Counting Items in a List

2012-06-20 Thread mariocatch
Sorry for the double-post. I don't think I am wrong either. Replace doesn't return a list of words in the way that my code sample was setup to do. How would you solve it with replace? Maybe an example prove the point better. -Mario On Wed, Jun 20, 2012 at 4:10 PM, mariocatch wrote: > Yes, I me

Re: [Tutor] Counting Items in a List

2012-06-20 Thread mariocatch
Yes, I meant split(). Was a typo :) On Wed, Jun 20, 2012 at 3:56 PM, Prasad, Ramit wrote: > Please always post back to the list. > > > Nope, strip() was intended so we get a list back instead of a string. > Need > > the list for iteration down below that. > > >> >para = paragraph.strip('.').s

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Prasad, Ramit
Please always post back to the list. > Nope, strip() was intended so we get a list back instead of a string. Need > the list for iteration down below that. >> >para = paragraph.strip('.').strip(',').strip().split() >> I think you want replace not strip. >> >> See http://docs.python.org/libra

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Steven D'Aprano
Oğuzhan Öğreden wrote: Hi, I have been learning Python and trying little bits of coding for a while. Recently I tried to have a paragraph and create a list of its words, then counting those words. Here is the code: [code snipped] I can't understand your code! Indentation is messed up badly. I

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Prasad, Ramit
>    para = paragraph.strip('.').strip(',').strip().split() I think you want replace not strip. See http://docs.python.org/library/stdtypes.html#string-methods Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216

Re: [Tutor] Counting Items in a List

2012-06-20 Thread mariocatch
Here's one way, can surely be optimized: *paragraph = "This paragraph contains words once, more than once, and possibly not at all either. Figure that one out. " para = paragraph.strip('.').strip(',').strip().split() wordDict = {} for word in para: word = word.strip('.').st

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Alan Gauld
On 20/06/12 16:20, Martin A. Brown wrote: : should have written something like "counting how many times each : word occured" insted of "counting words". You might like to try out collections.defaultdict(). Even a standard dict would simplify the code dramatically. The defaultdict does sui

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Martin A. Brown
Hello, : Sorry, it seems like I was not clear enough in that statement. I : should have written something like "counting how many times each : word occured" insted of "counting words". You might like to try out collections.defaultdict(). import collections summary = collections.defaul

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Brad Hudson
I think this is more of what you ar looking for: >>> a = 'this is a list with is repeated twice.' >>> a 'this is a list with is repeated twice.' >>> alist = a.strip().split() >>> alist ['this', 'is', 'a', 'list', 'with', 'is', 'repeated', 'twice.'] >>> var='is' >>> alist.count(var) 2 >>> On Wed,

Re: [Tutor] Counting Items in a List

2012-06-20 Thread Oğuzhan Öğreden
Sorry, it seems like I was not clear enough in that statement. I should have written something like "counting how many times each word occured" insted of "counting words". 2012/6/20 mariocatch > Hello, > > Not sure if this is what you're trying to solve, although it sounds like > it based on you

Re: [Tutor] Counting Items in a List

2012-06-20 Thread mariocatch
Hello, Not sure if this is what you're trying to solve, although it sounds like it based on your leading statement. I think what you have is more complicated than it needs to be for something that simple: Why not something like this? *paragraph = """I have been learning Python and trying litt

[Tutor] Counting Items in a List

2012-06-20 Thread Oğuzhan Öğreden
Hi, I have been learning Python and trying little bits of coding for a while. Recently I tried to have a paragraph and create a list of its words, then counting those words. Here is the code: import re > > >> a = """Performs the template substitution, returning a new string. >> mapping is any di