vector addition

Discussion in 'Python' started by GZ, Jun 6, 2010.

  1. GZ

    GZ Guest

    Hi,

    I am looking for a fast internal vector representation so that
    (a1,b2,c1)+(a2,b2,c2)=(a1+a2,b1+b2,c1+c2).

    So I have a list

    l = ['a'a,'bb','ca','de'...]

    I want to count all items that start with an 'a', 'b', and 'c'.

    What I can do is:

    count_a = sum(int(x[1]=='a') for x in l)
    count_b = sum(int(x[1]=='b') for x in l)
    count_c = sum(int(x[1]=='c') for x in l)

    But this loops through the list three times, which can be slow.

    I'd like to have something like this:
    count_a, count_b, count_c =
    sum( (int(x[1]=='a',int(x[1]=='b',int(x[1]=='c') for x in l)

    I hesitate to use numpy array, because that will literally create and
    destroy a ton of the arrays, and is likely to be slow.
     
    GZ, Jun 6, 2010
    #1
    1. Advertising

  2. GZ

    Chris Rebert Guest

    On Sat, Jun 5, 2010 at 6:20 PM, GZ <> wrote:
    > Hi,
    >
    > I am looking for a fast internal vector representation so that
    > (a1,b2,c1)+(a2,b2,c2)=(a1+a2,b1+b2,c1+c2).
    >
    > So I have a list
    >
    > l = ['a'a,'bb','ca','de'...]
    >
    > I want to count all items that start with an 'a', 'b', and 'c'.
    >
    > What I can do is:
    >
    > count_a = sum(int(x[1]=='a') for x in l)
    > count_b = sum(int(x[1]=='b') for x in l)
    > count_c = sum(int(x[1]=='c') for x in l)
    >
    > But this loops through the list three times, which can be slow.


    I don't really get how that relates to vectors or why you'd use that
    representation, and it looks like you're forgotten that Python uses
    0-based indexing, but anyway, here's my crack at something more
    efficient:

    from collections import defaultdict

    cared_about = set('abc')
    letter2count = defaultdict(int)
    for item in l:
    initial = item[0]
    if initial in cared_about:
    letter2count[initial] += 1

    count_a = letter2count['a']
    count_b = letter2count['b']
    count_c = letter2count['c']

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Jun 6, 2010
    #2
    1. Advertising

  3. GZ

    MRAB Guest

    GZ wrote:
    > Hi,
    >
    > I am looking for a fast internal vector representation so that
    > (a1,b2,c1)+(a2,b2,c2)=(a1+a2,b1+b2,c1+c2).
    >
    > So I have a list
    >
    > l = ['a'a,'bb','ca','de'...]
    >
    > I want to count all items that start with an 'a', 'b', and 'c'.
    >
    > What I can do is:
    >
    > count_a = sum(int(x[1]=='a') for x in l)
    > count_b = sum(int(x[1]=='b') for x in l)
    > count_c = sum(int(x[1]=='c') for x in l)
    >
    > But this loops through the list three times, which can be slow.
    >
    > I'd like to have something like this:
    > count_a, count_b, count_c =
    > sum( (int(x[1]=='a',int(x[1]=='b',int(x[1]=='c') for x in l)
    >
    > I hesitate to use numpy array, because that will literally create and
    > destroy a ton of the arrays, and is likely to be slow.
    >

    If you want to do vector addition then numpy is the way to go. However,
    first you could try:

    from collections import defaultdict
    counts = defaultdict(int)
    for x in l:
    counts[x[0]] += 1

    (Note that in Python indexes are zero-based.)
     
    MRAB, Jun 6, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Midnight Java Junkie

    Office Development (feature addition)

    Midnight Java Junkie, Jun 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    470
    Midnight Java Junkie
    Jun 20, 2004
  2. pmatos
    Replies:
    6
    Views:
    24,044
  3. Replies:
    8
    Views:
    1,989
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    606
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    387
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page