開始使用免費開始

找出股價最高的股票

另一個實用的清單方法是 .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) + '.')
編輯並執行程式碼