简体   繁体   English

不确定如何在元组列表中引用元组中的元素

[英]Unsure how to refer to elements in tuples in a list of tuples

I am trying to complete a beginner assignment that entails referencing elements in tuples in a list that uses for loops and conditionals to output one of two types of strings depending on the values in the tuples.我正在尝试完成一个初学者任务,该任务需要在列表中引用元组中的元素,该列表使用 for 循环和条件来根据元组中的值输出两种类型的字符串之一。

Using a for loop and an if statement, go through vacc_counties and print out a message for those counties that have a higher than 30% vaccination rate.使用 for 循环和 if 语句,通过 vacc_counties 并为那些疫苗接种率高于 30% 的县打印一条消息。

Add another loop that prints out a message for every county, but prints different messages if the rate is above or below 30.添加另一个循环,为每个县打印一条消息,但如果比率高于或低于 30,则打印不同的消息。

Example:例子:
Benton County is doing ok, with a rate of 41.4%
Fulton County is doing less ok, with a rate of 22.1%

Here is the list of tuples followed by my own code:这是元组列表,后面是我自己的代码:

vacc_counties = [('Pulaski', 42.7), ('Benton', 41.4), ('Fulton', 22.1), ('Miller', 9.6),
                 ('Mississippi', 29.4), ('Scotty County', 28.1)]

for tuple in vacc_counties:
    for element in tuple:
        if [1] < 30:
            print(f"{vacc_counties[0]}is doing ok, with a rate of" [1]"%")
        else [1] n > 30:
            print(f"{vacc_counties[0]}is doing ok, with a rate of" [1]"%")

Remarks:评论:

  • don't use reserved words for variable names, eg use tpl rather than tuple不要为变量名使用保留字,例如使用tpl而不是tuple
  • remove the for element in tuple: loop删除for element in tuple:中的for element in tuple:循环
  • to access second element of tuple, use tpl[1] instead of [1]要访问元组的第二个元素,请使用tpl[1]而不是[1]
  • use elif instead else使用elif代替else

Corrected code:更正的代码:

vacc_counties = [
    ("Pulaski", 42.7),
    ("Benton", 41.4),
    ("Fulton", 22.1),
    ("Miller", 9.6),
    ("Mississippi", 29.4),
    ("Scotty County", 28.1),
]

for tpl in vacc_counties:
    if tpl[1] < 30:
        print(f"{tpl[0]} is doing less ok, with a rate of {tpl[1]}%")
    elif tpl[1] >= 30:
        print(f"{tpl[0]} is doing ok, with a rate of {tpl[1]}%")

Prints:印刷:

Pulaski is doing ok, with a rate of 42.7%
Benton is doing ok, with a rate of 41.4%
Fulton is doing less ok, with a rate of 22.1%
Miller is doing less ok, with a rate of 9.6%
Mississippi is doing less ok, with a rate of 29.4%
Scotty County is doing less ok, with a rate of 28.1%

You can let Python unpack the two values in each tuple into easy to use variables like this:您可以让 Python 将每个元组中的两个值解包为易于使用的变量,如下所示:

vacc_counties = [('Pulaski', 42.7), ('Benton', 41.4), ('Fulton', 22.1), ('Miller', 9.6),
                 ('Mississippi', 29.4), ('Scotty County', 28.1)]

for county, rate in vacc_counties:
    if int(rate) > 30:
        print(f"{county} has a higher that 30% vaccination rate")

print()
for county, rate in vacc_counties:
    if int(rate) > 30:
        print(f"{county} is doing ok, with a rate of {rate}%")
    else:
        print(f"{county} is doing less ok, with a rate of {rate}%")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM