我有一个列表字典,如何制作第i个元素的字典?

时间:2018-02-22 10:01:29

标签: python python-3.x dictionary

在python 3中工作。

所以我有这样的字典; {"stripes": [1,0,5,3], "legs": [4,4,2,3], "colour": ['red', 'grey', 'blue', 'green']} 我知道字典中的所有列表都具有相同的长度,但可能不包含相同类型的元素。其中一些甚至可能是列表清单。

我想要返回这样的字典;

$>>> Get_element(2) {"stripes": 5, "legs": 2, "colour": 'blue'}

我知道词典理解是一件事,但我对如何使用词汇感到困惑。我不确定它是否也是实现目标的最优雅方式,我可以切片吗?

3 个答案:

答案 0 :(得分:1)

如果您想创建Get_element()功能,可以采用这种方式:

def Get_element(d, i):
    return {k: v[i] for k, v in d.items()}

答案 1 :(得分:1)

这是一种方式:

class JSONGeometry: Decodable {
    class Geometry: Decodable {
        var vertices: [CGFloat]
        var normals: [CGFloat]
        var faces: [Int16]

        init(vertices: [CGFloat], normals: [CGFloat], faces: [Int16]) {
            self.vertices = vertices
            self.normals = normals
            self.faces = faces
        }
    }

    static let data: [JSONGeometry] = {
        let json = """
[{
  "id" : "2O2Fr$t4X7Zf8NOew3FNtn",
  "type" : "IFCWALLSTANDARDCASE",
  "geometry" : {
    "metadata" : { "formatVersion" : 3 },
    "materials": [],
    "vertices": [  8.8,0.2085,0.0, 3.4479692,0.2085,0.0, 3.4479692,0.2085,2.42, 2.6349692,0.2085,2.42, 2.6349692,0.2085,0.0, 0.0,0.2085,0.0, 0.0,0.2085,3.1, 8.8,0.2085,3.1, 8.383,0.2085,2.52, 8.383,0.2085,0.1, 3.548,0.2085,2.52, 3.548,0.2085,0.1, 8.8,0.2085,0.0, 3.4479692,0.2085,0.0, 3.4479692,-0.2085,0.0, 8.8,-0.2085,0.0, 3.4479692,-0.2085,2.42, 3.4479692,0.2085,2.42, 3.4479692,-0.2085,0.0, 3.4479692,0.2085,0.0, 2.6349692,-0.2085,2.42, 2.6349692,0.2085,2.42, 3.4479692,-0.2085,2.42, 3.4479692,0.2085,2.42, 2.6349692,-0.2085,0.0, 2.6349692,0.2085,0.0, 2.6349692,-0.2085,2.42, 2.6349692,0.2085,2.42, 2.6349692,0.2085,0.0, 0.0,0.2085,0.0, 0.0,-0.2085,0.0, 2.6349692,-0.2085,0.0, 0.0,0.2085,0.0, 0.0,0.2085,3.1, 0.0,-0.2085,0.0, 0.0,-0.2085,3.1, 8.8,0.2085,3.1, 0.0,0.2085,3.1, 0.0,-0.2085,3.1, 8.8,-0.2085,3.1, 8.8,-0.2085,0.0, 8.8,-0.2085,3.1, 8.8,0.2085,0.0, 8.8,0.2085,3.1, 8.383,-0.2085,2.52, 8.383,0.2085,2.52, 8.383,-0.2085,0.1, 8.383,0.2085,0.1, 3.548,-0.2085,2.52, 3.548,0.2085,2.52, 8.383,-0.2085,2.52, 8.383,0.2085,2.52, 8.383,-0.2085,0.1, 8.383,0.2085,0.1, 3.548,-0.2085,0.1, 3.548,0.2085,0.1, 3.548,-0.2085,0.1, 3.548,0.2085,0.1, 3.548,-0.2085,2.52, 3.548,0.2085,2.52, 0.0,-0.2085,0.0, 2.6349692,-0.2085,0.0, 2.6349692,-0.2085,2.42, 3.4479692,-0.2085,2.42, 3.4479692,-0.2085,0.0, 8.8,-0.2085,0.0, 8.8,-0.2085,3.1, 0.0,-0.2085,3.1, 8.383,-0.2085,0.1, 3.548,-0.2085,0.1, 8.383,-0.2085,2.52, 3.548,-0.2085,2.52   ],
    "normals":  [ 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, -1.0,0.0,0.0, -1.0,0.0,0.0, -1.0,0.0,0.0, -1.0,0.0,0.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, -1.0,-0.0,-0.0, -1.0,-0.0,-0.0, -1.0,-0.0,-0.0, -1.0,-0.0,-0.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, -1.0,0.0,0.0, -1.0,0.0,0.0, -1.0,0.0,0.0, -1.0,0.0,0.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0    ],
    "colors":   [ ],
    "uvs":    [ ],
    "faces": [  32, 8,7,0,8,7,0, 32, 8,0,9,8,0,9, 32, 7,8,10,7,8,10, 32, 10,2,3,10,2,3, 32, 2,10,11,2,10,11, 32, 11,9,1,11,9,1, 32, 7,10,6,7,10,6, 32, 10,3,6,10,3,6, 32, 3,4,5,3,4,5, 32, 6,3,5,6,3,5, 32, 9,0,1,9,0,1, 32, 11,1,2,11,1,2, 32, 14,12,15,14,12,15, 32, 13,12,14,13,12,14, 32, 18,16,17,18,16,17, 32, 18,17,19,18,17,19, 32, 22,20,21,22,20,21, 32, 22,21,23,22,21,23, 32, 26,24,25,26,24,25, 32, 26,25,27,26,25,27, 32, 29,31,30,29,31,30, 32, 29,28,31,29,28,31, 32, 33,32,34,33,32,34, 32, 35,33,34,35,33,34, 32, 38,39,37,38,39,37, 32, 39,36,37,39,36,37, 32, 41,40,42,41,40,42, 32, 43,41,42,43,41,42, 32, 46,44,45,46,44,45, 32, 46,45,47,46,45,47, 32, 50,48,49,50,48,49, 32, 50,49,51,50,49,51, 32, 55,52,53,55,52,53, 32, 54,52,55,54,52,55, 32, 58,56,57,58,56,57, 32, 58,57,59,58,57,59, 32, 67,60,62,67,60,62, 32, 62,63,71,62,63,71, 32, 67,62,71,67,62,71, 32, 62,60,61,62,60,61, 32, 71,63,69,71,63,69, 32, 67,71,66,67,71,66, 32, 66,71,70,66,71,70, 32, 69,64,68,69,64,68, 32, 70,68,65,70,68,65, 32, 66,70,65,66,70,65, 32, 69,63,64,69,63,64, 32, 68,64,65,68,64,65 ]
     }

} ,{
  "id" : "2O2Fr$t4X7Zf8NOew3FKGS",
  "type" : "IFCWALLSTANDARDCASE",
  "geometry" : {
    "metadata" : { "formatVersion" : 3 },
    "materials": [],
    "vertices": [  3.583,0.062,0.0, 3.583,0.062,2.795, 6.661338E-16,0.062,0.0, 6.661338E-16,0.062,2.795, 6.661338E-16,0.062,0.0, 6.661338E-16,0.062,2.795, 6.661338E-16,-0.062,0.0, 6.661338E-16,-0.062,2.795, 6.661338E-16,-0.062,0.0, 6.661338E-16,-0.062,2.795, 3.583,-0.062,0.0, 3.583,-0.062,2.795, 3.583,-0.062,0.0, 3.583,-0.062,2.795, 3.583,0.062,0.0, 3.583,0.062,2.795, 3.583,0.062,0.0, 6.661338E-16,0.062,0.0, 6.661338E-16,-0.062,0.0, 3.583,-0.062,0.0, 3.583,0.062,2.795, 6.661338E-16,0.062,2.795, 6.661338E-16,-0.062,2.795, 3.583,-0.062,2.795   ],
    "normals":  [ 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, 0.0,1.0,0.0, -1.0,-0.0,-0.0, -1.0,-0.0,-0.0, -1.0,-0.0,-0.0, -1.0,-0.0,-0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 0.0,-1.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 1.0,0.0,0.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,-1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,0.0,1.0, 0.0,0.0,1.0    ],
    "colors":   [ ],
    "uvs":    [ ],
    "faces": [  32, 1,0,2,1,0,2, 32, 3,1,2,3,1,2, 32, 5,4,6,5,4,6, 32, 7,5,6,7,5,6, 32, 9,8,10,9,8,10, 32, 11,9,10,11,9,10, 32, 13,12,14,13,12,14, 32, 15,13,14,15,13,14, 32, 17,19,18,17,19,18, 32, 17,16,19,17,16,19, 32, 22,23,21,22,23,21, 32, 23,20,21,23,20,21 ]
  }
}]
"""
        guard let data = json.data(using: .utf8) else { return [] }
        guard let result = try? JSONDecoder().decode([JSONGeometry].self, from: data) else { return [] }
        return result
    }()

    var id: String
    var type: String
    var geometry: Geometry

    var toSCN: SCNGeometry {
        var sources: [SCNGeometrySource] = []

        let vertices = SCNGeometrySource(
            vertices: geometry.vertices.eachSlice(3).map{ SCNVector3($0[0], $0[1], $0[2]) }
        )
        sources.append(vertices)

        if geometry.normals.count > 0 {
            let normals = SCNGeometrySource(
                normals: geometry.normals.eachSlice(3).map{ SCNVector3($0[0], $0[1], $0[2]) }
            )
            sources.append(normals)
        }

        let indices = Data(
            bytes: geometry.faces.map{ Int16($0) },
            count: MemoryLayout<Int16>.size * geometry.faces.count
        )

        let elements = [
            SCNGeometryElement(
                data: indices,
                primitiveType: .triangles,
                primitiveCount: geometry.faces.count / 3,
                bytesPerIndex: MemoryLayout<Int16>.size
            )
        ]

        return SCNGeometry(sources: sources, elements: elements)
    }

    init(id: String, type: String, geometry: Geometry) {
        self.id = id
        self.type = type
        self.geometry = geometry
    }
}

extension Sequence {
    func eachSlice(_ clump:Int) -> [[Self.Element]] {
        return self.reduce(into:[]) { memo, cur in
            if memo.count == 0 {
                return memo.append([cur])
            }
            if memo.last!.count < clump {
                memo.append(memo.removeLast() + [cur])
            } else {
                memo.append([cur])
            }
        }
    }
}

答案 2 :(得分:1)

def ith_element(d, i):
  return { k : v[i] for k, v in d.items() }

这是一个可以完成这项工作的小字典。您只需逐个查看字典中的所有键值对,然后对其进行一些转换,得到另一个字典,其对应的数量与现有字典相同。

您可以阅读有关列表推导here的更多信息。