My very first python program, need help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WP

    My very first python program, need help

    Hello, below is my very first python program. I have some questions
    regarding it and would like comments in general. I won't be able to get
    my hands on a good python book until tomorrow at the earliest. The
    program takes a string and sums all numbers inside it. I'm testing with
    the following string: "123xx,22! p1" which should yield a sum of 123 +
    22 + 1 = 146. My solution to this exercise was to write a function that
    takes a string and splits it into a list of numbers and then I will sum
    up the numbers in the list. In fact, I wrote two versions of this
    function: One uses a for loop to replace all characters that is not a
    digit and not a space with a space. That should leave me with the
    numbers intact and with only spaces separating these numbers. Then I
    call split to get a list of numbers (here I discovered I must not use a
    sep equal to ' ' or the returned list would contain empty strings as
    well). This is calculate_sum_1 () and look at the comment in the code to
    see what worries me there. The second one uses regular expressions and
    the problem I have with that is that I get a list of containing not just
    the numbers but empty strings as well. I'm working around that in my
    loop that sums the numbers but it would be better to not have these
    empty string in the list in the first place. Here's the code:

    import re

    def calculate_sum_1 (str):
    for c in str:
    if c.isdigit() == False and c != ' ':
    # That we assign to the variable we're looping over worries
    me...
    str = str.replace(c, ' ')

    mylist = str.split()

    print "(for loop version) mylist after replace() and split() = ",
    mylist

    sum = 0

    for item in mylist:
    sum += long(item)

    return sum

    def calculate_sum_2 (str):
    #print "In replace_nondigi ts_2(), str = ", str
    p = re.compile('[^0-9]')

    mylist = p.split(str)

    print "(regex version) mylist after calling split(): ", mylist

    sum = 0

    for item in mylist:
    if item.isdigit():
    sum += long(item)

    return sum

    str = "123xx,22! p1" # Sum = 123 + 22 + 1 = 146

    print "str = ", str

    print "calculate_sum_ 1(str): %d" % calculate_sum_1 (str)
    print "calculate_sum_ 2(str): %d" % calculate_sum_2 (str)

    The output when run is:
    str = 123xx,22! p1
    (for loop version) mylist after replace() and split() = ['123', '22', '1']
    calculate_sum_1 (str): 146
    (regex version) mylist after calling split(): ['123', '', '', '22', '',
    '', '1']
    calculate_sum_2 (str): 146


    Hope I made some sense and thanks for reading!

    - Eric (WP)
  • Wojtek Walczak

    #2
    Re: My very first python program, need help

    Dnia Sun, 10 Aug 2008 15:52:37 +0200, WP napisa³(a):

    Hi,
    import re
    >
    def calculate_sum_1 (str):
    ^^^
    this word is reserved, better use some other name
    for c in str:
    if c.isdigit() == False and c != ' ':
    # That we assign to the variable we're looping over worries
    me...
    str = str.replace(c, ' ')
    It's good that it worries you. AFAIR python behavior when assigning to
    a variable that is used in for loop is undefined, so you can't count
    on it. Better introduce second variable.
    >
    mylist = str.split()
    >
    print "(for loop version) mylist after replace() and split() = ",
    mylist
    >
    sum = 0
    >
    for item in mylist:
    sum += long(item)
    You could use list comprehensions and sum function in here.


    def calculate_sum_2 (str):
    #print "In replace_nondigi ts_2(), str = ", str
    p = re.compile('[^0-9]')
    or: p = re.compile('\d+ ')
    '\d+' is for one or more digits
    mylist = p.split(str)
    You don't have to split this string. Just search through it.
    Findall method seems appropriate.

    A bit more pythonic approaches to both of your functions:

    =============== ========
    import re

    a="123xx,22! p1"

    # Note that 'string' isn't the best name too. It may be shadowed by
    # or shadow 'string' module if it's imported.
    def calculate_sum_1 (string):
    result = ''

    for i in string:
    if i.isdigit():
    result += i
    else:
    result += ' '

    return sum([int(i) for i in result.split()])


    def calculate_sum_2 (string):
    pat = re.compile('\d+ ')
    digits = [int(i) for i in re.findall(pat, string)]
    return sum(digits)


    print calculate_sum_1 (a)
    print calculate_sum_2 (a)
    =============== ============


    --
    Regards,
    Wojtek Walczak,

    Comment

    • WP

      #3
      Re: My very first python program, need help

      Wojtek Walczak wrote:
      [snip]
      Thanks for all your help. I've incorporated your suggestions and moved
      on to my next program. See new thread. :)

      - Eric (WP)

      Comment

      Working...