开始使用免费开始使用

查找最高股价对应的股票

另一个有用的列表方法是 .index(),它会返回指定元素的索引。例如,要获取 x 中 2 的索引,您可以这样写:

x = [1, 2, 3, 4]
x.index(2)

1

随后,您可以用这个结果去索引(子集化)另一个列表。本练习将让您这样做。

工作区中已提供列表 pricesnames

本练习是课程的一部分

Python 金融入门

查看课程

练习说明

  • 找出 max_price 在列表 prices 中的索引。
  • 使用该索引在列表 names 中定位股价最高的公司。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Do not modify this
max_price = max(prices)

# Identify index of max price
max_index = prices.____(____)

# Identify the name of the company with max price
max_stock_name = names[____]

# Fill in the blanks 
print('The largest stock price is associated with ' + max_stock_name + ' and is $' + str(max_price) + '.')
编辑并运行代码