Reputation: 75
I have an array in the following format:
[(u'iapsetting', 0), (u'iap-mms',0)]
The array must contain only tuples of this type:
(u'mmssetting', 0) or (u'iapsetting', 0) or (u'iap-mms', 0)
My first array is obviously missing the (u'mmssetting',0) but it could many different combinations of missing/existing tuples.
I am struggling to find a way to add the correct missing tuples. The array must always contain only 3 tuples.
This is what I have so far but it is not working as expected:
for type in setup: #setup is the array containing 1,2 or 3 tuples
iap_found = False
mms_found = False
iap_mms_found = False
if type[0]=='iapsetting':
iap_found = True
elif type[0]=='mmssetting':
mms_found = True
elif type[0]== 'iap-mms':
iap_mms_found = True
#Add missing settings
if(iap_found==False):
print("MISSING IAP",setup)
setup.append((u'iapsetting',0))
elif(mms_found==False):
print("MISSING MMS",setup)
setup.append((u'mmssetting',0))
elif(iap_mms_found==False):
print("MISSING IAP-MMS",setup)
setup.append((u'iap-mms',0))
Any help will be greatly appreciated because there might be a much better way of doing this. Thank you.
Sara
Upvotes: 1
Views: 1268
Reputation: 6606
I think sets represent this logic most clearly. Use a set comprehension to extract the strings.
REQUIRED_VALUES = {"mmssetting", "iapsetting", "iap-mms"}
input_tuple_list = [(u'mmssetting', 1), (u'iap-mms', 3)]
# set comprehension paired with set difference -- results in {"iapsetting"}
missing_values = REQUIRED_VALUES - {val for val, count in input_tuple_list}
# results in [(u'mmssetting', 1), (u'iap-mms', 3), (u'iapsetting', 3)]
input_tuple_list.extend((val, 0) for val in missing_values)
Upvotes: 0
Reputation: 1229
What you were doing wrong were mainly two things:
you started adding missing settings before you finished looping through the whole array.
# Initialize flags before entering loop:
iap_found = False
mms_found = False
iap_mms_found = False
for type in setup: #setup is the array containing 1,2 or 3 tuples
if type[0]=='iapsetting':
iap_found = True
elif type[0]=='mmssetting':
mms_found = True
elif type[0]== 'iap-mms':
iap_mms_found = True
#Add missing settings after looping through the whole array:
if(iap_found==False):
print("MISSING IAP",setup)
setup.append((u'iapsetting',0))
if(mms_found==False):
print("MISSING MMS",setup)
setup.append((u'mmssetting',0))
if(iap_mms_found==False):
print("MISSING IAP-MMS",setup)
setup.append((u'iap-mms',0))
Upvotes: 1
Reputation: 6767
Try this:
existing = set(x[0] for x in setup)
for expected in ('iapsetting', 'mmssetting', 'iap-mms'):
if expected not in existing:
setup.append((expected, 0))
Upvotes: 4
Reputation: 19776
Instead of using if
... elif
.. elif
you should use
if not iap_found:
print("MISSING IAP",setup)
setup.append((u'iapsetting',0))
if not mms_found:
print("MISSING MMS",setup)
setup.append((u'mmssetting',0))
if not iap_mms_found==False:
print("MISSING IAP-MMS",setup)
setup.append((u'iap-mms',0))
The problem of if
.. elif
is that only one branch will get executed.
For other solutions, different (and in this case also better) than if
, see the other anwers
Upvotes: 0