looping list?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • datulaida
    New Member
    • May 2007
    • 14

    #1

    looping list?

    hi..
    i'm trying to insert value into list according to the key (ASCII) that i generate..

    example :

    win = 95, so must insert into list[95]
    and = 70, so must insert into list[70]

    this is my coding..
    _______________ _______________ _______________ _______________ ______

    list = []

    try:
    conn = MySQLdb.connect (host = "localhost" ,
    user = "root",
    passwd = "508420",
    db = "myimg")
    except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit (1)

    cursor = conn.cursor()


    cursor.execute ("SELECT word,id,freq,in deks FROM tablesusun")

    for(word,id,fre q,indeks) in cursor.fetchall ():

    first = ord(word[0])
    second = ord(word[1])
    last = first * second
    key = last % 100

    for i in range (100):
    if i == key :
    list.append([i,word])

    else :
    list.append([0])

    print list

    cursor.close ()
    conn.commit ()
    conn.close ()
    _______________ _______________ _______________ _______________ ______

    the ouput is :
    [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [and], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0];

    [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [win], [0], [0], [0], [0];
    _______________ _______________ _______________ _______________ ______

    i want the output is like this :

    [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [and], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [win], [0], [0], [0], [0];

    any ideas?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by datulaida
    hi..
    i'm trying to insert value into list according to the key (ASCII) that i generate..

    example :

    win = 95, so must insert into list[95]
    and = 70, so must insert into list[70]

    this is my coding..
    _______________ _______________ _______________ _______________ ______

    list = []

    try:
    conn = MySQLdb.connect (host = "localhost" ,
    user = "root",
    passwd = "508420",
    db = "myimg")
    except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit (1)

    cursor = conn.cursor()


    cursor.execute ("SELECT word,id,freq,in deks FROM tablesusun")

    for(word,id,fre q,indeks) in cursor.fetchall ():

    first = ord(word[0])
    second = ord(word[1])
    last = first * second
    key = last % 100

    for i in range (100):
    if i == key :
    list.append([i,word])

    else :
    list.append([0])

    print list

    cursor.close ()
    conn.commit ()
    conn.close ()
    _______________ _______________ _______________ _______________ ______

    the ouput is :
    [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [and], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0];

    [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [win], [0], [0], [0], [0];
    _______________ _______________ _______________ _______________ ______

    i want the output is like this :

    [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [and], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [win], [0], [0], [0], [0];

    any ideas?
    Try this (untested):[code=Python]
    words = [item[0] for item in cursor.fetchall ()]
    wordsList = [((ord(word[0])*ord(word[1])) % 100, word) for word in words]
    dd = dict(zip([s[0] for s in wordsList], [s[1] for s in wordsList]))
    print [[dd.get(i, 0)] for i in range(100)][/code]PLEASE use code tags in your future posts.

    Comment

    • datulaida
      New Member
      • May 2007
      • 14

      #3
      thanks..it's work :-)

      but why i cannot add more than 1 value in wordsList?

      example :

      [code=Python] words = [(word[0:],id[0:]) for(word,id,fre q,indeks) in cursor.fetchall ()][/code]

      error :

      words = [(word[0:],id[0:]) for(word,id,fre q,indeks) in cursor.fetchall ()]
      TypeError: 'long' object is unsubscriptable

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by datulaida
        thanks..it's work :-)

        but why i cannot add more than 1 value in wordsList?

        example :

        [code=Python] words = [(word[0:],id[0:]) for(word,id,fre q,indeks) in cursor.fetchall ()][/code]

        error :

        words = [(word[0:],id[0:]) for(word,id,fre q,indeks) in cursor.fetchall ()]
        TypeError: 'long' object is unsubscriptable
        The variable 'id' apparently is a long integer and is unscriptable. Try this:[code=Python]words_IDs = [(item[0], item[1]) for item in cursor.fetchall ()][/code]

        Comment

        Working...