Python 如何使用dictionary在两个不同的数组中存储多个值

Python 如何使用dictionary在两个不同的数组中存储多个值,python,json,beautifulsoup,Python,Json,Beautifulsoup,我想在两个不同的列表中存储INFOSYS和RELIANCE衍生品报价的最后交易价格值。之后,我希望我的程序从相应的列表中减去两个最新的值,并将输出作为值之间的差值。给定的代码为一个衍生报价提供输出 如何使用单个代码从多个列表中为我提供所需的输出?我能用字典解决这个问题吗 import requests import json import time from bs4 import BeautifulSoup as bs import datetime, threading LTP_arr=[

我想在两个不同的列表中存储INFOSYS和RELIANCE衍生品报价的最后交易价格值。之后,我希望我的程序从相应的列表中减去两个最新的值,并将输出作为值之间的差值。给定的代码为一个衍生报价提供输出

如何使用单个代码从多个列表中为我提供所需的输出?我能用字典解决这个问题吗

import requests
import json
import time
from bs4 import BeautifulSoup as bs
import datetime, threading


LTP_arr=[0]
url = 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&expiry=27JUN2019&type=-&strike=-'

def ltpwap():
    resp = requests.get(url)
    soup = bs(resp.content, 'lxml')
    data = json.loads(soup.select_one('#responseDiv').text.strip())

    LTP=data['data'][0]['lastPrice']
    n2=float(LTP.replace(',', ''))

    LTP_arr.append(n2)
    LTP1= LTP_arr[-1] - LTP_arr[-2]

    print("Difference between the latest two values of INFY is ",LTP1)
    threading.Timer(1, ltpwap).start()

ltpwap()
产生:

INFY的最新两个值之差为4。 预期结果是:

INFY_list = (729, 730, 731, 732, 733)
RELIANCE_list = (1330, 1331, 1332, 1333, 1334)    

比保留一些列表更好的方法是只生成生成器,以一定的间隔从URL生成所需的值。下面是使用
time.sleep()
的实现,但对于许多URL,我建议查看
asyncio

from bs4 import BeautifulSoup as bs
import json
import requests
from collections import defaultdict
from time import sleep

urls_to_watch = {
    'INFOSYS': 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&expiry=27JUN2019&type=-&strike=-',
    'RELIANCE': 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=RELIANCE&instrument=FUTSTK&expiry=27JUN2019&type=-&strike=-'
}

def get_values(urls):
    for name, url in urls.items():
        resp = requests.get(url)
        soup = bs(resp.content, 'lxml')
        data = json.loads(soup.select_one('#responseDiv').text.strip())

        LTP=data['data'][0]['lastPrice']
        n2=float(LTP.replace(',', ''))
        yield name, n2

def check_changes(urls):
    last_values = defaultdict(int)
    current_values = {}
    while True:
        current_values = dict(get_values(urls))
        for name in urls.keys():
            if current_values[name] - last_values[name]:
                yield name, current_values[name], last_values[name]
        last_values = current_values
        sleep(1)

for name, current, last in check_changes(urls_to_watch):
    # here you can just print the values, or store current value to list
    # and periodically store it to DB, etc.
    print(name, current, last, current - last)
印刷品:

INFOSYS 750.7 0 750.7
RELIANCE 1284.4 0 1284.4
RELIANCE 1284.8 1284.4 0.3999999999998636
INFOSYS 749.8 750.7 -0.900000000000091
RELIANCE 1285.4 1284.8 0.6000000000001364

...and waits infinitely for any change to occur, then prints it.

比保留一些列表更好的方法是只生成生成器,以一定的间隔从URL生成所需的值。下面是使用
time.sleep()
的实现,但对于许多URL,我建议查看
asyncio

from bs4 import BeautifulSoup as bs
import json
import requests
from collections import defaultdict
from time import sleep

urls_to_watch = {
    'INFOSYS': 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=INFY&instrument=FUTSTK&expiry=27JUN2019&type=-&strike=-',
    'RELIANCE': 'https://nseindia.com/live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=RELIANCE&instrument=FUTSTK&expiry=27JUN2019&type=-&strike=-'
}

def get_values(urls):
    for name, url in urls.items():
        resp = requests.get(url)
        soup = bs(resp.content, 'lxml')
        data = json.loads(soup.select_one('#responseDiv').text.strip())

        LTP=data['data'][0]['lastPrice']
        n2=float(LTP.replace(',', ''))
        yield name, n2

def check_changes(urls):
    last_values = defaultdict(int)
    current_values = {}
    while True:
        current_values = dict(get_values(urls))
        for name in urls.keys():
            if current_values[name] - last_values[name]:
                yield name, current_values[name], last_values[name]
        last_values = current_values
        sleep(1)

for name, current, last in check_changes(urls_to_watch):
    # here you can just print the values, or store current value to list
    # and periodically store it to DB, etc.
    print(name, current, last, current - last)
印刷品:

INFOSYS 750.7 0 750.7
RELIANCE 1284.4 0 1284.4
RELIANCE 1284.8 1284.4 0.3999999999998636
INFOSYS 749.8 750.7 -0.900000000000091
RELIANCE 1285.4 1284.8 0.6000000000001364

...and waits infinitely for any change to occur, then prints it.

我完全听不懂这个问题。请提供预期结果如何在不同的多个列表中存储多个凭条的值预期结果:INFY_列表=(7297307331732733)RELIANCE_列表=(1330133113334)请不要在注释中发布代码,你的问题是,我如何利用字典将值存储在两个不同的列表中,这两个列表包含动态变化的票据的最后一个价格?我根本无法理解这个问题。请提供预期结果如何在不同的多个列表中存储多个凭条的值预期结果:INFY_列表=(7297307331732733)RELIANCE_列表=(1330133113334)请不要在注释中发布代码,你的问题是,我如何利用字典将值存储在两个不同的列表中,其中包含动态变化的票据的最后一个价格?