Saturday 15 February 2014

python - Tuples in unwanted alphabetical order -


In Python 3 I have a program as below, it basically takes an input from the user and it Examines against a list of TUPS (exchang'data) and outputs a list of information. The problem is that it is sorted alphabetically when this output is done and not the way I want it. How do I fix it? EXCHANGE_DATA portfolio_str = input ("Please give a portfolio list") import_ import from portfolio_str = portfolio_str.replace ('', '') portfolio_str = portfolio_str.upper () Portfolio_Lit = PortfoliosStatus (',' '} Print () print (' {:: & lt; 6} {: & lt; 20} {: & gt; 8} 'format (' code ',' name ',' value ')) for code, share_name , EXCHANGE_DATA in the share_value: if the portfolio_list in the code: print ('{: & lt; 6} {: & lt; 20} {: & gt; 8.2f}' format (code, share_name, share_wua))

Example Input: GPG, HNZ, DIL, FRE

Output Low Please list: Portfolio listing: GPG, HNZ, DL, FRE code name Price GPG GuinnessPit 2.32 HNZ Heartland N85 3.85 DIL Hard Work 5.30 FR freeway 6.71 < P> But I'm getting:

  Please list the portfolio: GPG, HNZ, DIL, FRE code name value DIL hard working 5.30 FR freeway 6.71 GPG guinnesspit 2.32 HNZ Hartland NJ 3.85   < EXCHANGE_DATA POR Tfolio_str = input ("Please list portfolio:") portfolio_str = portfolio_str.replace ('', '') portfolio_str 

updated code

  = Portfolio_str.upper () portfolio_list = portfolio_str.split (',') print () print ('{:: :: & lt; 6} {: & lt; 20} {: & lt; 20} {}: code for EXCHANGE_DATA = {code: (share_name, share_value), share_name, share_value EXCHANGE_DATA} code for portfolio_lit: share_name, share_value = exchang_data [code] for code (code ',' name ',' value ')) ] Print ('{:: & lt; 6} {: & lt; 20} {: & gt; 8.2 F}'. Format (Code, Share_Name, Share_WA))  Using , < Code> EXCHANGE_DATA  is converted to a dictionary   

code form of key Then you will be able to change your loop:

  Portfolio for code In the yo-list: share_name, share_value = EXCHANGE_DATA [code] print ('{: & lt; 6} {: & lt; 20} {: & gt; 8.2f}' format (code, share_name, share_value))    

No comments:

Post a Comment