jevans
jevans

Reputation: 399

Comparing user input with tuple value

In Python 3, I have a list of tuples containing various records, lets say it's called alpha_data. They are laid out as follows: [('A', 'Apple', 14.5), ('B', 'Banana', 23.4), etc.]

Is there a way a user can input a value similar to the first value of the tuple (e.g. A or B) and the program will check if this value exists and print back the whole nested value if it does? Example:

Input: A

Output: A Apple 14.5

Is there also a way it can be modified so the user can input multiple values and it checks and prints them all? For example:

Input: A, B

Output:

    A Apple 14.5
    B Banana 23.4

EDIT: Maybe I need to be a bit clearer. I've got code that gets the input and converts it to a list of multiple entries. I've then got a line which prints the 3 values of the tuple the way I need it. All I need it way to join these two parts together.

Here is my code so far:

import shares
portfolio_str=input("Please list portfolio: ")
portfolio_str= portfolio_str.replace(' ','')
portfolio_str= portfolio_str.upper()
portfolio_list= portfolio_str.split(',')
print(portfolio_list)
print()
print('{:<6} {:<20} {:>8}'.format('Code', 'Name', 'Price'))
data=shares.EXCHANGE_DATA
for  (code, name, share_value) in data:
    if code == portfolio_list[]:
            print('{:<6} {:<20} {:>8.2f}'.format(code, name, share_value))
    else:
            print("Failure")

As you can see I'm using a module called shares containing a list of tuples called EXCHANGE_DATA which is set out like this:

 EXCHANGE_DATA = [('AIA', 'Auckair', 1.50),
             ('AIR', 'Airnz', 5.60),
             ('AMP', 'Amp',3.22), 
             ('ANZ', 'Anzbankgrp', 26.25),
             ('ARG', 'Argosy', 12.22),
             ('CEN', 'Contact', 11.22),
             ('CNU', 'Chorus',3.01),
             ('DIL', 'Diligent', 5.3),
             ('DNZ', 'Dnz Property', 2.33),
             ('EBO', 'Ebos', 1.1),

Now how do I get it to check against any value in the user input and print any of them?

Upvotes: 0

Views: 1682

Answers (1)

Marius
Marius

Reputation: 60160

Getting a record given some input "A" or "B" is pretty simple:

alpha_data = [('A', 'Apple', 14.5), ('B', 'Banana', 23.4)]

user_input = "A"

[rec for rec in alpha_data if rec[0] == user_input]
Out[4]: [('A', 'Apple', 14.5)]

But if your goal is to allow users to access those entries using "A", "B" etc., then a dict seems like the more sensible solution:

alpha_dict = {"A": ("Apple", 14.5), "B": ("Banana", 23.4)}

user_input = "A"

alpha_dict[user_input]
Out[8]: ('Apple', 14.5)

Applying these ideas to your actual use case is pretty simple, in your attempt you iterated through the data and tried to find matches in the portfolio_list keys, but it's easier to iterate through portfolio_list and look for matches in the data:

EXCHANGE_DATA = [('AIA', 'Auckair', 1.50),
            ('AIR', 'Airnz', 5.60),
            ('AMP', 'Amp',3.22), 
            ('ANZ', 'Anzbankgrp', 26.25),
            ('ARG', 'Argosy', 12.22),
            ('CEN', 'Contact', 11.22),
            ('CNU', 'Chorus',3.01),
            ('DIL', 'Diligent', 5.3),
            ('DNZ', 'Dnz Property', 2.33),
            ('EBO', 'Ebos', 1.1)]

portfolio_str=input("Please list portfolio: ")
portfolio_str= portfolio_str.replace(' ','')
portfolio_str= portfolio_str.upper()
portfolio_list= portfolio_str.split(',')

Enter some user input:

Please list portfolio: AIA, ARG

Printing code:

print('{:<6} {:<20} {:>8}'.format('Code', 'Name', 'Price'))
Code   Name                    Price

for code in portfolio_list:
    # This assumes there will only be one record per code
    rec = [rec for rec in EXCHANGE_DATA if rec[0] == code][0]
    # The *rec part is using a neat python feature called
    # tuple unpacking, google it if you're not familiar with it
    print('{:<6} {:<20} {:>8.2f}'.format(*rec))

Output:

AIA    Auckair                  1.50
ARG    Argosy                  12.22

Upvotes: 4

Related Questions