在PYTHON中,如何通过从其他列表中获取相同索引的值来追加值,并根据条件形成新列表
发布时间:2022-05-03 19:46:10 208
相关标签:
我有如下4个列表,每个列表包含8个值。
eventIdList=['112','114','115','117','198','125','138','107'] ## No duplicate value inside, like primary key.
eventTypeList=[fuel,driver,driver,trax,driver,fuel,lux,driver] ## duplicate values exist inside.
partnerIdList= ['aux','box','box','disc','box','cot','top','box' ] ## duplicate values exist inside.
customerIdList= ['dell','apple','apple','amazon','apple','microsoft','dell','apple'] ## duplicate values exist inside.
我得到的第一个有效载荷是索引0,如下所示。类似地,for循环中的其他索引也是如此。
"message":{
"eventId": '112',
"eventType": 'fuel',
"partnerId": 'aux'
"customerId": 'dell'
}
注意这里,除了eventId之外,所有有效负载(eventType、partnerId和customerId)的第一、第二和第七索引都是相同的。所以我需要在迭代过程中显示列表中这些索引的evend id,如下所示。我怎样才能做到呢??
"message":{
eventId: [114,115,107],
eventType: "driver",
partnerId: "box"
customerId: "apple"
}
def myAppendFunction(eventIdList, eventTypeList, customerIdList, partnerIdList):
formedEventIdList=[]
for inc , eventIdelem in enumerate(eventIdList):
print('inc**',inc)
print('eventid**',eventIdelem)
print('eventType**', eventTypeList[inc])
print('customerId**', customerIdList[inc])
print('partnerId**', partnerIdList[inc])
#LOGIC WANT
# if (eventTypeList[inc]==eventTypeList[inc+1]) and (customerIdList[inc]==customerIdList[inc+1]) and (partnerIdList[inc]==partnerIdList[inc+1]) :
#formedEventIdList.append(eventIdelem)
message = {"Type": "Notification",
"Message": {"eventType": eventType,"customerId": customerId,"partnerId": partnerId, "eventIds": formedEventIdList}
}
#for end
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报