[Tutor] test

Chad Crabtree flaxeater at yahoo.com
Fri Jun 17 16:41:17 CEST 2005


How about this.
 >>> from random import choice
 >>> alist=[choice(range(100)) for x in range(1000)] #just making a
list 
to look at pretend this is a file
 >>>
 >>> counter={}
 >>> for item in alist:
...     if item in counter.keys():
...         counter[item]+=1
...     else:
...         counter[item]=1
...    
 >>> counter
{0: 9, 1: 9, 2: 9, 3: 14, 4: 15, 5: 15, 6: 13, 7: 5, 8: 11, 9: 12,
10: 
9, 11: 12, 12: 13, 13: 8, 14: 5, 15: 12, 16: 14, 17: 9, 18: 11, 19:
8, 
20: 6, 21: 13, 22: 11, 23: 10, 24: 8, 25: 15, 26: 19, 27: 11, 28: 13,

29: 13, 30: 12, 31: 18, 32: 10, 33: 5, 34: 9, 35: 5, 36: 9, 37: 12,
38: 
8, 39: 11, 40: 8, 41: 12, 42: 6, 43: 13, 44: 11, 45: 8, 46: 8, 47: 6,

48: 9, 49: 6, 50: 5, 51: 11, 52: 11, 53: 12, 54: 15, 55: 15, 56: 10,
57: 
12, 58: 13, 59: 6, 60: 6, 61: 7, 62: 8, 63: 13, 64: 14, 65: 7, 66: 7,

67: 12, 68: 5, 69: 10, 70: 8, 71: 7, 72: 12, 73: 12, 74: 6, 75: 13,
76: 
12, 77: 13, 78: 9, 79: 5, 80: 13, 81: 14, 82: 4, 83: 6, 84: 8, 85:
12, 
86: 8, 87: 10, 88: 10, 89: 7, 90: 7, 91: 9, 92: 12, 93: 14, 94: 8,
95: 
7, 96: 10, 97: 11, 98: 8, 99: 8}
 >>>


Asif Iqbal wrote:

>Hi All
>
>I have a very simple problem and I am looking for the simplest
solution.
>
>I have a list of elements in a file. I like to find the total
occurance
>of each element in the list like this
>
>10 string1
>7 string2
>1 string3
>
>from a list which has only string1,string2 and string3 like this
>
>string1
>..
>string2
>...
>string1
>..
>string3
>...
>...
>
>
>I have a list of 30000 lines and I think I have only 615 unique
>elements. So I like the script not to take too much memory of the
>system. I will be running it on Solaris 8
>
>Thanks a lot
>
>  
>



		
____________________________________________________ 
Yahoo! Sports 
Rekindle the Rivalries. Sign up for Fantasy Football 
http://football.fantasysports.yahoo.com


More information about the Tutor mailing list