簡體   English   中英

Python - 列表和用戶輸入的列表(用戶從更大的列表中選擇位置,然后打印帶有所選子列表元素的 f 字符串)

[英]Python - lists of lists and user input (user selects position from a bigger list, then gets print of an f string with elements of a chosen sub - list)

這是我在這里的第一個問題,我正處於編程課程的開始階段。 我真的試圖自己找到解決方案,然后在互聯網上尋找類似的東西,但似乎我什至不知道如何正確命名(非常基本的)問題,所以谷歌搜索沒有給我任何結果。

我有任務要做。 該任務的一部分是:我有一個列表列表,它是世界上最富有的人的列表以及有關他們的其他數據。 它有 101 個位置,如下所示:

[['Rank', 'Name', 'Total Net Worth', '$ Last Change', '$ YTD Change', 'Country', 'Industry'], ['1', 'Jeff Bezos', '$188B', '+$1.68B', '-$2.31B\xa0', 'United States', 'Technology'], 
['2', 'Elon Musk', '$170B', '-$2.89B', '+$773M\xa0', 'United States',

等等。 我的目標是要求用戶輸入 int 值(1 到 100 之間的數字),然后向他顯示在排名中具有該位置的人的數據,如下所示:

position = int(input("Hello, this is Top 100 Richest People List. Press number "\
f"from 1 to 100 to see what billionaire is on that position in the ranking."))

#stuff happens#
    
print(f"The billionaire with a position number ... is ... . Total Net Worth   "\
f"amounts to ... $. Last change amounts to ...$. YTD change is ... .          "\
f"The country of origin is ... and the industry is ... .") 

我不知道如何將用戶輸入映射到列表中的選擇,然后在 f 字符串中打印該子列表的內容。 因此,我懇請您尋求一些幫助、提示或至少一些有用的教程“列表和列表 + 用戶輸入”。

謝謝

這是你要找的嗎?

l = [['Rank', 'Name', 'Total Net Worth', '$ Last Change', '$ YTD Change', 'Country', 'Industry'], ['1', 'Jeff Bezos', '$188B', '+$1.68B', '-$2.31B\xa0', 'United States', 'Technology'], 
['2', 'Elon Musk', '$170B', '-$2.89B', '+$773M\xa0', 'United States', ...

position = int(input("Hello, this is Top 100 Richest People List. Press number "\
f"from 1 to 100 to see what billionaire is on that position in the ranking."))

try:
 found_person = l[position]
 
 print(f"The billionaire with a position number {position} is {found_person[1]} . Total Net Worth  
 "\
 f"amounts to {found_person[2]} $. Last change amounts to {found_person[3]}$. YTD change is {found_person[4]} .          
 "\
 f"The country of origin is {found_person[5]} and the industry is {found_person[6]} .") 

except IndexError:
 print('Cannot find any rich people with given position-index!')

分解代碼:

  1. l是我們初始化列表的地方(正如您在問題描述中提到的)

  2. position - 列表中人員的索引。 基本上是初始列表中子列表的索引

  3. 我們try - except條件以處理IndexError錯誤。 如果在初始列表中找不到具有用戶給定索引的子列表,則會出現此錯誤

  4. found_person是找到與此人相關的子列表

  5. found_person之后的print ,我們通過簡單地尋址每條信息(找到的子列表中的每個元素)的索引來打印有關此人的最終信息。 我們可以通過將這些元素(即found_person[1]found_person[2]等)包含在字符串內的大括號{ }並將f放在它的前面來打印它們(就像在問題描述中一樣)

為了給你建議 這只是您正在尋找的示例 我試圖讓它簡單明了。

persons = [['Name', 'Age', 'hair Color'],
           ['Alice', '25', 'blonde'],
           ['Bob', '33', 'black'],
           ['Ann', '18', 'purple']]

position = int(input("Hello, this is list of 3 persons. "\
f"Press number to get information about it:"))

Name= persons[position][0] # get data from 1st column 
Age=  persons[position][1] # get data from 2nd column 
Color=persons[position][2] # get data from 3rd column 


print(f"The person with a position number {position} is {Name}. Has {Age} old."\
f" They have a {Color} hair color.") 

我不得不修改你的建議,因為我忘了提到我導入 .csv 並且搞砸了一些東西,但你們仍然非常幫助我,謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM