Wednesday 15 August 2012

python - Comparing user input with tuple value -


In Python 3, I have a list of Tuples with different records, say that it has alpha_data

Code>. They are kept as follows: [('A', 'Apple', 14.5), ('B', 'Banana', 23.4), etc.]

Is there any way that users can input Is the value of Tupal's first value (such as A or B) and the program will check whether this value is present and if it does, then print the whole nested value back? Example:

Input: A

Output: An apple 14.5

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

Input: A, B

Output:

  one Apple 14.5b banana 23.4   

EDIT: Maybe I need to be a bit clearer. I have a code that receives the input and changes it into a list of several entries. I've got a line that prints 3 types of tubal the way I need it. I need to join these two parts together.

This is my code yet: Import shares portfolio_str = input ("Please list portfolio:") portfolio_str (portfolio_list) print () print ('{: & Lt; 6} {: & lt; 20}} = 'Portfolio_Stell (' ',' ') Portfolio_State = Portfolio_Strap. In data: (code, name, share_value) for data = shares.EXCHANGE_DATA (code', 'name' , 'Price')). Format (code, name, share_value)): print ("Failure"); print ('{: & lt; 6} {: & lt; 20} {: & gt; 8.2f} ) As you can see, I am using a module that holds a list of shares of exchanges called Eu data, which is defined in this way:

  EXCHANGE_DATA = [('AIA', 'Okayer', 1.50), ('AIR', 'airs', 5.60), ('AMP', 'amp', 3.22) ',' Anabbankgroup ', 26.25), (' ARG ',' ergonji ', 12.22) CN,' Contact ', 11.22), (' CNU ' ('EBOS', 'Eboss', 1.1), 'CORS', 3.01), ('DIL', 'hard working', 5.3), ('dnz', 'dng property', 2.33),  Pre> 

Now how do I check it against any value in user input and print any of these?

is very easy:
  alpha_data = [[ R [0] == User_Input] Out [4]: ​​RE for rec for 'A', 'Apple', 14.5, ('B', 'banana', 23.4)] user_input = "A" [RE But if your goal is to allow users to use code that is  "A" code> ([A ',' Apple ', 14.5]]  ,  "B"  etc., then a  dict  looks like a more sensible solution:  
  alpha_dict = {"A": (" Apple ", 14.5)," B ": (" banana ", 23.4)} user_ The input = "A" alpha_dict [user_input] out [8]: ('apple', 14.5)   

Applying these ideas to your actual use is very simple, in your effort You have sneaked through the data and tried to find matches in the portfolio_list key, but it is easy to iterate through the portfolio_li cents and find matches in the data :

  EXCHANGE_DATA = [('AIA', 'Okayer', 1.50), ('air', 'airs', 5.60), ('cn', 'contact', 11.22)('' DJ '', 'DNJ Property', 2.33) '(' AMP ',' AMP ', 3.22), (' ANZ ',' Angbanggroup ', 26.25), (' ARG ',' ERGOSI ', 12.22) Portfolio_str = input (CNU ',' chorus', 3.01), ('DIL', 'hard working', 5.3) ("Please list portfolio:") portfolio_str = portfolio_str.replace ('EBO', 'eboss', 1.1) ',' ') Portfolio_str = portfolio_str.upper () portfolio_list = portfolio_str.split (', ')   

Enter some user input:

  please List Portfolio: AIA, ARG   

Printing code:

  print ('{:: & lt; 6} {: & lt; 20} {: & gt; 8} '. The value (code, name, value) for the code code code in the format portfolio_list: # It assumes that only one record per code rec = [rec] [rec] [rec] [rec] [REC] REC [0] == code] [0] # REC part is using a clean python feature called #tuplal unpacking if you are not familiar with it (print '{: & lt; 6} {: & Lt; 20} {: & gt; 8.2f} '. Format (* REC))   

Output:

  AIAAC 1.50 RG Argosy 12.22    

No comments:

Post a Comment