python 2.7创建一个对象数组

时间:2017-08-23 17:08:39

标签: python arrays python-2.7 dictionary collections

我怎么能在python

中做这样的事情
//Initailize array of objects.
 let myArray = [
  {id: 0, name: "Jhon",position: null},
  {id: 1, name: "Sara", position: null}
],

//Find index of specific object using findIndex methode.    
objIndex = myArray.findIndex((obj => obj.id == 1));
myArray[objIndex].position= "manager"

2 个答案:

答案 0 :(得分:1)

执行问题描述的简单脚本 -

#! /usr/bin/python

obj1 = {"id": 0, "name": "John", "position": None}
obj2= {"id": 1, "name": "Sara", "position": None}

myArray = [obj1, obj2]

for i in myArray:
    if i["id"] == 1:
        i["position"] = "manager"

print(myArray)

答案 1 :(得分:-1)

您可以将对象列表设置为:

list = []

list.append({
       EmpID:1234
       FirstName:Joe
})