#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Add Array Variables


    Okay I am having a bit of a issue I am new to python and I learn best by just jumping in head first and attempting to build my latest stuff in it.

    I am building a stock program. Currently I harvest all the stocks prices and volume from a mysql data base that gets them everyday for me.

    But I have a little issue. I am trying to make python pull this information and then run some math on it. The math is no problem its just I can seem to pull all the variables and store them in one long array

    here is my current code

    Stock1ID = [0]
    Stock1Symbol = [0]
    Stock1UnixTime = [0]
    Stock1Price = [0]
    Stock2ID = [0]
    Stock2Symbol = [0]
    Stock2UnixTime = [0]
    Stock2Price = [0]

    #!/usr/bin/python
    # view_rows.py - Fetch and display the rows from a MySQL database query

    # import the MySQLdb and sys modules
    import MySQLdb
    import sys

    # open a database connection
    # be sure to change the host IP address, username, password and database name to match your own
    connection = MySQLdb.connect (host = ".db...com", user = "", passwd = "@", db = "")

    # prepare a cursor object using cursor() method
    cursor = connection.cursor ()

    # execute the SQL query using execute() method.
    cursor.execute ("select ID, StockSymbol, UnixTime, Price from Data")

    # fetch all of the rows from the query
    data = cursor.fetchall ()

    # print the rows

    for Stock1Data in data :

    if Stock1Data[1] == "ADTN":
    Stock1Data[0] = Stock1Id
    Stock1ID.extend[Stock1Id]
    Stock1Symbol.extend[Stock1Data[1]]
    Stock1UnixTime.extend[Stock1Data[2]]
    Stock1Price.extend[Stock1Data[3]]

    for Stock2Data in data :
    if Stock2Data[1] == "ARAY":
    Stock2ID.extend[Stock2Data[0]]
    Stock2Symbol.extend[Stock2Data[1]]
    Stock2UnixTime.extend[Stock2Data[2]]
    Stock2Price.extend[Stock2Data[3]]


    if Stock1UnixTime == Stock2UnixTime: #Our unix times line up were all good
    print "Our unix times line up were all good"
    if Stock1UnixTime > Stock2UnixTime: #Stock 1 has a bigger time
    print "Stock 1 has a bigger time"
    if Stock1UnixTime < Stock2UnixTimx: #Stock 2 has a bigger time
    print "Stock 2 has a bigger time"


    # close the cursor object
    cursor.close ()

    # close the connection
    connection.close ()

    # exit the program
    sys.exit()





    Obviously this dose not work I wanted to just make it so it would have two array one called Stock1 and another called Stock2 that would contain all the values in the data base for that stock.


    But I cant figure out how to add to an array after ward

    all help is greatly appreciated.

    Thank You
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    - First, please put your code in [ code ] tags, because whitespace is critical to python code and we can't understand your code without them.

    - Second, and this may seem nitpicky, but there are no arrays in Python. There are a number of different collection types that all behave a little differently, and what you're using here is called a List. You'll have more luck finding out information about them if you use the right terminology.

    - You can append values to a list in a variety of ways:
    Code:
    mylist = []
    
    #use the append method
    mylist.append("something")
    
    #add a second list using +
    mylist += ["something else", "yet another something"]
    
    #insert into an arbitrary spot in the list
    mylist.insert(2, "foo")
    
    #This, however, won't work; you'll get an exception 
    # unless mylist[99] is previously assigned:
    mylist[99] = "blah"
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    I wish people wouldn't confuse "add" with joining words like "concatenate", "append", "extend". Needless to say, I've never myself been guilty. Or not never.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Thanks


    Thank you for the help now im sure I can play with all those an get it to work and im sorry I come from C++ so there is a quite a few difference's that im trying to get used to.

    Thank you and also I wasn't sure how to enclose my script from now on I will thanks.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    3
    Rep Power
    0

    Quick Question


    Okay I got this to work no problem but how would I get a count of the amount of items included in the mylist variable.

    Thank You so much for the help by the way
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    len(mylist)

    or maybe you mean

    sum(mylist)

    ?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo