Home > Net >  How can I append value by getting values from other lists same index and form a new list based on a
How can I append value by getting values from other lists same index and form a new list based on a

Time:03-30

(MY INPUT): I have 4 lists as below , and each list contains 8 values.

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.

First payload I am getting for index 0 as below. Similarly other indexes inside the for loop.

"message":{

"eventId": '112',

"eventType": 'fuel',

"partnerId": 'aux'

"customerId": 'dell'

}

Observe 4 lists in Input, Except the eventId, 1st, 2nd and 7th index of all the payloads (eventType, partnerId and customerId value) is same {'driver','box','apple'}. So I need to display-- those event ids in a list, appending the eventId values by iteration, Here eventId should be as [114,115,107]. How can I achieve it?

OUTPUT Expecting under for loop

when index is 1 "message":{

eventId: [114],

eventType: "driver",

partnerId: "box"

customerId: "apple"

}

when index is 2 "message":{

eventId: [114,115],

eventType: "driver",

partnerId: "box"

customerId: "apple"

}

when index is 7 "message":{

eventId: [114,115,107],

eventType: "driver",

partnerId: "box"

customerId: "apple"

}

My code something like this. not completed.

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

AGAIN GIVING My PROGRAM, OUTPUT and Expected OUTPUT Below. Please give me the Code which can work for me here.

My Python PROGRAM below:

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)

OUTPUT of this program below:

inc= 0 OUTPUT*********:: {'Message': {'eventType': 'fuel', 'customerId': 'dell', 'eventIds': '112', 'partnerId': 'aux'}}

inc= 1 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': '114', 'partnerId': 'box'}}

inc= 2 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': '115', 'partnerId': 'box'}}

inc= 3 OUTPUT*********:: {'Message': {'eventType': 'trax', 'customerId': 'amazon', 'eventIds': '117', 'partnerId': 'disc'}}

inc= 4 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': '198', 'partnerId': 'box'}}

inc= 5 OUTPUT*********:: {'Message': {'eventType': 'fuel', 'customerId': 'microsoft', 'eventIds': '125', 'partnerId': 'cot'}}

inc= 6 OUTPUT*********:: {'Message': {'eventType': 'lux', 'customerId': 'dell', 'eventIds': '138', 'partnerId': 'top'}}

inc= 7 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': '107', 'partnerId': 'box'}}

But I WANT OUTPUT as below:

inc= 0 OUTPUT*********:: {'Message': {'eventType': 'fuel', 'customerId': 'dell', 'eventIds': ['112'], 'partnerId': 'aux'}}

inc= 1 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': ['114'], 'partnerId': 'box'}}

inc= 2 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': ['115',114], 'partnerId': 'box'}}

inc= 3 OUTPUT*********:: {'Message': {'eventType': 'trax', 'customerId': 'amazon', 'eventIds': ['117'], 'partnerId': 'disc'}}

inc= 4 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': ['198'], 'partnerId': 'box'}}

inc= 5 OUTPUT*********:: {'Message': {'eventType': 'fuel', 'customerId': 'microsoft', 'eventIds': ['125'], 'partnerId': 'cot'}}

inc= 6 OUTPUT*********:: {'Message': {'eventType': 'lux', 'customerId': 'dell', 'eventIds': ['138'], 'partnerId': 'top'}}

inc= 7 OUTPUT*********:: {'Message': {'eventType': 'driver', 'customerId': 'apple', 'eventIds': ['107',115,114], 'partnerId': 'box'}}

========================

NOTE: Only those records which have same 'eventType', 'customerId' and 'partnerId'. store those eventIds and display in a list there. [when 'eventType','customerId' and 'partnerId' have same values, matches with other index, then append the eventID. Here when index=1,2 and 7. So in this case need to append 'eventIds' in a list and display]

[when index=0,3,4,5,6] 'eventType', 'customerId' and 'partnerId' values all 3 are not matching with other records/indices. So not required to append in this case.

CodePudding user response:

Iterating on four lists simultaneously can be done conveniently with zip.

Filtering elements that meet a certain condition can be done with a list comprehension.

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')]

You could also convert your four lists into one list of dicts:

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'}]

You can also find which element is the most common in a list, using statistics.mode:

from statistics import mode

goodPartnerId = mode(partnerIdList)
print(goodPartnerId)
# box

CodePudding user response:

# first create all the permutations from the three lists
all_combinations = [{"message":{"eventID": [], "eventType": eventTypeList[i], "partnerID": partnerIdList[j], "customerID": customerIdList[k]}} for i in range(len(list(set(eventTypeList)))) for j in range(len(list(set(partnerIdList)))) for k in range(len(list(set(customerIdList))))]

# use zip function to iterate simultaneously over the lists
for eventID, eventtype, partnerID, customerID in zip(eventIdList, eventTypeList, partnerIdList, customerIdList):
    event_dict = {"eventType": eventtype, "partnerID": partnerID, "customerID": customerID}
    for i in range(len(all_combinations)):
        if event_dict.items() <= all_combinations[i]["message"].items():
            if not eventID in all_combinations[i]["message"]["eventID"]:
                all_combinations[i]["message"]["eventID"].append(eventID)

# remove the dicts where for three given values of eventtype, partnerID, customerID no eventID was found
all_combs = [all_combinations[i] for i in range(len(all_combinations)) if len(all_combinations[i]["message"]["eventID"]) != 0]
print(all_combs)

  • Related