将列表列表的值与字典列表合并

时间:2016-08-15 22:33:50

标签: python loops indexing

u= [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '10']]
v=[{'id': 'a', 'adj': ['blue', 'yellow']}, {'id': 'b', 'adj': ['purple', 'red']}, {'id': 'c', 'adj': ['green', 'orange']}, {'id': 'd', 'adj': ['black', 'purple']}]

我想:

 result=[ {'id': 'a', 'adj': ['blue', 'yellow'], 'value': '1' },
        {'id': 'a', 'adj': ['blue', 'yellow'], 'value': '2' },
        {'id': 'a', 'adj': ['purple', 'red'], 'value': '3' },
        ...]

我已将u转换为字典:

m=[]
for i in u:
    s={}
    s['value']=i
    m.append(s)

#>>m= [{'value': ['1', '2']}, {'value': ['3']}, {'value': ['4', '5', '6']}, {'value': ['7', '8', '9', '10']}]

然后尝试应用zip函数...

for i,j in enumerate(v):
    for s,t in enumerate(l):
        if i= =s:
            #zip 2 dictionary together. Stuck here

提前多多感谢!这是我学习编程的第二周。

3 个答案:

答案 0 :(得分:0)

您需要压缩,遍历来自u的每个子列表,deepcopy来自v的每个dict并添加新的键/值配对,最后将新的dict附加到列表中:

   <div class="navbar-fixed"  >
    <nav id="nav_f" class="transparent z-depth-0" role="navigation" >
        <div class="container">



            <div class="nav-wrapper"  >

             <div class="row s12">

            <div>
                  <ul class="hide-on-med-and-down navbar " >
                      <li><a id="desk-about-us" href="#about-us">ABOUT US</a></li>
                      <li><a href="#how-it-works">HOW IT WORKS</a></li>
                      <li><a href="#comments">COMMENTS</a></li>
                  </ul>
                </div>
              </div>

                <ul id="nav-mobile" class="side-nav side-nav-menu ">
                    <li><a href="#about-us">ABOUT US</a></li>
                    <li><a href="#how-it-works">HOW IT WORKS</a></li>
                    <li><a href="#comments">COMMENTS</a></li>
                </ul>
            <a href="#" data-activates="nav-mobile" class="button-collapse right"><i class="material-icons">menu</i></a>
            </div>

       </div>
    </nav>
</div>

哪个会给你:

from copy import deepcopy

u= [['1', '2'], ['3'], ['4', '5', '6'], ['7', '8', '9', '10']]
v=[{'id': 'a', 'adj': ['blue', 'yellow']}, {'id': 'b', 'adj': ['purple', 'red']}, {'id': 'c', 'adj': ['green', 'orange']}, {'id': 'd', 'adj': ['black', 'purple']}]

out = []
# match up corresponding elements fromm both lists
for dct, sub in zip(v, u):
    # iterate over each sublist
    for val in sub:
        # deepcopy the dict as it contains mutable elements (lists)
        dct_copy = deepcopy(dct)
        # set the new key/value pairing
        dct_copy["value"] = val
        # append the dict to our out list
        out.append(dct_copy)
from pprint import pprint as pp
pp(out)

dicts有一个[{'adj': ['blue', 'yellow'], 'id': 'a', 'value': '1'}, {'adj': ['blue', 'yellow'], 'id': 'a', 'value': '2'}, {'adj': ['purple', 'red'], 'id': 'b', 'value': '3'}, {'adj': ['green', 'orange'], 'id': 'c', 'value': '4'}, {'adj': ['green', 'orange'], 'id': 'c', 'value': '5'}, {'adj': ['green', 'orange'], 'id': 'c', 'value': '6'}, {'adj': ['black', 'purple'], 'id': 'd', 'value': '7'}, {'adj': ['black', 'purple'], 'id': 'd', 'value': '8'}, {'adj': ['black', 'purple'], 'id': 'd', 'value': '9'}, {'adj': ['black', 'purple'], 'id': 'd', 'value': '10'}] 属性,或者你可以调用.copy但是因为你有可变对象作为值,所以只做一个浅拷贝是行不通的。以下示例显示了实际差异:

dict(dct)

what-exactly-is-the-difference-between-shallow-copy-deepcopy-and-normal-assignment

答案 1 :(得分:0)

在两个列表上应用In [19]: d = {"foo":[1, 2, 4]} In [20]: d1_copy = d.copy() # shallow copy, same as dict(d) In [21]: from copy import deepcopy In [22]: d2_copy = deepcopy(d) # deep copy In [23]: d["foo"].append("bar") In [24]: d Out[24]: {'foo': [1, 2, 4, 'bar']} In [25]: d1_copy Out[25]: {'foo': [1, 2, 4, 'bar']} # copy also changed In [26]: d2_copy Out[26]: {'foo': [1, 2, 4]} # deepcopy is still the same ,并创建一个新词典,其中旧词典将相应列表中的值添加为键值条目zip:< em>列表中的数字:

value

答案 2 :(得分:0)

您可以使用以下代码获取所需的结果。

result = []
for index, d in enumerate(u):
    for value in d:
        result.append(dict(v[index], value=value))

迭代enumerate - u离子,然后将正确的v字典和value的组合附加到result列表中

您可以使用列表理解将其压缩为相对干净的单行。

result = [dict(v[index], value=value) for index, d in enumerate(u) for value in d]