提问人:hosein moradi.k 提问时间:10/28/2023 最后编辑:mkrieger1hosein moradi.k 更新时间:10/29/2023 访问量:50
如何在函数python中找到列表的索引
How can I find an index of list in function python
问:
我在我的项目中制作了许多函数,在一个函数中,我需要找到另一个列表的一个列表的索引,但它没有执行并显示错误。
def name():
Name = []
while True:
Name.append(input('<Department Electricity,District 1,Tehran>\n Hello dear customer\n enter your name: '))
if input('press s to stop: ') == 's':
break
return Name
def expense(get_name):
expense = []
for x in get_name:
expense.append(int(input(f' dear {x} insert your monthly electricity expense: ')))
return expense
def average(Expense):
avg = sum(Expense) / len(Expense)
return avg
def result(name, expense, average):
for x in expense:
if x > average:
index = expense.index[x]
print(name[index], 'is high expense')
Name = name()
Expense = expense(Name)
result(Name, Expense, average(Expense))
答:
0赞
Hossein Koofi
10/28/2023
#1
您不必在“result”函数中对每个循环使用。 确保您也能够使用常规的 for 循环。
def result(name, expense, average):
for i in range(len(expense)):
if expense[i] > average:
print(name[i], 'is high expense')
-1赞
Arthur
10/29/2023
#2
in result 函数 u 使用 expense.index[x],但 expense.index 不是一个列表,而是 s 函数,所以 使用 expense.index(x)
评论
for index, x in emumerate(expense):
expense