[英]How can I convert a groupby object to a list of lists and append a new column/value to the list's within the list
[英]How can I append value by getting values from other lists same index and form a new list based on a condition
(我的输入):我有如下 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'
}
观察 Input 中的 4 个列表,除了 eventId,所有有效负载(eventType、partnerId 和 customerId 值)的第 1、第 2 和第 7 个索引都是相同的 {'driver','box','apple'}。 所以我需要在列表中显示这些事件 ID,通过迭代附加 eventId 值,这里的 eventId 应该是 [114,115,107]。 我怎样才能实现它?
当索引为 1 “消息”时:{
eventId: [114],
eventType: "driver",
partnerId: "box"
customerId: "apple"
}
当索引为 2 “消息”时:{
eventId: [114,115],
eventType: "driver",
partnerId: "box"
customerId: "apple"
}
当索引为 7 “消息”时:{
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
再次提供我的程序, OUTPUT和预期的 OUTPUT下面。 请给我可以在这里为我工作的代码。
eventIdList=['112','114','115','117','198','125','138','107']
eventTypeList=['fuel','driver','driver','trax','driver','fuel','lux','driver'] ## duplicate values (like driver, fuel etc) is exist in eventTypeList,
partnerIdList= ['aux','box','box','disc','box','cot','top','box' ] ## duplicate values exist in partnerIdList like box
customerIdList= ['dell','apple','apple','amazon','apple','microsoft','dell','apple'] ## duplicate values is exist like apple, dell etc in customerIdList
for inc , eventIdelem in enumerate(eventIdList):
formTheEventIdList=[]
message = {
"Message": {"eventType": eventTypeList[inc],"customerId": customerIdList[inc],"eventIds": eventIdelem,"partnerId": partnerIdList[inc]}
}
print('inc=',inc,'OUTPUT*********:: ', message)
inc= 0 OUTPUT*********:: {'Message': {'eventType': 'fuel', 'customerId': 'dell', 'eventIds': '112', 'partnerId': '辅助'}}
inc= 1 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': '114', 'partnerId': '盒子'}}
inc= 2 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': '115', 'partnerId': '盒子'}}
inc= 3 OUTPUT*********:: {'Message': {'eventType': 'trax', 'customerId': 'amazon', 'eventIds': '117', 'partnerId': '光盘'}}
inc= 4 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': '198', 'partnerId': '盒子'}}
inc= 5 OUTPUT*********:: {'Message': {'eventType': 'fuel', 'customerId': 'microsoft', 'eventIds': '125', 'partnerId': '婴儿床'}}
inc= 6 OUTPUT*********:: {'Message': {'eventType': 'lux', 'customerId': 'dell', 'eventIds': '138', 'partnerId': '最佳'}}
inc= 7 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': '107', 'partnerId': '盒子'}}
inc= 0 OUTPUT*********:: {'Message': {'eventType': 'fuel', 'customerId': 'dell', 'eventIds': ['112'], 'partnerId' : '辅助'}}
inc= 1 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': ['114'] , 'partnerId' : '盒子'}}
inc= 2 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': ['115',114] , ' partnerId': 'box'}}
inc= 3 OUTPUT*********:: {'Message': {'eventType': 'trax', 'customerId': 'amazon', 'eventIds': ['117'], 'partnerId' : '光盘'}}
inc= 4 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': ['198'], 'partnerId' : '盒子'}}
inc= 5 OUTPUT*********:: {'Message': {'eventType': 'fuel', 'customerId': 'microsoft', 'eventIds': ['125'], 'partnerId' : '婴儿床'}}
inc= 6 OUTPUT*********:: {'Message': {'eventType': 'lux', 'customerId': 'dell', 'eventIds': ['138'], 'partnerId' : '顶部'}}
inc= 7 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': ['107',115,114] , ' partnerId': 'box'}}
========================
注意:只有那些具有相同“eventType”、“customerId”和“partnerId”的记录。 存储这些 eventIds 并显示在列表中。 [当'eventType','customerId'和'partnerId'值相同时,与其他索引匹配,则eventID为append。 这里当index=1,2 and 7时。所以在这种情况下需要将append'eventIds'放在一个列表中并显示]
[当 index=0,3,4,5,6] 'eventType'、'customerId' 和 'partnerId' 值 3 都与其他记录/索引不匹配。 因此在这种情况下不需要 append。
使用zip
可以方便地同时迭代四个列表。
可以通过列表理解来过滤满足特定条件的元素。
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.
def filterPartnerId(goodPartnerId, eventIdList, eventTypeList, partnerIdList, customerIdList):
return [(evId, evType, pId, cId) for (evId, evType, pId, cId) in zip(eventIdList, eventTypeList, partnerIdList, customerIdList) if pId == goodPartnerId]
print(filterPartnerId("box", eventIdList, eventTypeList, partnerIdList, customerIdList))
# [('114', 'driver', 'box', 'apple'),
# ('115', 'driver', 'box', 'apple'),
# ('198', 'driver', 'box', 'apple'),
# ('107', 'driver', 'box', 'apple')]
您还可以将四个列表转换为一个字典列表:
def fourlists_to_onelist(eventIdList, eventTypeList, partnerIdList, customerIdList):
return [{"eventId": evId, "eventType": evType, "partnerId": pId, "customerId": cId} for (evId, evType, pId, cId) in zip(eventIdList, eventTypeList, partnerIdList, customerIdList)]
def filterPartnerId(goodPartnerId, onelist):
return [x for x in onelist if x['partnerId'] == goodPartnerId]
onelist = fourlists_to_onelist(eventIdList, eventTypeList, partnerIdList, customerIdList)
print(filterPartnerId("box", onelist))
# [{'eventId': '114', 'eventType': 'driver', 'partnerId': 'box', 'customerId': 'apple'},
# {'eventId': '115', 'eventType': 'driver', 'partnerId': 'box', 'customerId': 'apple'},
# {'eventId': '198', 'eventType': 'driver', 'partnerId': 'box', 'customerId': 'apple'},
# {'eventId': '107', 'eventType': 'driver', 'partnerId': 'box', 'customerId': 'apple'}]
您还可以使用statistics.mode
查找列表中最常见的元素:
from statistics import mode
goodPartnerId = mode(partnerIdList)
print(goodPartnerId)
# box
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.