Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python如何连接两个字典_Python_Dictionary_Concatenation - Fatal编程技术网

python如何连接两个字典

python如何连接两个字典,python,dictionary,concatenation,Python,Dictionary,Concatenation,在寻找了很长一段时间后,每个人都将答案指向了这个帖子: [ 但我希望将这两个词典连接起来,而不是合并。 我的两个字典都有相同的键,因此当我使用tempdict.update(data)时,它会替换我的数据,而不是将数据添加到其中。 字典如下所示: { "symbol" : "AAPL", "profile" : { "price" : 254.29, "beta" : "1.228499", "volAvg" : "49330169", "mktCap"

在寻找了很长一段时间后,每个人都将答案指向了这个帖子: [

但我希望将这两个词典连接起来,而不是合并。 我的两个字典都有相同的键,因此当我使用
tempdict.update(data)
时,它会替换我的数据,而不是将数据添加到其中。 字典如下所示:

{
  "symbol" : "AAPL",
  "profile" : {
    "price" : 254.29,
    "beta" : "1.228499",
    "volAvg" : "49330169",
    "mktCap" : "1.11264072E12",
    "lastDiv" : "2.92",
    "range" : "170.27-327.85",
    "changes" : -0.52,
    "changesPercentage" : "(-0.20%)",
    "companyName" : "Apple Inc.",
    "exchange" : "Nasdaq Global Select",
    "industry" : "Computer Hardware",
    "website" : "http://www.apple.com",
    "description" : "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
    "ceo" : "Timothy D. Cook",
    "sector" : "Technology",
    "image" : "https://financialmodelingprep.com/images-New-jpg/AAPL.jpg"
  }
}
{
  "companyProfiles" : [ {
    "symbol" : "AAPL",
    "profile" : {
      "price" : 254.29,
      "beta" : "1.228499",
      "volAvg" : "49330169",
      "mktCap" : "1.11264072E12",
      "lastDiv" : "2.92",
      "range" : "170.27-327.85",
      "changes" : -0.52,
      "changesPercentage" : "(-0.20%)",
      "companyName" : "Apple Inc.",
      "exchange" : "Nasdaq Global Select",
      "industry" : "Computer Hardware",
      "website" : "http://www.apple.com",
      "description" : "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
      "ceo" : "Timothy D. Cook",
      "sector" : "Technology",
      "image" : "https://financialmodelingprep.com/images-New-jpg/AAPL.jpg"
    }
  }, {
    "symbol" : "FB",
    "profile" : {
      "price" : 166.8,
      "beta" : "1.062394",
      "volAvg" : "21198830",
      "mktCap" : "4.75455062E11",
      "lastDiv" : "0",
      "range" : "137.1-224.2",
      "changes" : 0.85,
      "changesPercentage" : "(+0.51%)",
      "companyName" : "Facebook Inc.",
      "exchange" : "Nasdaq Global Select",
      "industry" : "Online Media",
      "website" : "http://www.facebook.com",
      "description" : "Facebook Inc is the world's largest online social network. Its products are Facebook, Instagram, Messenger, WhatsApp, and Oculus. Its products enable people to connect and share through mobile devices and personal computers.",
      "ceo" : "Mark Zuckerberg",
      "sector" : "Technology",
      "image" : "https://financialmodelingprep.com/images-New-jpg/FB.jpg"
    }
  } ]
}
我希望它看起来像这样:

{
  "symbol" : "AAPL",
  "profile" : {
    "price" : 254.29,
    "beta" : "1.228499",
    "volAvg" : "49330169",
    "mktCap" : "1.11264072E12",
    "lastDiv" : "2.92",
    "range" : "170.27-327.85",
    "changes" : -0.52,
    "changesPercentage" : "(-0.20%)",
    "companyName" : "Apple Inc.",
    "exchange" : "Nasdaq Global Select",
    "industry" : "Computer Hardware",
    "website" : "http://www.apple.com",
    "description" : "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
    "ceo" : "Timothy D. Cook",
    "sector" : "Technology",
    "image" : "https://financialmodelingprep.com/images-New-jpg/AAPL.jpg"
  }
}
{
  "companyProfiles" : [ {
    "symbol" : "AAPL",
    "profile" : {
      "price" : 254.29,
      "beta" : "1.228499",
      "volAvg" : "49330169",
      "mktCap" : "1.11264072E12",
      "lastDiv" : "2.92",
      "range" : "170.27-327.85",
      "changes" : -0.52,
      "changesPercentage" : "(-0.20%)",
      "companyName" : "Apple Inc.",
      "exchange" : "Nasdaq Global Select",
      "industry" : "Computer Hardware",
      "website" : "http://www.apple.com",
      "description" : "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
      "ceo" : "Timothy D. Cook",
      "sector" : "Technology",
      "image" : "https://financialmodelingprep.com/images-New-jpg/AAPL.jpg"
    }
  }, {
    "symbol" : "FB",
    "profile" : {
      "price" : 166.8,
      "beta" : "1.062394",
      "volAvg" : "21198830",
      "mktCap" : "4.75455062E11",
      "lastDiv" : "0",
      "range" : "137.1-224.2",
      "changes" : 0.85,
      "changesPercentage" : "(+0.51%)",
      "companyName" : "Facebook Inc.",
      "exchange" : "Nasdaq Global Select",
      "industry" : "Online Media",
      "website" : "http://www.facebook.com",
      "description" : "Facebook Inc is the world's largest online social network. Its products are Facebook, Instagram, Messenger, WhatsApp, and Oculus. Its products enable people to connect and share through mobile devices and personal computers.",
      "ceo" : "Mark Zuckerberg",
      "sector" : "Technology",
      "image" : "https://financialmodelingprep.com/images-New-jpg/FB.jpg"
    }
  } ]
}
这是我到目前为止得到的,但我知道问题出在
。更新

tempdict = dict()
for symbol in ["AAPL","FB"]:
    with requests.get("https://financialmodelingprep.com/api/v3/company/profile/"+ symbol) as url:
        data_string = url.content.decode()
        data =json.loads(data_string)
    tempdict.update(data)
    print(tempdict)

您可以使用映射到空列表的新键
“companyProfiles”
创建字典。然后,在每次迭代(对于每个
符号
)时,您可以将JSON响应字典附加到此列表,即:

import json
import requests
tempdict = {"companyProfiles": []}
for symbol in ["AAPL","FB"]:
    with requests.get("https://financialmodelingprep.com/api/v3/company/profile/"+ symbol) as url:
        data_string = url.content.decode()
        data =json.loads(data_string)
    tempdict["companyProfiles"].append(data)
print(tempdict)
产出:

{'companyProfiles': [{'profile': {'beta': '1.228499',
                                  'ceo': 'Timothy D. Cook',
                                  'changes': -0.52,
                                  'changesPercentage': '(-0.20%)',
                                  'companyName': 'Apple Inc.',
                                  'description': 'Apple Inc is designs, '
                                                 'manufactures and markets '
                                                 'mobile communication and '
                                                 'media devices and personal '
                                                 'computers, and sells a '
                                                 'variety of related software, '
                                                 'services, accessories, '
                                                 'networking solutions and '
                                                 'third-party digital content '
                                                 'and applications.',
                                  'exchange': 'Nasdaq Global Select',
                                  'image': 'https://financialmodelingprep.com/images-New-jpg/AAPL.jpg',
                                  'industry': 'Computer Hardware',
                                  'lastDiv': '2.92',
                                  'mktCap': '1.11264072E12',
                                  'price': 254.29,
                                  'range': '170.27-327.85',
                                  'sector': 'Technology',
                                  'volAvg': '49330169',
                                  'website': 'http://www.apple.com'},
                      'symbol': 'AAPL'},
                     {'profile': {'beta': '1.062394',
                                  'ceo': 'Mark Zuckerberg',
                                  'changes': 0.85,
                                  'changesPercentage': '(+0.51%)',
                                  'companyName': 'Facebook Inc.',
                                  'description': "Facebook Inc is the world's "
                                                 'largest online social '
                                                 'network. Its products are '
                                                 'Facebook, Instagram, '
                                                 'Messenger, WhatsApp, and '
                                                 'Oculus. Its products enable '
                                                 'people to connect and share '
                                                 'through mobile devices and '
                                                 'personal computers.',
                                  'exchange': 'Nasdaq Global Select',
                                  'image': 'https://financialmodelingprep.com/images-New-jpg/FB.jpg',
                                  'industry': 'Online Media',
                                  'lastDiv': '0',
                                  'mktCap': '4.75455062E11',
                                  'price': 166.8,
                                  'range': '137.1-224.2',
                                  'sector': 'Technology',
                                  'volAvg': '21198830',
                                  'website': 'http://www.facebook.com'},
                      'symbol': 'FB'}]}