Re: removing items from a dictionary ?

2007-07-26 Thread martyw
Stef Mientki wrote:
> hello,
> 
> I want to remove some items from a dictionary,
> so I would expect this should work:
> 
>   Nets = {}
>   ... fill the dictionary Nets
> 
>   for net in Nets:
> if net.upper() in Eagle_Power_Nets :
>   del Nets [ net ]
> 
> 
> But it gives me
> MessageFile NameLinePosition
> Traceback   
> ?D:\data_to_test\JALsPy\Eagle_import.py380   
> RuntimeError: dictionary changed size during iteration   
> 
> 
> Now I can solve this problem in the following way
> 
>   power_nets = []
>   for net in Nets:
> if net.upper() in Eagle_Power_Nets :
>   power_nets.append ( net )
> 
>   # remove power nets from netlist
>   for net in power_nets:
>  del Nets [ net ]
> 
> 
> But I wonder if this is the best way to manipulate a dictionary,
> because I've to do more "complex" operations on the dictionary,
> like joining items,
> I would like to have a better understanding of what can and what can't 
> be done.
> 
> thanks,
> Stef Mientki
Remoing elements from a dict is done with del, try this;
 >>> d = {'a' : 1,'b' : 2}
 >>> del d['a']
 >>> print d
{'b': 2}
 >>>

maybe you can post a working snippet to demonstrate your problem
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: while statements

2007-10-16 Thread martyw
Shawn Minisall wrote:
> I just learned about while statements and get why you place them around 
> inputs for validation, but I'm a little lost on exactly where to place 
> it with what condition in this program where the number of fat grams 
> exceeds the total number of calories so that it loops back and asks you 
> the two questions again instead of just saying The calories or fat grams 
> were incorrectly entered.  Any idea's?
> 
> thx
> 
> while cal <=0:
>#Prompt for calories
>cal = input("Please enter the number of calories in your food: ")
>if cal <=0:
>print "Error.  The number of calories must be positive."
> 
>#Prompt for fat
>fat = input("Please enter the number of fat grams in your food: ")
>if fat <=0:
>print "Error.  The number of fat grams must be positive."
> 
> 
>#Calculate calories from fat
>calfat = float(fat) * 9
>   #Calculate number of calories from fat
>caldel = calfat / cal
> 
>#change calcent decimal to percentage
>calcent = caldel * 100
> 
>#evaluate input
>if calfat > cal:
>print "The calories or fat grams were incorrectly entered."
> 
>elif calcent > 0 and calfat < cal:
>   if caldel <= .3:
>print "Your food is low in fat."
>elif caldel >= .3:
>print "Your food is high in fat."
> 
>#Display percentage of calories from fat
>print "The percentage of calories from fat in your food is %", 
> calcent
> 
you could change to something like this


while True: # don test in the loop
#Prompt for calories
 cal = input("Please enter the number of calories in your food: ")
 if cal <=0:
 print "Error.  The number of calories must be positive."
 continue ### here you don need to go any further in this loop

#Prompt for fat
 fat = input("Please enter the number of fat grams in your food: ")
 if fat <=0:
 print "Error.  The number of fat grams must be positive."
 continue ### here you don need to go any further in this loop


#Calculate calories from fat
 calfat = float(fat) * 9
   #Calculate number of calories from fat
 caldel = calfat / cal

#change calcent decimal to percentage
 calcent = caldel * 100

#evaluate input
 if calfat > cal:
print "The calories or fat grams were incorrectly entered."

 #elif calcent > 0 and calfat < cal:
 else: # calcent will be bigger than zero now by construction,
   # and I guess you dont want to test for equality of
   # calfat and cal
 if caldel <= .3:
print "Your food is low in fat."
 elif caldel >= .3:
print "Your food is high in fat."

#Display percentage of calories from fat
 print "The percentage of calories from fat in your food "\
   "is %f%%" % calcent
 break # we got a satisfactory result, leave this loop
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need some help...

2007-10-28 Thread martyw
[EMAIL PROTECTED] wrote:
>> I want to create a program that I type in a word.
> 
> You can see that Python has a command to input strings from the
> command line.
> 
>> chaos
>> each letter equals a number
>> A=1
>> B=20
>>  and so on.
>> So Chaos would be
>> C=13 H=4 A=1 O=7 S=5
>> I want to then have those numbers
>> 13+4+1+7+5 added together to be 30.
>> How can I do that?
> 
> Python has a dictionary data structure called dict(), or {}, that you
> can use to map your letters to those numbers. With it you can created
> the letter-number association.
> 
> Then you can scan the characters of the input string one after the
> other, and sum their values into a single total value. Try writing
> that code, and then show it to us, we can give more suggestions if you
> need them...
> 
> Bye,
> bearophile
> 
as an alternative for the suggest dictionary approach you could study 
the built in functions ord() and chr(), see the documentation 
(http://docs.python.org/lib/built-in-funcs.html)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting Countries by Region

2007-11-16 Thread martyw
[EMAIL PROTECTED] wrote:
> Hi all,
> 
> I'm analyzing some data that has a lot of country data.  What I need
> to do is sort through this data and output it into an excel doc with
> summary information.  The countries, though, need to be sorted by
> region, but the way I thought I could do it isn't quite working out.
> So far I can only successfully get the data alphabetically.
> 
> Any ideas?
> 
> import xlrd
> import pyExcelerator
> 
> def get_countries_list(list):
it isn't a good idea to use a built-in object as a variable name
> countries_list=[]
> for country in countries:ii
> if country not in countries_list:
> countries_list.append(country)
> 
> EU = ["Austria","Belgium", "Cyprus","Czech Republic",
> "Denmark","Estonia", "Finland"]
> NA = ["Canada", "United States"]
> AP = ["Australia", "China", "Hong Kong", "India", "Indonesia",
> "Japan"]
> Regions_tot = {'European Union':EU, 'North America':NA, 'Asia
> Pacific':AP,}
i would create a class to capture country information, e.g.
class country(object):
 def __init__(self, name, size = 0, population = 0):
 self.name = name
 self.size = size
 self.poplation = population

 def __cmp__(self, other):
 if self.name < other.name:
 return -1
 elif self.name > other.name:
 return 1
 else:
 return 0

then you can set up the world as

world = {'NA': [country("United States"), country("Canada")], \
  'Europe': [country("Belgium"), country("Austria")]}


now you can sort and print it easy

for region in world:
 print region
 lands = world[region]
 lands.sort()
 for land in lands:
 print land.name

the sort works because the country objects have a method __cmp__

> 
> path_file = "c:\\1\country_data.xls"
> book = xlrd.open_workbook(path_file)
> Counts = book.sheet_by_index(1)
> countries= Counts.col_values(0,start_rowx=1, end_rowx=None)
> 
> get_countries_list(countries)
> 
> wb=pyExcelerator.Workbook()
> matrix = wb.add_sheet("matrix")
> 
> n=1
> for country in unique_countries:
> matrix.write(n,1, country)
> n = n+1
> 
> wb.save('c:\\1\\matrix.xls')
> 
> 
> 

i'm not familiar with the excel modules so i can't help you with that
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sorting Countries by Region

2007-11-19 Thread martyw
nothing, Alan wrote:
> On Nov 16, 8:28 pm, martyw <[EMAIL PROTECTED]> wrote:
>> i would create a class to capture country information, e.g.
> 
> 
> What is the advantage of this:
> 
>>  def __cmp__(self, other):
>>  if self.name < other.name:
>>  return -1
>>  elif self.name > other.name:
>>  return 1
>>  else:
>>  return 0
>>
> 
> over
> def __cmp__(self,other):
> return cmp(self.name,other.name)
> 
> ?
> 
> --
> Alan
yours is better!
-- 
http://mail.python.org/mailman/listinfo/python-list