I am currently trying to build python code that removes the last element in a list in the case that any of three lists's lengths do not equal to each other. This would save time going back into the lists and manually listExample{}.pop()
to certain lists every time the criteria is not met.
The list with the lowest length should be the desired length of elements for all three lists. In this example, it would be four elements but ideally the program should count the lengths of the three lists and grab the lowest integer as it's target. If the lists do not equal to each other, then the program will .pop()
from the correct lists until listLength1 == listLength2 == listLength3
is met.
Here are the lists and variables I created to set up the example:
listExample1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
listExample2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
listExample3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']
listLength1 = len(listExample1)
listLength2 = len(listExample2)
listLength3 = len(listExample3)
print(listLength1) #6
print(listLength2) #4
print(listLength3) #5
Here is code I am currently building to attempt this:
if listLength1 == listLength2 == listLength3:
pass
elif listLength1 < listLength2:
pass
elif listLength1 > listLength3:
pass
elif listLength1 == listLength2:
pass
elif listLength2 < listLength1:
pass
elif listLength2 > listLength3:
pass
elif listLength2 == listLength1:
pass
elif listLength3 < listLength3:
pass
elif listLength3 > listLength1:
pass
elif listLength3 == listLength2:
pass
else:
pass
The if/elif seems redundant and I feel like there is room for error when I start listExample1.pop()
in a certain condition which would likely mess up down the line. What is the best way to approach this?
Desired output:
print(listExample1) #['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
print(listExample2) #['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
print(listExample3) #['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
CodePudding user response:
You'd better have them in a list of lists. Then delete the unwanted suffixes.
n = min(map(len, lists))
for L in lists:
del L[n:]
More efficient than popping elements one by one or copying prefix slices.
CodePudding user response:
n = min(listLength1,listLength2,listLength3)
for i in range(listLength1-n):
listExample1.pop()
for i in range(listLength2-n):
listExample2.pop()
for i in range(listLength3-n):
listExample3.pop()
print(listExample1)
print(listExample2)
print(listExample3)
Output
['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
CodePudding user response:
Instead of popping iteratively, we can simply use this.
listExample1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
listExample2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
listExample3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']
lengths = [len(listExample1) ,len(listExample2) ,len(listExample3) ]
minL = min(lengths)
listExample1 = listExample1[:minL]
listExample2 = listExample2[:minL]
listExample3 = listExample3[:minL]
print(listExample1)
print(listExample2)
print(listExample3)
Output came as
['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
CodePudding user response:
You can get the minimum length using the min function. Then run a for loop for each list, popping out the last element if needed.
listExample1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
listExample2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
listExample3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']
minLen = min(len(listExample1),len(listExample2),len(listExample3))
for _ in range(len(listExample1)-minLen):
listExample1.pop(-1)
for _ in range(len(listExample2)-minLen):
listExample2.pop(-1)
for _ in range(len(listExample3)-minLen):
listExample3.pop(-1)
print(listExample1)
print(listExample2)
print(listExample3)
CodePudding user response:
Slicing is possible:
min_len = min(len(a), len(b), …)
a = a[:min_len]
b = b[:min_len]
…
CodePudding user response:
Not so much different from the previous comments, but I would prefer just to slice the lists and to return a copy of them if they aren't too big.
l1 = [1,2,3,4,5]
l2 = [3,4,5,6]
l3 = [1,2]
m = min(map(len, (l1,l2,l3)))
for i in [l1,l2,l3]:
print i[:m]
RESULT:
[1, 2]
[3, 4]
[1, 2]
CodePudding user response:
Try something like this:
def reduce_size(*sequences):
min_length = min(len(sequence) for sequence in sequences)
return (sequence[:min_length] for sequence in sequences)
list1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
list2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
list3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']
list1, list2, list3 = reduce_size(list1, list2, list3)
CodePudding user response:
Here is a "cute" way, that has the advantage of working for arbitrary iterables:
list1 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement', 'SixthElement']
list2 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement']
list3 = ['FirstElement', 'SecondElement', 'ThirdElement', 'ForthElement', 'FifthElement']
list1, list2, list3 = map(list, zip(*zip(list1, list2, list3)))