[英]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)
this is my first question here ever, I am on the very beginnin of my programming course.这是我在这里的第一个问题,我正处于编程课程的开始阶段。 I really tried to find a solution myself and then look for something similar in the internet, but it seems I don't even know how to name the (extremely basic) problem properly, so google search gives me no results.
我真的试图自己找到解决方案,然后在互联网上寻找类似的东西,但似乎我什至不知道如何正确命名(非常基本的)问题,所以谷歌搜索没有给我任何结果。
I have a task to do.我有任务要做。 Part of that task is: I have a list of lists, which is a list of richest people in the world and additional data about them.
该任务的一部分是:我有一个列表列表,它是世界上最富有的人的列表以及有关他们的其他数据。 It has 101 positions and looks like this:
它有 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',
and so on.等等。 My goal is to ask user for int input (a number between 1 and 100) and then display him data of a person who has that position in the ranking, like this:
我的目标是要求用户输入 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 ... .")
I have no idea how to map user input into the selection from a list, and then print the content just of that sub - list in the f string.我不知道如何将用户输入映射到列表中的选择,然后在 f 字符串中打印该子列表的内容。 So I kindly ask for some help, hints, or at least some tutorial "lists and lists + user inputs" which would be useful.
因此,我恳请您寻求一些帮助、提示或至少一些有用的教程“列表和列表 + 用户输入”。
Thank you谢谢
Is this what you're looking for?这是你要找的吗?
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!')
Breaking down the code:分解代码:
l
is where we initialize the list (as you've mentioned in your question description) l
是我们初始化列表的地方(正如您在问题描述中提到的)
position
- index of the person in the list. position
- 列表中人员的索引。 Basically index of a sublist in the initial list基本上是初始列表中子列表的索引
we have try
- except
condition in order to handle IndexError
error.我们
try
- except
条件以处理IndexError
错误。 This error can appear if a sublist with a given index by the user is not found in the initial list如果在初始列表中找不到具有用户给定索引的子列表,则会出现此错误
found_person
is found sublist which is related to the person found_person
是找到与此人相关的子列表
in the print
after found_person
we are printing the final information about the person by simply addressing the index of every piece of information (every element in the found sublist).在
found_person
之后的print
,我们通过简单地寻址每条信息(找到的子列表中的每个元素)的索引来打印有关此人的最终信息。 We can print them out by including these elements (ie found_person[1]
, found_person[2]
and so on) in curly brackets { }
inside the string and putting f
in front of it (just like in the question description)我们可以通过将这些元素(即
found_person[1]
、 found_person[2]
等)包含在字符串内的大括号{ }
并将f
放在它的前面来打印它们(就像在问题描述中一样)
In order to advise you.为了给你建议。 This is just an Example of what you looking for.
这只是您正在寻找的示例。 I'm trying to make it simple and clear.
我试图让它简单明了。
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.