Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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
创建一个连接其他两个dict的dict[Python]_Python_Dictionary - Fatal编程技术网

创建一个连接其他两个dict的dict[Python]

创建一个连接其他两个dict的dict[Python],python,dictionary,Python,Dictionary,我试图创造一个与下面的格言相结合的地方: buy_dict = {'Coin1': [{'buy_price': 105, 'id_buy':2 },{'buy_price': 100, 'id_buy':1 }], 'Coin2': [{'buy_price': 1200, 'id_buy':2 },{'buy_price': 1100, 'id_buy':1 }]} sell_dict = {'Coin1': [{'sell_price': 106, 'id_sell':1 }, {'se

我试图创造一个与下面的格言相结合的地方:

buy_dict = {'Coin1': [{'buy_price': 105, 'id_buy':2 },{'buy_price': 100, 'id_buy':1 }], 'Coin2': [{'buy_price': 1200, 'id_buy':2 },{'buy_price': 1100, 'id_buy':1 }]}

sell_dict = {'Coin1': [{'sell_price': 106, 'id_sell':1 }, {'sell_price': 110, 'id_sell':2 }], 'Coin2': [{'sell_price': 1250, 'id_sell':1 },{'sell_price': 1350, 'id_sell':2 }]}
在buy_dict上,我有一个带有购买价格和ID的dict,按硬币分组。sell_dict上的相同模型。我需要做的是通过连接来自两个dict adobe的数据来创建第三个dict,因此新dict的第一条记录将是dictA的第一条记录加上dictB的第一条记录。下面是它的外观:

DictC = {'Coin1': [{'buy_price': 105, 'id_buy':2, 'sell_price': 106, 'id_sell':1}, {'buy_price': 100, 'id_buy':1, 'sell_price': 110, 'id_sell':2}], 'Coin2': [{'buy_price': 1200,'id_buy':2, 'sell_price': 1250,'id_sell':1}, {'buy_price': 1100, 'id_buy':1,'sell_price': 1350,'id_sell':2}]}
简言之,DictC的记录将是最高买入价和最低卖出价的结合点。无需订购,因为已订购buy_dict和sell_dict


谢谢

这个技巧是将两个字典合并,并按顺序遍历数组。与我的答案不同的是,我的答案是允许一枚硬币只存在于一个目录中,并且在每个目录中有不同数量的买卖

  from collections import defaultdict
  from itertools import zip_longest

  DictC = defaultdict(list)
  for coin in (buy_dict.keys() | sell_dict.keys()):
      for buy_item, sell_item in zip_longest(buy_dict.get(coin, []), sell_dict.get(coin, []), fillvalue={}):
          DictC[coin].append({**buy_item, **sell_item})
对于python2.7,替换DictC[coin]。将{**buy_item,**sell_item}附加为


买dict.keys |卖dict.keys与setbuy_dict.keys+卖dict.keys和zip_longest与izip_longest

你可以在一行字典中完成这一操作:

这将为您提供所需的输出:

{'Coin1': [{'id_sell': 1, 'buy_price': 105, 'id_buy': 2, 'sell_price': 106}, {'id_sell': 2, 'buy_price': 100, 'id_buy': 1, 'sell_price': 110}], 'Coin2': [{'id_sell': 1, 'buy_price': 1200, 'id_buy': 2, 'sell_price': 1250}, {'id_sell': 2, 'buy_price': 1100, 'id_buy': 1, 'sell_price': 1350}]}
为什么?

我们希望遍历buy_dict中的每个键,这就是最外层的循环。然后在这里面,我们想把这个键设置为一个值,它是一个列表。这是通过以下语法完成的:{k:[…]for k in buy_dict}

这张单子里有什么?既然原始词典中的列表已经正确排序,我们可以将它们按顺序解包成自己的新词典

为此,我们遍历buy_dict[k]列表中的不同索引,然后使用解包技术形成一个新字典


请注意,输出的字典在顺序上与您的字典不同,因为字典没有顺序;它们只是一组键:值对

我发现的最简单的方法是:

dictD = {}
for coin in sell_dict:
    coinList = []
    for things in zip(sell_dict[coin], buy_dict[coin]):
        joinedDict = {}
        for d in things:
            joinedDict.update(d)
        coinList.append(joinedDict)
    dictD[coin] = coinList

这里我假设sell_dict和buy_dict的键是相同的。除了执行明显的for循环之外,除了使用zip使列表按您想要的方式对齐之外,没有什么特别的。

可能重复@eyllanesc不确定这是否是这篇博文的好目标。@eyllanescnope@eyllanesc我试过这个方法,但我的字典更复杂,事实上,这是一系列的命令。为什么要投否决票?这是OP想要的,我解释了代码…@julianhenning请接受灰色->绿色箭头,然后回答!谢谢
{'Coin1': [{'id_sell': 1, 'buy_price': 105, 'id_buy': 2, 'sell_price': 106}, {'id_sell': 2, 'buy_price': 100, 'id_buy': 1, 'sell_price': 110}], 'Coin2': [{'id_sell': 1, 'buy_price': 1200, 'id_buy': 2, 'sell_price': 1250}, {'id_sell': 2, 'buy_price': 1100, 'id_buy': 1, 'sell_price': 1350}]}
dictD = {}
for coin in sell_dict:
    coinList = []
    for things in zip(sell_dict[coin], buy_dict[coin]):
        joinedDict = {}
        for d in things:
            joinedDict.update(d)
        coinList.append(joinedDict)
    dictD[coin] = coinList