Re: [Tutor] Memory error - how to manage large data sets?
Since the question is less than 200, I used b limit. However, to have limit = 2, perhaps I should do while b = limit. Thanks Alan for pointing it out. - Original Message - From: Alan Gauld [EMAIL PROTECTED] To: tutor@python.org Date: Thu, 31 Jul 2008 06:39:32 +0100 Subject: Re: [Tutor] Memory error - how to manage large data sets? Kepala Pening [EMAIL PROTECTED] wrote def sumEvenFibonacci( limit ): a, b = 1, 1 # don't waste with a = 0 sum = 0 while b limit: if b%2 == 0: sum += b a, b = b, a + b return sum print sumEvenFibonacci( 200 ) Does it work for limit = 2? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Nested dictionary with defaultdict
count = lambda x: [{y: x.count(y)} for y in set(x)] y = {} for key, val in myDict.items(): y[key] = count(val) print y {'1': [{'220': 3}], '3': [{'238': 1}, {'220': 1}], '2': [{'238': 4}, {'220': 1}], '5': [{'238': 1}, {'220': 2}], '4': [{'220': 2}], '7': [{'220': 1}], '6': [{'238': 2}]} - Original Message - From: GTXY20 [EMAIL PROTECTED] To: tutor@python.org Date: Tue, 15 Apr 2008 21:51:02 -0400 Subject: [Tutor] Nested dictionary with defaultdict Hi tutors, I currently have a dictionary like the following: {'1': ['220', '220', '220''], '2': ['220', '238', '238', '238', '238'], '3': ['220', '238'], '4': ['220', '220'], '5': ['220', '220', '238'], '6': ['238', '238'], '7': ['220']} I am trying to create a dictionary that would list the current key and a count of the iterations of values within the value list like so: {'1': {'220' : 3}, '2': {'220' : 1}, 2: {238 : 4}, '3': {'220' : 1}, 3: { '238' : 1}, '4': {220 : 2}, '5': {'220: 2}, '5': {238 : 1}, '6': {'238' : 2}, '7': {'220' : 1}} Now I am pretty sure that I need to loop through the first dictionary and create a defaultdict from the values for each key in that dictionary but at this point I am having difficulty coming up with the loop. I am looking for a satrting point or any suggestions. Many thanks in advance. GTXY20 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 50, Issue 9
import re items = [] for line in open('data.txt'): items.append(re.sub('\n', '', line).split(' ')) - Original Message - From: Gloom Demon [EMAIL PROTECTED] To: tutor@python.org Date: Wed, 9 Apr 2008 15:29:35 +0300 Subject: Re: [Tutor] Tutor Digest, Vol 50, Issue 9 Hello :-) Can someone please explain to me ho can I find out how many elements are there in one record of a list? The problem is as follows: I have a txt file from which I read data into Python. The file looks something like this: 01 bla bla bla 23,15 2345,67 02 alb alb 2,4 890,1 03 bal bla alb lab 567,12345 87,45 I need to be able to discriminate the string parts from the numeric ones. Since the number of words in the file can vary, I have to be able to find out when they are finished and when the floats come in mystring[0]- always integer mystring[1]- string (word) mystring[1-X]- last string (word) mystring[X+1]- always float mystring[X+2]- always float it would have been nice if I could find out the total number of the fields in one list record so that I could then adress them via a variable. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Even More Converter!
import re num = 123456789 print ','.join(re.findall(\d{3}, str(num))) output: 123,456,789 - Original Message - From: [EMAIL PROTECTED] To: tutor@python.org Date: Fri, 21 Mar 2008 21:49:18 -0700 Subject: [Tutor] Even More Converter! It works perfectly, so I am sure my question will not be hard to answer. When Python gives me the answer to my conversion, is there a way to create it so every 3 numbers a comma is inserted? Such as: 1 mile is 5,280 feet. Instead of 1 mile is 5280 feet. Yes a simple thing, but something which I believe will make it look better. Also is there a way to make this so I don't have to go through every individual line of code and add *insert comma* or something to it, simply at the top like how the Unit Menu is placed only once there, and yet applies to the whole document. Thank you ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Even More Converter!
sorry, I forgot that re search from the front of the string. import re putComma = lambda x: (','.join(re.findall(\d{1,3}, str(x)[::-1])))[::-1] print putComma(1234567) # 1,234,567 print putComma(12345678)# 12,345,678 print putComma(123456789) # 123,456,789 - Original Message - From: bhaaluu [EMAIL PROTECTED] To: Kepala Pening [EMAIL PROTECTED] Cc: tutor@python.org Date: Sat, 22 Mar 2008 09:02:40 -0400 Subject: Re: [Tutor] Even More Converter! import re num = 12345678 print ','.join(re.findall(\d{3}, str(num))) output: 123,456 Where is the '78'? It looks like that solution inserts comma's from left to right instead of from right to left. -- b h a a l u u at g m a i l dot c o m You assist an evil system most effectively by obeying its orders and decrees. An evil system never deserves such allegiance. Allegiance to it means partaking of the evil. A good person will resist an evil system with his or her whole soul. [Mahatma Gandhi] On Sat, Mar 22, 2008 at 6:17 AM, Kepala Pening [EMAIL PROTECTED] wrote: import re num = 123456789 print ','.join(re.findall(\d{3}, str(num))) output: 123,456,789 - Original Message - From: [EMAIL PROTECTED] To: tutor@python.org Date: Fri, 21 Mar 2008 21:49:18 -0700 Subject: [Tutor] Even More Converter! It works perfectly, so I am sure my question will not be hard to answer. When Python gives me the answer to my conversion, is there a way to create it so every 3 numbers a comma is inserted? Such as: 1 mile is 5,280 feet. Instead of 1 mile is 5280 feet. Yes a simple thing, but something which I believe will make it look better. Also is there a way to make this so I don't have to go through every individual line of code and add *insert comma* or something to it, simply at the top like how the Unit Menu is placed only once there, and yet applies to the whole document. Thank you ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor