简体   繁体   English

如何从基于3个列表的特定索引输出输出(所有3个列表在相同位置上)

[英]How do I print out an output from a specific index based upon 3 Lists (same position on list for all 3)

I am trying to create a program where there are 3 Lists that will print out the name, wage, and total wage based upon their index location on the list. 我正在尝试创建一个程序,其中有3个列表,这些列表将根据列表中的索引位置打印出姓名,工资和总工资。

The length of each variable cannot just be 6, so it should accommodate any input given (name/wage/hour list could as long as desired). 每个变量的长度不能仅为6,因此它应该容纳任何给定的输入(名称/工资/小时列表可以根据需要设置)。 Ignore data validation/formatting as we assume that the user will input the info correctly all the time for all the variables. 忽略数据验证/格式设置,因为我们假设用户将始终为所有变量正确输入信息。

For example, I want my desired output to be (see index 0 in the list of the 3 variables in the code): 例如,我希望得到想要的输出(请参见代码中3个变量的列表中的索引0):

Sanchez worked 42.0 hours at $10.00 per hour, and earned $420.00

$420.00 -> (10.00*42.0) $ 420.00->(10.00 * 42.0)

Currently here is my code: 目前这是我的代码:

name = ['Sanchez', 'Ruiz', 'Weiss', 'Choi', 'Miller', 'Barnes']
wage = ['10.0', '18', '14.80', '15', '18', '15']
hours = [42.0, 41.5, 38.0, 21.5, 21.5, 22.5]


i = 0
for y in name:

    payOut = float(wage[i]) * float(hours[i])
    product = (name[i], wage[i], payOut)
    i += 1
    print(product)


empName = input("gimmie name: ") #no data validation neeeded

def target(i,empName):
    for x in i:
    if x == str(empName):
      empName = x #should return index of the empName
'''^^^ this is currently incorrect too as i believe it should return
the index location of the name, which I will use to print out the
final statement based upon the location of each respective variable 
list. (not sure if this works)'''



target(name,empName)

You can simply use the list.index() method for the list. 您可以简单地将list.index()方法用于列表。 No need to iterate through everything. 无需遍历所有内容。

emp_name = input("gimmie name: ") # Weiss

idx = name.index(emp_name) # 99% of the work is done right here.

print('{n} worked {h} hours at ${w:.2f} per hour, and earned ${p:.2f}'.format(
    n = name[idx], 
    h = hours[idx],
    w = float(wage[idx]), # Convert to float so you can show 2 decimals for currency
    p = float(wage[idx]) * hours[idx] # Calculate pay here
))

#Weiss worked 38.0 hours at $14.80 per hour, and earned $562.40

You could use [Python 3]: enumerate ( iterable, start=0 ) : 您可以使用[Python 3]: 枚举可迭代,start = 0

names = ['Sanchez', 'Ruiz', 'Weiss', 'Choi', 'Miller', 'Barnes']
wages = ['10.0', '18', '14.80', '15', '18', '15']
hours = [42.0, 41.5, 38.0, 21.5, 21.5, 22.5]


def name_index(name_list, search_name):
    for index, item in enumerate(name_list):
        if item == search_name:
            return index
    return -1


emp_name = input("gimmie name: ")

idx = name_index(names, emp_name)
if idx == -1:
    print("Name {:s} not found".format(emp_name))
else:
    wage = float(wages[idx])
    hour = hours[idx]        
    print("{:s} worked {:.2f} hours at $ {:.2f} per hour earning $ {:.2f} ".format(names[idx], hour, wage, wage * hour))

If your vectors have the same lenght, you case use range . 如果向量的长度相同,则可使用range

for i in range(0,len(name),1):
    payOut = float(wage[i]) * float(hours[i])
    product = (name[i], wage[i], payOut)
    print(product)

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

相关问题 如何打印此列表列表? - How do I print out this list of lists? 我如何打印出所有列表 - How do I print out all the list 如何打印具有特定质量的列表? - How do I print out lists with a specific quality? 如何计算列表列表中某个位置的特定元素的出现次数? - How do I count occurrences of a specific element in a position in a list of lists? 如何从操作循环中以列表形式获取打印输出?,或者,在不打印的情况下获得相同的结果? - How do I get a print output as a list from a manipulated loop?, Or, get same result without print? 如果最大的数字在同一个列表中多次出现,我如何将它们全部打印出来? - If the largest number occurs more than once in the same list, how do I print them all out? 如何使用Jinja 2在HTML中以一行显示来自多个python列表中同一索引位置的值? - How do i display values from the same index position from multiple python lists, on 1 line, in HTML using Jinja 2? 如何根据索引位置将列表列表连接到另一个小列表? - How to connect a list of lists to another small list based on index position? 如何从列表中查找并打印出特定值(至少我认为那是我想做的事?) - How to find and print out of a specific value from a list (at least i think thats what i want to do?) 如何从列表列表中打印第一行? - How do I print the first row from a list of lists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM