Like a matrix

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

    Like a matrix

    I have a string of numbers in a file like so:
    0 3 23.3 352 45 4
    4 45 23 54 4 5.4 .6

    I need to average them horizontally and vertically. Horizontally is easy
    since I'm reading one line at a time, but how can I do it vertically like 0
    and 4, 3 and 45, etc.
    I was thinking of a dictionary but how do you add entries to a dictionary,
    you can't can you? Like I would add 0 to a dictionary that holds the 0th
    column. And then when I get to line 2, I would add 4 to the dictionary
    which has the 0th column data.



  • Erik Max Francis

    #2
    Re: Like a matrix

    Yazar Yolait wrote:
    [color=blue]
    > I have a string of numbers in a file like so:
    > 0 3 23.3 352 45 4
    > 4 45 23 54 4 5.4 .6
    >
    > I need to average them horizontally and vertically. Horizontally is
    > easy
    > since I'm reading one line at a time, but how can I do it vertically
    > like 0
    > and 4, 3 and 45, etc.[/color]

    Why not read it into a list of lists, and then iterate both ways?
    [color=blue]
    > I was thinking of a dictionary but how do you add entries to a
    > dictionary,
    > you can't can you?[/color]

    Sure you can:
    [color=blue][color=green][color=darkred]
    >>> d = {}
    >>> d[0] = 'zero'
    >>> d[/color][/color][/color]
    {0: 'zero'}[color=blue][color=green][color=darkred]
    >>> d[1] = 'one'
    >>> d[/color][/color][/color]
    {0: 'zero', 1: 'one'}[color=blue][color=green][color=darkred]
    >>> d[10] = {'a': 1, 'b': 2, 'c': 3}
    >>> d[/color][/color][/color]
    {0: 'zero', 1: 'one', 10: {'a': 1, 'c': 3, 'b': 2}}

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ The price of eternal vigilance is indifference.
    \__/ Marshall McLuhan

    Comment

    • Peter Otten

      #3
      Re: Like a matrix

      Yazar Yolait wrote:
      [color=blue]
      > I have a string of numbers in a file like so:
      > 0 3 23.3 352 45 4
      > 4 45 23 54 4 5.4 .6
      >
      > I need to average them horizontally and vertically. Horizontally is easy
      > since I'm reading one line at a time, but how can I do it vertically like
      > 0 and 4, 3 and 45, etc.
      > I was thinking of a dictionary but how do you add entries to a dictionary,
      > you can't can you? Like I would add 0 to a dictionary that holds the 0th
      > column. And then when I get to line 2, I would add 4 to the dictionary
      > which has the 0th column data.[/color]
      [color=blue][color=green][color=darkred]
      >>> from __future__ import division
      >>> def avg(alist): return sum(alist)/len(alist)[/color][/color][/color]
      ....[color=blue][color=green][color=darkred]
      >>> u = [1,2,3]
      >>> v = [3,2,1]
      >>> avg(u)[/color][/color][/color]
      2.0

      So that was the easy part. Now let's create the columns:
      [color=blue][color=green][color=darkred]
      >>> m, n, o = zip(u, v)
      >>> m[/color][/color][/color]
      (1, 3)[color=blue][color=green][color=darkred]
      >>> n[/color][/color][/color]
      (2, 2)[color=blue][color=green][color=darkred]
      >>> avg(o)[/color][/color][/color]
      2.0[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      So that wasn't particular hard, now you see it :-)
      zip() returns tuples instead of lists, but as you see above that does no
      harm.

      Peter

      Comment

      • ryan scott

        #4
        Re: Like a matrix

        Peter Otten wrote:[color=blue]
        > Yazar Yolait wrote:
        >
        >[color=green]
        >>I have a string of numbers in a file like so:
        >>0 3 23.3 352 45 4
        >>4 45 23 54 4 5.4 .6
        >>
        >>I need to average them horizontally and vertically. Horizontally is easy
        >>since I'm reading one line at a time, but how can I do it vertically like
        >>0 and 4, 3 and 45, etc.
        >>I was thinking of a dictionary but how do you add entries to a dictionary,
        >>you can't can you? Like I would add 0 to a dictionary that holds the 0th
        >>column. And then when I get to line 2, I would add 4 to the dictionary
        >>which has the 0th column data.[/color]
        >
        >[color=green][color=darkred]
        >>>>from __future__ import division
        >>>>def avg(alist): return sum(alist)/len(alist)
        >>>[/color][/color]
        > ...
        >[color=green][color=darkred]
        >>>>u = [1,2,3]
        >>>>v = [3,2,1]
        >>>>avg(u)
        >>>[/color][/color]
        > 2.0
        >
        > So that was the easy part. Now let's create the columns:
        >
        >[color=green][color=darkred]
        >>>>m, n, o = zip(u, v)
        >>>>m
        >>>[/color][/color]
        > (1, 3)
        >[color=green][color=darkred]
        >>>>n
        >>>[/color][/color]
        > (2, 2)
        >[color=green][color=darkred]
        >>>>avg(o)
        >>>[/color][/color]
        > 2.0
        >
        >
        > So that wasn't particular hard, now you see it :-)
        > zip() returns tuples instead of lists, but as you see above that does no
        > harm.
        >
        > Peter[/color]

        As an addition to this post, you could use avg in map to return a list
        of your averages.
        [color=blue][color=green]
        >>map(avg, zip(u,v))[/color][/color]
        [2.0, 2.0, 2.0]


        Ryan

        Comment

        Working...