Searching and manipulating lists of tuples

2006-06-12 Thread MTD
Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs e.g. L = [ (X,1),(Y,2)] would mean X occurs once and Y occurs twice If I am given a string, I want to search L to see if

Re: Searching and manipulating lists of tuples

2006-06-12 Thread Mike Kent
MTD wrote: Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs ... So clearly that doesn't work... any ideas? Yes, use the proper tool for the job. Tuples are

Re: Searching and manipulating lists of tuples

2006-06-12 Thread Steve Holden
MTD wrote: Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs e.g. L = [ (X,1),(Y,2)] would mean X occurs once and Y occurs twice If I am given a string, I want

Re: Searching and manipulating lists of tuples

2006-06-12 Thread Harold Fellermann
MTD wrote: Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs e.g. L = [ (X,1),(Y,2)] would mean X occurs once and Y occurs twice If I am given a string, I want to

Re: Searching and manipulating lists of tuples

2006-06-12 Thread MTD
Yes, use the proper tool for the job. Tuples are immutable (they are read-only once created). Instead use a dictionary. They key would be your string, the value would be the count. Wow, I really should have thought of that! Thanks. -- http://mail.python.org/mailman/listinfo/python-list

Re: Searching and manipulating lists of tuples

2006-06-12 Thread Gerard Flanagan
MTD wrote: Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs e.g. L = [ (X,1),(Y,2)] would mean X occurs once and Y occurs twice If I am given a string, I want to

Re: Searching and manipulating lists of tuples

2006-06-12 Thread Sion Arrowsmith
Steve Holden [EMAIL PROTECTED] wrote: def algorith(d, s): if s in d: d[s] += 1 else: d[s] = 1 def algorith(d, s): d[s] = d.get(s, 0) + 1 And the OP should note that converting between dict d and list of pairs L is simply a matter of L = d.items() and d = dict(L)

Re: Searching and manipulating lists of tuples

2006-06-12 Thread bruno at modulix
MTD wrote: Hello, I'm wondering if there's a quick way of resolving this problem. In a program, I have a list of tuples of form (str,int), where int is a count of how often str occurs e.g. L = [ (X,1),(Y,2)] would mean X occurs once and Y occurs twice If I am given a string, I want