0

Would this be the way to sort a list of stock information with sorted using stock symbols as the key to sort on? new_translactions_info = sorted(transactions_info, key=lambda transactions_info: i.stock_symbol) transactions_info = new_transactions_info. Also, for reference, here's the code I currently have: https://pastebin.com/Gw64H1ky. And here's my template: https://pastebin.com/macgJ9AE.

The reason I want to do this is so that I can compare adjacent items in a list, while being able to guarantee that adjacent items in the list have the same stock symbol and can add up stocks' numbers of shares for the current user's holdings of the same stock.

Anyway, thanks in advance for any help. If there's a good place I can go to read about it, like a page in the Python documentation, it'd be great if someone would point me there.

5
  • Don't have an answer for your question, not sure I fully understand it. But you might want to look into the SQL query modifiers ORDER BY and GROUP BY. I think they might show you a more efficient path to your solution.
    – Cliff B
    Apr 5 '17 at 18:42
  • That sounds good, actually. But as for my question, I just want to know how to use sorted() to sort my transactions_info list, which has information on stocks owned by the current user, based on the stock symbols. And I want to use sorted() because I found that the list has dicts in it. But yeah, now that you mention it, ORDER By and GROUP BY might be more efficient for this. I'll look into those. The reason I want to do this is so that I can add up all of the instances of numbers of shares in the table for the same stock and the same user ID by only have to compare adjacent items. Apr 5 '17 at 21:53
  • Interesting. If I recall the exercise (it may have changed for the current year), if a user buys additional shares of a given stock, the existing row in the portfolio database should have the shares updated, not another row entered in the table. In database terms, there should be a unique or key index on the combination of the id and symbol to prevent more than one row being entered for a given user AND a given symbol. (For the history table, just the opposite is true. There should be a row entered for each and every buy or sell that successfully executes.)
    – Cliff B
    Apr 5 '17 at 23:26
  • In my table where I'm storing info on what was bought, at what quantity and at what price, I'm adding additional rows for additional shares of the stock bought by a user. For when some share(s) of stock is/are sold, I'll just add a row with a negative value to show how much of that stock was sold. I'm just worried about the date and time. I used time.time(), but it seems to have entered a weird number into the table that I don't understand. Like this for instance: "1490137303.48037" Apr 6 '17 at 10:59
  • I'm trying to create another table after dropping one, and this is the query I'm running: PRAGMA foreign_keys = ON; CREATE TABLE user_stocks ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, FOREIGN KEY (user_id) REFERENCES users (id), stock_name TEXT NOT NULL, stock_symbol TEXT NOT NULL, stock_price NUMERIC NOT NULL, no_of_shares INTEGER NOT NULL, time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP); I'm getting an error "near 'stock_symbol': syntax error" Apr 6 '17 at 16:19
0

The query manipulators GROUP BY and ORDER BY were what I needed. I've figured out the original "sort" problem I was having with that. Thank you, Cliff B.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .