How To Add A Character To A List If Two Items From Another List Appear Consecutively? Python
I have a DataFrame in which each cell contains a list. I have a function that aims to insert a '1' into each list, based on a condition. However, my code is not doing what I expect
Solution 1:
with this question, your answer is to change your function func
by:
def func(row):
look =2for i inrange(len(row)-look):
if (row[i] in members) & \
(row[i+1] in non_members) & \
(row[i+2] in non_members):
# if the conditionis met, return the list with the 1 added where you want
returnrow[:i+1] + ['1'] +row[i+1:]
# incase you never met your condition, you return the original list without changes
returnrow
Your problem was that you mixed str
and list
type in your func
Post a Comment for "How To Add A Character To A List If Two Items From Another List Appear Consecutively? Python"