0

So I've got multiple lists like:

transaction = ['BUY', 'SELL', ...]
company = ['Acer', 'Cemex', ...]
value = [[0.5344, 2.23423], [5.43534, 4.3342543], ...]

The pseudo code that I want is like:

for iteration in transaction:
    count = 0
    if iteration == 'BUY':
        for each different company in the list company:
            execute an equation to add to an accumulator
            count += 1
    if iteration == 'SELL':
        for each different company in the list company:
            execute an equation to subtract from an accumulator
            count += 1

Can anybody explain to me what method I would use to achieve the outcome that I would like. I was thinking a for loop with multiple if statements beneath it, but it doesn't work.

8
  • 2
    You already pretty much have it. Just take out the extra pseudocode words (i.e. for each in company: instead of for each different company in the list company:), and all you have left to implement is the equation. Commented Nov 9, 2015 at 23:59
  • You can also simplify by having only one nested for loop and placing both ifs in there as an if/elif block. Commented Nov 10, 2015 at 0:06
  • I forgot to add a pretty important condition. Sorry. So basically, each index of the list corresponds with each other. So company[0] has to correspond with transaction[0] and so on. I've updated my pseudo code. Can you tell me if my logic is on the right track? Thanks. Commented Nov 10, 2015 at 0:27
  • look into zip function if you want to loop through company and transaction at the same time ie for x,y in zip(company,transaction): Commented Nov 10, 2015 at 0:31
  • @RNar: At least as written, it looks like each transaction is intended to apply to all companies, so zip wouldn't be appropriate. Of course, the code may be doing something other than intended... Commented Nov 10, 2015 at 0:41

2 Answers 2

1

I'm not entirely sure what outcome you would like, but based on your comments on the question, it seems you may want to loop over all lists at once. You can use zip for this.

transactions = ['BUY', 'SELL']
companies = ['Acer', 'Cemex']
values = [[0.5344, 2.23423], [5.43534, 4.3342543]]
for trans, company, (val1, val2) in zip(transactions, companies, values):
    if trans == 'BUY':
        print("Bought {}: {}, {}".format(company, val1, val2))
    elif trans == 'SELL':
        print("Sold {}: {}, {}".format(company, val1, val2))
Sign up to request clarification or add additional context in comments.

Comments

0

As mentioned, consider using Pandas, Python's powerful data analysis package where you can even import your lists into a dataframe or import from csv or sql table.

(I add more data points into your sample for demonstration. Notice the count of Acer and Cemex by transaction):

import pandas as pd

transaction = ['BUY','SELL', 'BUY', 'BUY', 'BUY', 'SELL']
company = ['Acer', 'Cemex', 'Acer', 'Apple', 'Cemex', 'GE']
value = [[0.5344, 2.23423], [5.43534, 4.3342543],
         [5.4387874, 4.3342543], [2.9313534, 4.3342543],
         [4.431224, 4.3342543], [6.4778734, 4.3342543]]

# DEFINE DATA FRAME
df = pd.DataFrame({'transaction':transaction,
                   'company':company,
                   'value1':[value[0][0],value[1][0],value[2][0],
                             value[3][0],value[4][0],value[5][0]],
                   'value2':[value[0][1],value[1][1],value[2][1],
                             value[3][1],value[4][1],value[5][1]]})

print(df)

#   company transaction    value1    value2
# 0    Acer         BUY  0.534400  2.234230
# 1   Cemex        SELL  5.435340  4.334254
# 2    Acer         BUY  5.438787  4.334254
# 3   Apple         BUY  2.931353  4.334254
# 4   Cemex         BUY  4.431224  4.334254
# 5      GE        SELL  6.477873  4.334254

print(df.groupby(['company', 'transaction']).count())

#                      value1  value2
# company transaction                
# Acer    BUY               2       2
# Apple   BUY               1       1
# Cemex   BUY               1       1
#         SELL              1       1
# GE      SELL              1       1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.