我一直在做一個專案到學校。在這一點上,我有一個包含多個串列的串列,其中包含有關我從金融網站抓取的特定股票的資訊。這是名為 csv_data 的串列變數:
csv_data = [['Table', 'Symbol', 'Company', 'OPEN', 'PREVIOUS CLOSE', 'VOLUME', 'MARKET CAP'], ['Most Actives', 'F', 'Ford Motor Co', '19.67', '19.22', '38,654,707', '$79.4B'], ['Most Actives', 'CCL', 'Carnival Corp', '19.00', '18.59', '29,271,792', '$17.6B'], ['Most Actives', 'T', 'AT&T Inc', '23.29', '23.28', '25,784,807', '$164.6B'], ['Most Actives', 'BAC', 'Bank of America Corp', '44.60', '44.15', '15,844,588', '$367.4B'], ['Most Actives', 'PFE', 'Pfizer Inc', '51.29', '51.48', '15,897,466', '$297.7B'], ['Most Actives', 'NCLH', 'Norwegian Cruise Line Holdings Ltd', '20.78', '20.03', '14,695,511', '$8.0B'], ['Most Actives', 'C', 'Citigroup Inc', '63.09', '62.52', '12,859,826', '$126.6B'], ['Most Actives', 'VZ', 'Verizon Communications Inc', '51.00', '51.07', '11,762,158', '$210.0B'], ['Most Actives', 'TWTR', 'Twitter Inc', '45.08', '44.47', '10,301,235', '$34.1B'], ['Most Actives', 'KO', 'Coca-Cola Co', '54.91', '54.91', '10,240,325', '$229.2B'], ['Gainers', 'AZO', 'Autozone Inc', '1,905.65', '1,879.99', '135,103', '$39.2B'], ['Gainers', 'DVN', 'Devon Energy Corp', '43.21', '42.36', '5,149,907', '$28.2B'], ['Gainers', 'NOW', 'ServiceNow Inc', '627.10', '616.54', '470,638', '$124.1B'], ['Gainers', 'MRO', 'Marathon Oil Corp', '15.91', '15.60', '6,315,893', '$12.1B'], ['Gainers', 'HES', 'Hess Corp', '77.63', '77.16', '729,681', '$23.9B'], ['Gainers', 'CMG', 'Chipotle Mexican Grill Inc', '1,661.14', '1,623.46', '142,430', '$45.3B'], ['Gainers', 'JNPR', 'Juniper Networks Inc', '32.07', '31.45', '1,864,313', '$10.1B'], ['Gainers', 'EMN', 'Eastman Chemical Co', '111.35', '109.34', '540,451', '$14.1B'], ['Gainers', 'FCX', 'Freeport-McMoRan Inc', '38.28', '37.37', '7,933,903', '$54.5B'], ['Gainers', 'MOS', 'Mosaic Co', '35.15', '34.47', '2,467,991', '$13.1B'], ['Losers', 'WU', 'Western Union Co', '17.84', '17.85', '3,847,445', '$6.9B'], ['Losers', 'MKC', 'McCormick & Company Inc', '89.85', '90.11', '924,086', '$21.7B'], ['Losers', 'CLX', 'Clorox Co', '167.79', '168.73', '488,732', '$20.5B'], ['Losers', 'GNRC', 'Generac Holdings Inc', '375.93', '366.68', '895,094', '$25.6B'], ['Losers', 'MRK', 'Merck & Co Inc', '71.94', '73.42', '7,217,967', '$186.6B'], ['Losers', 'DG', 'Dollar General Corp', '227.05', '225.53', '658,546', '$50.4B'], ['Losers', 'LLY', 'Eli Lilly and Co', '246.21', '246.33', '1,070,621', '$237.1B'], ['Losers', 'VZ', 'Verizon Communications Inc', '51.00', '51.07', '11,813,210', '$210.0B'], ['Losers', 'CHD', 'Church & Dwight Co Inc', '93.82', '94.15', '601,514', '$22.2B'], ['Losers', 'T', 'AT&T Inc', '23.29', '23.28', '25,784,807', '$164.6B']]
我正在尋找包含用戶輸入的股票代碼的 csv_data 索引。
下面是我嘗試這樣做的代碼:
import re
x = list()
found_list = list()
user_symbol = input("Enter a stock: ")
for list_ in csv_data:
for string in list_:
x = re.findall(user_symbol, string)
if len(x) > 0:
break
if len(x) > 0:
print(list_)
abc = found_list.extend(list_)
break
print(found_list)
z = found_list.index(x)
print(z)
當我運行此代碼并為 user_symbol 輸入“F”時,出現此錯誤:
Enter a stock: F
['Most Actives', 'F', 'Ford Motor Co', '19.67', '19.22', '38,654,707', '$79.4B']
['Most Actives', 'F', 'Ford Motor Co', '19.67', '19.22', '38,654,707', '$79.4B']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-53-7435bef5228a> in <module>
28
29 print(found_list)
---> 30 z = found_list.index(x)
31
32 print(z)
ValueError: ['F'] is not in list
我很好奇為什么它說“F”不在 found_list 中,當我看到它列印在上面時。我可能錯誤地使用了 index() 并希望得到任何幫助!
uj5u.com熱心網友回復:
我正在尋找包含用戶輸入的股票代碼的 csv_data 索引。
像下面這樣的東西
csv_data = [['Table', 'Symbol', 'Company', 'OPEN', 'PREVIOUS CLOSE', 'VOLUME', 'MARKET CAP'], ['Most Actives', 'F', 'Ford Motor Co', '19.67', '19.22', '38,654,707', '$79.4B'], ['Most Actives', 'CCL', 'Carnival Corp', '19.00', '18.59', '29,271,792', '$17.6B'], ['Most Actives', 'T', 'AT&T Inc', '23.29', '23.28', '25,784,807', '$164.6B'], ['Most Actives', 'BAC', 'Bank of America Corp', '44.60', '44.15', '15,844,588', '$367.4B'], ['Most Actives', 'PFE', 'Pfizer Inc', '51.29', '51.48', '15,897,466', '$297.7B'], ['Most Actives', 'NCLH', 'Norwegian Cruise Line Holdings Ltd', '20.78', '20.03', '14,695,511', '$8.0B'], ['Most Actives', 'C', 'Citigroup Inc', '63.09', '62.52', '12,859,826', '$126.6B'], ['Most Actives', 'VZ', 'Verizon Communications Inc', '51.00', '51.07', '11,762,158', '$210.0B'], ['Most Actives', 'TWTR', 'Twitter Inc', '45.08', '44.47', '10,301,235', '$34.1B'], ['Most Actives', 'KO', 'Coca-Cola Co', '54.91', '54.91', '10,240,325', '$229.2B'], ['Gainers', 'AZO', 'Autozone Inc', '1,905.65', '1,879.99', '135,103', '$39.2B'], ['Gainers', 'DVN', 'Devon Energy Corp', '43.21', '42.36', '5,149,907', '$28.2B'], ['Gainers', 'NOW', 'ServiceNow Inc', '627.10', '616.54', '470,638', '$124.1B'], ['Gainers', 'MRO', 'Marathon Oil Corp', '15.91', '15.60', '6,315,893', '$12.1B'], ['Gainers', 'HES', 'Hess Corp', '77.63', '77.16', '729,681', '$23.9B'], ['Gainers', 'CMG', 'Chipotle Mexican Grill Inc', '1,661.14', '1,623.46', '142,430', '$45.3B'], ['Gainers', 'JNPR', 'Juniper Networks Inc', '32.07', '31.45', '1,864,313', '$10.1B'], ['Gainers', 'EMN', 'Eastman Chemical Co', '111.35', '109.34', '540,451', '$14.1B'], ['Gainers', 'FCX', 'Freeport-McMoRan Inc', '38.28', '37.37', '7,933,903', '$54.5B'], ['Gainers', 'MOS', 'Mosaic Co', '35.15', '34.47', '2,467,991', '$13.1B'], ['Losers', 'WU', 'Western Union Co', '17.84', '17.85', '3,847,445', '$6.9B'], ['Losers', 'MKC', 'McCormick & Company Inc', '89.85', '90.11', '924,086', '$21.7B'], ['Losers', 'CLX', 'Clorox Co', '167.79', '168.73', '488,732', '$20.5B'], ['Losers', 'GNRC', 'Generac Holdings Inc', '375.93', '366.68', '895,094', '$25.6B'], ['Losers', 'MRK', 'Merck & Co Inc', '71.94', '73.42', '7,217,967', '$186.6B'], ['Losers', 'DG', 'Dollar General Corp', '227.05', '225.53', '658,546', '$50.4B'], ['Losers', 'LLY', 'Eli Lilly and Co', '246.21', '246.33', '1,070,621', '$237.1B'], ['Losers', 'VZ', 'Verizon Communications Inc', '51.00', '51.07', '11,813,210', '$210.0B'], ['Losers', 'CHD', 'Church & Dwight Co Inc', '93.82', '94.15', '601,514', '$22.2B'], ['Losers', 'T', 'AT&T Inc', '23.29', '23.28', '25,784,807', '$164.6B']]
symbol = 'LLY'
found = False
for idx,e in enumerate(csv_data):
if e[1] == symbol:
print(f'{symbol} was found in index {idx}')
found = True
break
if not found:
print(f'Could not find the symbol {symbol}')
uj5u.com熱心網友回復:
對于任何看到這一點的人,我當然在提出問題后終于弄清楚了。
索引時,我使用 x 變數在變數 found_list 中進行搜索。
x 在這種情況下是型別串列,我改為使用 x[0] 來回傳字串值。
uj5u.com熱心網友回復:
正如錯誤所說['F']的不在串列中,這是正確的,因為它是一個單元素串列。
found_list沒有一個元素是一個包含一個元素的串列Ffound_list確實有一個字串F
那是因為re.findall回傳一個串列:found_list.index(x[0])給出1
user_symbol = "GNRC"
for idx, (_, symbol, *_) in enumerate(csv_data):
if symbol == user_symbol:
print(f'{user_symbol} was found in index {idx}')
break
else:
print(f'Could not find the symbol {user_symbol}')
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/375872.html
下一篇:從給定數字的串列中查找唯一元素
