• yshan
    2019-05-24
    [(xx, yy) for xx in x for yy in y if x != y]
    应该是 if xx != yy] 吧
    思考题一行:
    [dict(zip(attributes,v)) for v in values]



    展开

    作者回复: 嗯嗯,笔误已更新。
    思考题👍

    
     63
  • AllenGFLiu
    2019-05-25
    思考题
    一行版:
    [dict(zip(attributes, value)) for value in values]

    循环版:
    l = []
    for value in values:
        d = {}
        for i in range(3):
            d[attributes[i]] = value[i]
        l.append(d)


    展开
     1
     34
  • LiANGZE
    2019-05-24
    课后的练习题,手机打的,格式可能不好看

    print( [{ attributes[i]: value[i] for i in range(len(attributes)) } for value in values])
    
     23
  • Fergus
    2019-05-26
    # Ask
    attributes = ['name', 'dob', 'gender']
    values = [['jason', '2000-01-01', 'male'],
              ['mike', '1999-01-01', 'male'],
              ['nancy', '2001-02-01', 'female']]
    # Answers1
    lis = []
    for i in values:
        print(i)
        print(dict(zip(attributes, i)))
        print(lis.append(dict(zip(attributes, i))))
    # Answers2
    [dict(zip(attributes, i)) for i in values]
    # Answers3
    list(map(lambda x: dict(zip(attributes, x)), values))
    # Answers4
    lis = []
    for i in values:
        dic = {}
        for n, j in enumerate(i):
            dic[attributes[n]] = j
        lis.append(dic)
    print(lis)

    我没有想到用多行条件循环语句实现的方法。先留言,然后看别人怎么做的。
    展开
    
     13
  • Deed
    2019-05-24
    [dict(zip(attributes, value)) for value in values]
    
     9
  • 呜呜啦
    2019-06-15
    attributes = ['name', 'dob', 'gender']
    values = [
    ['jason', '2000-01-01', 'male'],
    ['mike', '1999-01-01', 'male'],
    ['nancy', '2001-02-01', 'female']
    ]

    # 多行代码版
    list1 = [] # 建立空列表
    for value in values: # 对值列表进行循环
        dict1 = {} # 建立空字典,方便后续字典键值对存储
        for index,key in enumerate(attributes): # 遍历键列表,返回元素与索引
            dict1[key] = value[index] # 键与值配对,组装成字典元素
        list1.append(dict1) # 将新字典添加到里列表中
    print(list1) # 打印显示出完整列表

    # 一行代码版
    [{key:value[index] for index,key in enumerate(attributes)}for value in values]
    # 最外层[]与上面“list1=[]”和“list1.append(dict1)”等价
    # "{key:value[index] for index,key in enumerate(attributes)}"与上面"for value in values:"内代码等价
    # 体会:先梳理逻辑,写出多行代码版,再回溯写出一行代码版,体现出Python的简洁优美
    展开
     2
     7
  • 武林秀才
    2019-05-28
    一行的
    [dict([(attributes[j],values[i][j]) for j in range(len(attributes))]) for i in range(len(values))]
    
     4
  • Wing·三金
    2019-05-27
    # 多行版本
    combined = []
    for value in values:
        temp_dict = {}
        for index, attr in enumerate(attributes):
            temp_dict[attr] = value[index]
        combined.append(temp_dict)

    combined

    # 单行版本
    [{attr: value[index] for index, attr in enumerate(attributes)} for value in values]
    展开
    
     3
  • 敏杰
    2019-05-27
    [dict(map(lambda x, y:(x, y), attributes, value)) for value in values]
    
     3
  • k8scloud
    2019-05-26
    attributes = ['name','dob','gender']
    values = [['jason','2000-1-01','male'],['mike','1999-01-1','male'],['nancy','201-02-01','female']]
    output = []
    for v in values:
        dst = {}
        for k in range(len(v)):
            dst[attributes[k]] = v[k]
        output.append(dst)
    print(output)
    展开
    
     3
  • 隰有荷
    2019-06-08
    zip函数感觉好神奇,得差一下,哈哈
    
     2
  • 夜下凝月
    2019-05-25
    Python中没有switch,但是可以用字典来代替。
    #不调用函数
    level = {0:'red', 1:'yellow', 2:'green'}
    try:
        level(id)
    except:
        pass

    #需要调用函数
    def delete (id):
        '''delete the movie that id is 0'''
    ………
        ………
    ………
        ………
    opt = {0:delete, 1:warning, 2:allow}

    opt[ id ]( id )
    展开
    
     2
  • 小希
    2019-11-27
    通过反复查看文章内容跟参考留言中的评论,理解后输出。
    虽然不是自己写的,也算是对自己的一种鼓励,至少清楚了逻辑。
    #一行
    print ([dict(zip(attributes,value))for value in values])

    #多行输出
    list1 = [] #建议空列表
    for value in values: #对值列表进行循环
        dict1 = {} #简历空字典,后续对字典键值对存储
        for index ,key in enumerate(attributes):#遍历建列表,返回索引与元素
            dict1[key] = value[index] #键与值匹配对,组装成字典元素
        list1.append(dict1) #将字典添加到列表中
    print(list1)
    展开

    作者回复: 加油

    
     1
  • 夜雨声烦
    2019-09-17
    作业:使用zip函数将两个list聚合成一个元祖,然后再转换成字典:
    bb = [dict(zip(attributes,va)) for va in values]
    
     1
  • 易拉罐
    2019-06-03
    # general.
    expected_output = []
    for v in values:
        expected_output.append(dict(zip(attributes, v)))
    print(expected_output)

    # one line
    print([dict(zip(attributes, v)) for v in values])
    展开
    
     1
  • 夏秋冬的春天
    2019-05-31
    # 多行
    output_dist = []
    for v in values:
        dict_at = {}
        for i in range(len(attributes)):
            dict_at[attributes[i]] = v[i]
        output_dist.append(dict_at)
    print(output_dist)

    # 一行
    print( [{ attributes[i]: value[i] for i in range(len(attributes)) } for value in values])
    展开
    
     1
  • Geek_d848f7
    2019-05-29
    #多行
    res=[]
    for value in values:
        if len(attributes)!=len(value):
            continue
        temp={}
        for index,each in enumerate(value):
            temp[attributes[index]]=each
        res.append(temp)
    print(res)

    #使用zip
    s=[dict(zip(attributes,value)) for value in values if len(attributes)==len(value)]
    
     1
  • taoist
    2019-05-26
    一行: [dict(zip(attributes, value)) for value in values]
    多行:
    l = []
    for value in values:
        d = {}
        for i,j in zip(attributes, value):
            d[i] = j
        l.append(d)
    展开
    
     1
  • catshitfive
    2019-05-24
    写个长的吧
    def get_list_dicts(attribs,vals):
        result=[]
        for l in values:
            temp_dict={}
            if len(l)==len(attributes):
                for i in range(len(attributes)):
                    temp_dict[attributes[i]]=l[i]
            else:
                print('values列表中第{}组内值的个数'
                      '需与属性列表内的个数保持一致!'.format(values.index(l)+1))
                return None
            result.append(temp_dict)
        return result
    展开
    
     1
  • wang
    2020-02-08
    [{attributes[0]:item[0], attributes[1]:item[1], attributes[2]:item[2]} for item in values]
    
    
我们在线,来聊聊吧