comparison does not allow me to act on the results - python

I want to know why is it that my if statements are not working. I have code that compares a recently compiled list to a bunch of other lists to see if there is a similar one.
The lists are made up of 12 values, direction of the list, and the number of times that same list has occurred. the comparison is done between the first 12 values of each list then what should happen is the position that is found with that list should be identified, however it seems I am unable to call that into action.
In essence this is what I want to do...
These are the lists I want to compare to one another:
list1 = [['1', '1', '1', '1', '1', '0', '1', '0', '1', '0', '1', 'up', 5], ['1', '0', '0', '1', '1', '0', '1', '0', '0', '0', '0', 'up', 2], ['1', '0', '1', '1', '0', '0', '1', '1', '1', '1', '1', 'up', 13], ['1', '0', '0', '1', '1', '0', '1', '1', '0', '0', '1', 'down', 5], ['0', '0', '1', '0', '1', '0', '1', '1', '1', '0', '1', 'up', 8], ['0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '1', 'up', 10], ['0', '1', '1', '1', '0', '0', '0', '0', '1', '1', '0', 'up', 6], ['1', '1', '0', '1', '1', '1', '0', '0', '0', '0', '0', 'down', 8], ['0', '0', '1', '0', '1', '1', '1', '0', '1', '0', '1', 'up', 6], ['0', '1', '0', '1', '0', '0', '0', '0', '0', '0', '1', 'up', 1], ['0', '0', '0', '0', '0', '1', '1', '1', '0', '1', '0', 'up', 3], ['0', '0', '0', '1', '1', '0', '0', '1', '0', '1', '1', 'up', 7], ['1', '1', '1', '1', '1', '1', '1', '0', '1', '0', '1', 'up', 9], ['0', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', 'down', 7], ['0', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1', 'down', 1]]
list2 = ['1', '0', '0', '1', '1', '0', '1', '0', '0', '0', '0']
This is the comparison code I want to use for the lists:
def fl(list1, list2):
index = -1
occ = 0
for i, l in enumerate(list2):
if l[:len(list1)] == list1:
if l[-1] > occ:
index = i
occ = l[-1]
if index == -1:
return "The first list is not present in the second one."
else:
print(f"The first lists appears in the second one at index {index} with a number of occurences equal too {occ}.")
u = "up"
y = "down"
lp = [(''.join(a[:6]), a[6]) for a in list2]
if (''.join(list1), u) in lp:
print("up")
elif (''.join(list1), y) in lp:
print("down")
print(fl(listB, listA))
This is the result I want to get:
The first lists appears in the second one at index 2 with a number of occurrences equal to 2.
up
However what I seem too get is this:
The first lists appears in the second one at index 2 with a number of occurrences equal too 2.
None
Why am I getting none and how can I solve this issue?

I think your problem is caused in the if/elif statement. In general if/elif should be used ONLY when you know that you covered ALL possible scenarios in your code.

Related

how to select a list with the most occurances

I have a list of lists that contains a list I am looking for. This list of lists contains 11 integers before a string and after that string is the number of times that list in particular has occurred e.g ['1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1', 'down', 9]. The string changes from time to time and can either be "up" or "down" meaning that the same list can occur twice e.g ['1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1', 'down', 9], ['1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1', 'up', 13].
What I seek assistance in is how do I make it so that in the instance the is a repeated list like as mentioned above, select the list with the most occurrences which in this case would be ['1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1', 'up', 13] while at the same time identifying the string.
To put in a more practical standpoint:
#this is the list of lists
listA = [['0', '1', '1', '0', '0', '0', '0', '1', '1', '0', '1', 'down', 5], ['0', '0', '0', '0', '1', '0', '0', '1', '1', '0', '0', 'down', 7], ['1', '1', '0', '1', '0', '1', '1', '0', '0', '1', '1', 'down', 1], ['0', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', 'down', 6], ['1', '1', '1', '0', '0', '0', '0', '0', '1', '1', '1', 'down', 3], ['1', '0', '1', '0', '0', '1', '1', '1', '0', '0', '0', 'down', 9], ['0', '1', '1', '0', '0', '0', '0', '1', '0', '1', '1', 'up', 4], ['0', '0', '0', '1', '1', '1', '1', '0', '1', '0', '0', 'down', 8], ['1', '0', '1', '1', '0', '1', '1', '0', '1', '0', '1', 'up', 5], ['0', '1', '0', '1', '0', '1', '1', '0', '1', '0', '1', 'down', 3], ['0', '1', '0', '1', '1', '0', '0', '1', '0', '1', '0', 'down', 5], ['1', '1', '0', '1', '0', '0', '1', '1', '1', '0', '1', 'up', 6], ['0', '0', '1', '1', '1', '0', '1', '0', '0', '1', '1', 'up', 10], ['1', '1', '1', '1', '1', '1', '0', '0', '1', '0', '1', 'up', 3], ['0', '1', '1', '1', '0', '0', '0', '1', '0', '1', '1', 'down', 3], ['1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '0', 'up', 6], ['0', '0', '1', '1', '1', '0', '0', '0', '1', '0', '1', 'down', 7], ['1', '1', '0', '0', '1', '0', '0', '1', '0', '1', '1', 'down', 3], ['0', '0', '0', '0', '1', '1', '0', '1', '1', '1', '0', 'up', 3], ['1', '1', '0', '1', '0', '1', '1', '0', '1', '1', '0', 'up', 10], ['0', '1', '0', '1', '1', '1', '1', '0', '1', '0', '0', 'up', 6], ['0', '0', '1', '1', '0', '0', '1', '1', '1', '0', '0', 'down', 2], ['1', '1', '1', '1', '0', '0', '0', '0', '0', '1', '1', 'up', 4], ['0', '0', '0', '1', '1', '0', '1', '1', '0', '0', '0', 'down', 6], ['1', '0', '0', '1', '1', '0', '1', '1', '1', '0', '1', 'up', 7], ['0', '1', '0', '0', '1', '0', '0', '0', '1', '0', '1', 'down', 13], ['0', '1', '0', '0', '1', '1', '0', '0', '0', '0', '0', 'up', 2], ['0', '0', '0', '1', '1', '1', '1', '0', '0', '0', '1', 'down', 5], ['0', '0', '0', '0', '1', '0', '0', '1', '0', '1', '1', 'down', 9], ['0', '1', '1', '1', '1', '1', '1', '0', '0', '0', '1', 'up', 4], ['1', '1', '1', '0', '0', '0', '0', '1', '0', '0', '0', 'up', 3], ['0', '0', '1', '0', '1', '0', '0', '0', '1', '1', '0', 'down', 7], ['1', '0', '1', '1', '0', '1', '1', '0', '1', '1', '0', 'up', 5], ['0', '1', '0', '0', '1', '1', '1', '0', '0', '1', '0', 'down', 10], ['0', '1', '1', '0', '0', '1', '0', '0', '0', '0', '1', 'down', 6], ['1', '1', '0', '1', '1', '0', '0', '0', '0', '1', '1', 'down', 3], ['1', '1', '0', '1', '1', '1', '0', '1', '0', '0', '0', 'up', 4], ['1', '0', '0', '1', '1', '0', '1', '0', '1', '0', '1', 'down', 6], ['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', 'down', 6], ['0', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', 'up', 6], ['1', '0', '1', '0', '0', '0', '1', '0', '1', '0', '1', 'down', 8], ['0', '1', '0', '1', '1', '0', '1', '0', '0', '0', '1', 'down', 5], ['1', '1', '1', '1', '1', '1', '0', '0', '0', '0', '1', 'up', 4], ['1', '0', '0', '1', '1', '1', '0', '0', '1', '0', '0', 'up', 2], ['0', '0', '1', '0', '0', '1', '1', '1', '1', '0', '0', 'down', 2], ['0', '0', '1', '1', '0', '0', '1', '1', '1', '1', '1', 'up', 4], ['1', '0', '0', '0', '0', '0', '0', '1', '0', '1', '0', 'down', 3], ['1', '0', '1', '1', '0', '1', '1', '1', '1', '1', '0', 'up', 10], ['0', '0', '0', '0', '0', '0', '1', '1', '1', '0', '1', 'up', 3], ['1', '1', '0', '1', '0', '1', '0', '0', '1', '0', '0', 'down', 7], ['1', '1', '0', '1', '0', '1', '1', '1', '1', '0', '1', 'up', 6], ['0', '1', '1', '0', '1', '1', '0', '1', '0', '1', '0', 'down', 2], ['1', '1', '1', '1', '1', '1', '0', '1', '1', '0', '0', 'down', 1], ['1', '0', '1', '1', '0', '0', '1', '1', '0', '0', '0', 'down', 6], ['1', '0', '1', '0', '0', '1', '0', '0', '1', '1', '1', 'up', 4], ['1', '1', '0', '0', '0', '0', '1', '0', '0', '1', '1', 'up', 3], ['0', '1', '1', '0', '0', '1', '1', '1', '1', '0', '0', 'up', 5], ['0', '0', '1', '0', '0', '0', '0', '1', '1', '1', '1', 'up', 2], ['0', '1', '1', '0', '1', '0', '1', '0', '1', '0', '1', 'up', 7], ['1', '1', '1', '0', '1', '1', '0', '0', '1', '0', '0', 'down', 5], ['0', '0', '1', '1', '0', '1', '1', '1', '0', '0', '1', 'up', 7], ['0', '1', '1', '1', '0', '1', '0', '0', '0', '1', '1', 'up', 11], ['1', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', 'down', 3], ['0', '0', '0', '0', '1', '0', '0', '0', '1', '1', '1', 'down', 11], ['0', '0', '0', '0', '1', '1', '0', '0', '0', '1', '0', 'down', 11], ['1', '0', '1', '0', '1', '0', '1', '1', '1', '0', '1', 'up', 6], ['0', '0', '1', '1', '1', '0', '0', '0', '1', '1', '0', 'down', 5], ['1', '1', '1', '0', '0', '0', '1', '0', '0', '1', '0', 'down', 5], ['1', '0', '1', '0', '0', '1', '0', '1', '0', '0', '0', 'down', 14], ['1', '1', '0', '1', '0', '1', '1', '0', '0', '1', '1', 'up', 8], ['0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', 'down', 10], ['1', '1', '0', '1', '1', '1', '0', '0', '1', '1', '1', 'up', 12], ['1', '0', '1', '1', '0', '1', '1', '1', '0', '0', '1', 'up', 8], ['1', '0', '0', '1', '0', '1', '0', '0', '0', '1', '0', 'down', 8], ['0', '1', '1', '0', '0', '1', '1', '0', '1', '0', '1', 'up', 7], ['1', '0', '0', '1', '0', '1', '0', '0', '1', '1', '1', 'down', 5], ['1', '1', '1', '1', '0', '0', '0', '1', '1', '1', '0', 'up', 5], ['1', '1', '1', '0', '0', '0', '0', '1', '0', '1', '0', 'down', 6], ['1', '0', '0', '1', '0', '0', '1', '0', '1', '1', '1', 'down', 2], ['1', '1', '1', '0', '0', '1', '1', '1', '0', '1', '0', 'down', 2], ['0', '1', '0', '1', '1', '1', '0', '0', '1', '1', '1', 'down', 1], ['0', '1', '1', '1', '1', '0', '1', '1', '0', '0', '0', 'up', 4], ['1', '0', '0', '0', '0', '0', '1', '1', '1', '1', '0', 'up', 1], ['0', '1', '1', '1', '1', '0', '1', '0', '1', '0', '0', 'down', 3], ['1', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'down', 7], ['1', '0', '0', '1', '1', '1', '0', '0', '1', '1', '0', 'up', 3], ['0', '1', '1', '0', '1', '1', '1', '1', '0', '1', '0', 'up', 8], ['1', '0', '1', '1', '1', '1', '1', '1', '0', '1', '1', 'up', 7], ['0', '0', '0', '0', '1', '1', '1', '0', '1', '1', '1', 'down', 4], ['1', '1', '0', '1', '1', '0', '1', '0', '1', '1', '0', 'up', 4], ['1', '0', '1', '1', '0', '0', '1', '0', '1', '0', '1', 'down', 5], ['1', '1', '0', '1', '1', '1', '0', '0', '0', '0', '1', 'down', 5], ['1', '0', '1', '0', '0', '1', '0', '0', '1', '1', '0', 'down', 9], ['1', '1', '0', '1', '0', '0', '0', '1', '1', '0', '1', 'up', 5], ['1', '1', '0', '1', '1', '0', '0', '1', '0', '1', '1', 'up', 9], ['0', '1', '1', '0', '1', '1', '1', '1', '0', '0', '0', 'up', 2], ['1', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', 'down', 6], ['0', '1', '0', '1', '1', '1', '0', '1', '0', '0', '1', 'up', 8], ['0', '0', '0', '1', '1', '1', '0', '0', '1', '1', '1', 'down', 2], ['0', '1', '1', '0', '0', '0', '1', '1', '1', '1', '1', 'down', 1], ['1', '0', '1', '0', '0', '1', '1', '1', '0', '0', '1', 'up', 8], ['1', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'down', 9], ['0', '1', '1', '1', '1', '0', '1', '0', '1', '1', '0', 'up', 9], ['0', '1', '1', '0', '1', '0', '1', '0', '0', '1', '0', 'down', 3], ['0', '0', '1', '0', '0', '1', '1', '0', '1', '1', '1', 'down', 3], ['0', '0', '0', '1', '1', '0', '1', '0', '1', '1', '0', 'up', 4], ['0', '1', '0', '1', '1', '0', '1', '0', '1', '1', '1', 'down', 1], ['0', '1', '1', '1', '0', '0', '0', '1', '1', '1', '0', 'down', 2], ['1', '0', '0', '1', '1', '1', '0', '1', '1', '0', '0', 'up', 4], ['0', '0', '1', '1', '1', '0', '0', '1', '1', '0', '1', 'up', 13], ['1', '1', '0', '1', '1', '1', '0', '1', '0', '0', '0', 'down', 6], ['1', '1', '1', '0', '1', '0', '0', '1', '1', '1', '0', 'up', 8], ['0', '0', '1', '0', '0', '0', '0', '1', '0', '0', '0', 'down', 8], ['0', '1', '1', '1', '0', '0', '0', '0', '1', '1', '0', 'down', 2], ['1', '1', '0', '0', '1', '1', '0', '1', '0', '0', '0', 'down', 7], ['0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', 'down', 7], ['1', '1', '1', '0', '1', '1', '1', '0', '1', '1', '1', 'up', 10], ['0', '1', '0', '1', '1', '0', '0', '1', '0', '1', '1', 'up', 8], ['1', '0', '0', '0', '1', '1', '1', '0', '0', '1', '0', 'up', 1], ['0', '0', '1', '1', '1', '1', '0', '1', '1', '1', '0', 'up', 7], ['1', '0', '0', '1', '0', '1', '1', '1', '1', '0', '0', 'up', 4], ['1', '0', '1', '1', '1', '0', '1', '1', '0', '0', '1', 'up', 7], ['1', '0', '1', '1', '1', '0', '0', '1', '0', '0', '1', 'up', 5], ['0', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1', 'up', 4], ['0', '1', '1', '0', '1', '0', '0', '0', '0', '0', '1', 'down', 6], ['1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', 'up', 8], ['1', '1', '1', '1', '0', '0', '0', '0', '1', '1', '0', 'up', 7], ['1', '1', '0', '1', '0', '0', '1', '1', '0', '0', '1', 'up', 5], ['0', '0', '0', '0', '1', '0', '0', '1', '0', '1', '0', 'down', 5], ['1', '1', '1', '0', '0', '1', '0', '1', '1', '1', '0', 'up', 8], ['1', '1', '0', '1', '0', '0', '1', '0', '0', '0', '0', 'down', 14], ['1', '1', '0', '0', '1', '1', '1', '0', '1', '0', '0', 'up', 2], ['1', '0', '1', '1', '1', '0', '1', '1', '0', '0', '1', 'down', 2], ['1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '0', 'up', 5], ['1', '1', '0', '0', '1', '0', '1', '1', '1', '1', '0', 'up', 4], ['1', '1', '1', '0', '0', '1', '0', '1', '0', '0', '1', 'down', 4], ['1', '1', '1', '0', '0', '0', '1', '1', '1', '0', '1', 'down', 3], ['1', '1', '1', '1', '1', '0', '0', '0', '0', '0', '1', 'down', 1], ['0', '0', '1', '0', '1', '0', '1', '0', '0', '0', '1', 'up', 2], ['1', '0', '0', '1', '1', '0', '1', '1', '0', '0', '0', 'down', 8], ['0', '1', '1', '0', '0', '0', '0', '1', '1', '0', '0', 'up', 4], ['0', '1', '1', '0', '0', '1', '0', '0', '0', '0', '0', 'down', 7], ['0', '0', '0', '1', '1', '0', '0', '1', '1', '0', '1', 'up', 5], ['1', '1', '0', '0', '0', '0', '1', '1', '0', '1', '0', 'down', 7], ['0', '1', '1', '1', '0', '1', '0', '1', '1', '1', '1', 'up', 11], ['0', '1', '1', '0', '1', '1', '1', '1', '0', '0', '1', 'up', 9], ['0', '0', '1', '0', '1', '1', '0', '1', '1', '0', '0', 'down', 4], ['0', '1', '0', '1', '0', '0', '1', '0', '0', '1', '0', 'up', 5], ['0', '1', '0', '1', '0', '1', '1', '0', '1', '0', '0', 'down', 8], ['0', '0', '0', '0', '0', '1', '1', '0', '0', '1', '1', 'up', 3], ['0', '0', '0', '1', '0', '0', '1', '1', '1', '0', '0', 'up', 3], ['1', '0', '1', '1', '1', '0', '0', '0', '1', '0', '1', 'up', 4], ['1', '1', '1', '1', '1', '0', '1', '0', '1', '0', '0', 'up', 8], ['1', '1', '0', '1', '1', '1', '0', '1', '0', '0', '1', 'down', 3], ['0', '0', '1', '1', '0', '1', '1', '1', '1', '0', '0', 'up', 2], ['1', '1', '1', '0', '1', '1', '1', '0', '0', '1', '1', 'up', 4], ['0', '1', '1', '0', '0', '1', '0', '1', '0', '0', '0', 'down', 6], ['1', '1', '1', '1', '0', '0', '0', '1', '1', '0', '0', 'down', 3], ['1', '1', '1', '0', '1', '0', '1', '1', '0', '0', '1', 'down', 4], ['0', '0', '1', '1', '0', '0', '1', '0', '1', '1', '0', 'down', 6], ['1', '1', '1', '0', '0', '1', '1', '0', '1', '0', '0', 'down', 9], ['0', '0', '1', '0', '1', '1', '0', '1', '1', '0', '1', 'up', 4], ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', 'up', 5], ['1', '1', '0', '0', '1', '1', '0', '1', '0', '1', '0', 'up', 7], ['1', '0', '0', '0', '1', '0', '1', '1', '0', '1', '1', 'down', 4], ['0', '1', '0', '1', '1', '1', '0', '0', '1', '0', '1', 'up', 10], ['1', '1', '0', '1', '0', '1', '0', '0', '0', '0', '1', 'down', 9], ['1', '0', '1', '1', '0', '0', '1', '1', '1', '0', '0', 'up', 1], ['0', '0', '1', '0', '1', '1', '1', '1', '1', '1', '1', 'up', 5], ['1', '0', '1', '1', '1', '1', '0', '0', '0', '0', '0', 'down', 8], ['1', '0', '1', '0', '0', '0', '0', '1', '1', '1', '1', 'up', 3], ['1', '0', '0', '0', '1', '1', '0', '0', '0', '1', '0', 'down', 6], ['0', '0', '1', '0', '1', '0', '0', '1', '1', '0', '1', 'down', 5], ['1', '0', '1', '1', '1', '1', '1', '0', '1', '1', '0', 'up', 6], ['1', '0', '1', '0', '1', '1', '1', '1', '1', '0', '1', 'up', 7], ['1', '0', '1', '1', '0', '0', '0', '1', '0', '1', '1', 'down', 2], ['0', '1', '0', '0', '1', '1', '1', '1', '0', '0', '0', 'down', 3], ['1', '0', '1', '1', '1', '0', '0', '1', '0', '0', '0', 'down', 4], ['0', '1', '1', '0', '1', '0', '0', '1', '1', '0', '0', 'down', 5], ['0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '0', 'down', 11], ['1', '1', '0', '0', '1', '1', '0', '1', '1', '0', '0', 'up', 5], ['0', '1', '0', '0', '1', '1', '1', '1', '0', '1', '0', 'up', 9], ['1', '1', '0', '1', '1', '0', '0', '0', '1', '1', '0', 'up', 5], ['0', '1', '1', '0', '1', '0', '0', '1', '0', '0', '1', 'down', 5], ['1', '0', '1', '1', '0', '0', '1', '0', '0', '0', '0', 'up', 1], ['1', '1', '0', '0', '0', '1', '1', '0', '1', '1', '1', 'down', 3], ['1', '0', '0', '1', '0', '1', '1', '1', '0', '0', '1', 'down', 2], ['0', '0', '1', '0', '1', '0', '1', '1', '0', '0', '1', 'down', 9], ['0', '0', '0', '0', '1', '0', '1', '0', '1', '0', '0', 'down', 8], ['1', '0', '1', '1', '0', '1', '1', '0', '1', '0', '1', 'down', 3], ['1', '0', '0', '1', '1', '0', '1', '0', '0', '1', '0', 'down', 5], ['1', '1', '0', '0', '0', '1', '1', '1', '0', '0', '1', 'up', 6], ['1', '0', '1', '1', '0', '1', '1', '1', '0', '1', '1', 'up', 9], ['0', '1', '0', '0', '1', '1', '1', '1', '0', '0', '1', 'up', 5], ['0', '1', '0', '1', '0', '1', '1', '1', '0', '0', '1', 'down', 4], ['0', '0', '0', '1', '0', '0', '1', '0', '0', '1', '0', 'down', 8], ['1', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', 'down', 5], ['1', '0', '1', '0', '0', '1', '0', '0', '0', '1', '0', 'down', 5], ['1', '1', '1', '1', '1', '0', '1', '1', '0', '1', '0', 'up', 6], ['0', '1', '1', '0', '1', '0', '0', '0', '1', '1', '1', 'down', 4], ['0', '0', '1', '0', '0', '1', '1', '0', '0', '1', '1', 'down', 5], ['0', '0', '1', '1', '0', '0', '0', '1', '0', '0', '1', 'up', 3], ['0', '1', '0', '1', '0', '0', '0', '0', '1', '1', '1', 'up', 6], ['0', '1', '0', '1', '0', '0', '1', '0', '1', '0', '0', 'down', 8], ['0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', 'down', 5], ['1', '0', '0', '0', '1', '0', '1', '1', '0', '0', '0', 'up', 2], ['1', '0', '0', '1', '0', '0', '1', '0', '0', '1', '1', 'down', 9], ['0', '1', '1', '1', '0', '0', '0', '0', '0', '0', '0', 'down', 6], ['1', '1', '1', '0', '0', '0', '1', '0', '1', '0', '1', 'up', 5], ['0', '1', '0', '1', '0', '1', '1', '1', '0', '0', '0', 'up', 4], ['1', '0', '1', '1', '0', '1', '1', '1', '0', '1', '0', 'up', 7], ['1', '0', '0', '1', '1', '0', '1', '1', '1', '0', '1', 'down', 2], ['1', '0', '1', '1', '0', '1', '0', '0', '0', '1', '0', 'down', 7], ['0', '1', '1', '1', '1', '0', '1', '0', '0', '1', '0', 'down', 5], ['0', '1', '1', '0', '1', '0', '0', '0', '0', '0', '0', 'down', 2], ['0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '0', 'down', 11], ['1', '1', '0', '0', '0', '0', '1', '1', '1', '0', '0', 'down', 8], ['0', '0', '0', '0', '1', '1', '0', '0', '1', '0', '0', 'up', 1], ['1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '1', 'down', 9], ['1', '0', '0', '0', '0', '1', '0', '1', '1', '0', '1', 'up', 3], ['1', '1', '1', '0', '1', '1', '0', '1', '0', '1', '0', 'up', 14], ['1', '1', '1', '1', '0', '0', '0', '0', '1', '0', '0', 'up', 3], ['1', '0', '1', '1', '0', '0', '1', '1', '0', '1', '1', 'up', 5], ['1', '0', '0', '0', '1', '1', '0', '1', '1', '0', '1', 'up', 9], ['1', '0', '0', '0', '0', '1', '0', '1', '1', '0', '1', 'down', 10], ['1', '1', '1', '1', '0', '1', '0', '1', '0', '0', '0', 'down', 3], ['1', '0', '0', '0', '0', '1', '0', '0', '1', '1', '1', 'down', 6], ['0', '0', '1', '1', '0', '1', '1', '1', '0', '0', '1', 'down', 6], ['1', '1', '1', '1', '1', '0', '1', '0', '0', '0', '0', 'down', 4], ['1', '1', '0', '0', '1', '1', '0', '0', '1', '0', '0', 'down', 9], ['1', '1', '1', '1', '0', '0', '0', '1', '1', '0', '0', 'up', 6], ['0', '1', '0', '1', '0', '0', '1', '1', '0', '1', '0', 'down', 7], ['1', '0', '0', '0', '0', '1', '1', '0', '0', '0', '1', 'down', 8], ['0', '0', '1', '0', '1', '1', '1', '1', '0', '1', '0', 'up', 6]]
#this is the list I am looking for
listB = ['1', '0', '0', '0', '0', '1', '0', '1', '1', '0', '1']
#this is the method I am using to try and find the occurrence of `listB` in `listA`
def fl1(list1, list2):
index = -1
occ = 0
for i, l in enumerate(list2):
if l[:len(list1)] == list1:
if l[-1] > occ:
index = i
occ = l[-1]
if index == -1:
return "The 1st list doesn't appear in the 2nd one."
else:
print(f"The 1st list appears in the 2nd one at index {index} with a number of occurrences equal to {occ}.")
u = "up"
y = "down"
look_up = [(''.join(a[:11]), str(a[11])) if len(a) >= 12 else ('', '') for a in list2]
if (''.join(list1), y) in look_up:
print("down")
if (''.join(list1), u) in look_up:
print("up")
print(fl1(listB, listA))
The output I get proves that both instances of the list exist but I wish for a way for me to use the list with the most occurrences while at the same time identifying whether the string is "up" or "down". How can I do that?
#output
The 1st list appears in the 2nd one at index 579 with a number of occurrences equal to 10.
down
up
None
I don't know if I got what you want, but If other than returning what you already return in your function, you also want to return the list with the highest number of occurrences, you can do that by appending to a list the list that you want to find everytime you find it, and then just return the list with the highest number of occurrences:
#this is the method I am using to try and find the occurrence of `listB` in `listA`
def fl1(list1, list2):
found_lists = [] #The instances of the list you want to find
index = -1
occ = 0
for i, l in enumerate(list2):
if l[:len(list1)] == list1:
if l[-1] > occ:
index = i
occ = l[-1]
found_lists.append(l) #Add the list to the found lists
if index == -1:
return "The 1st list doesn't appear in the 2nd one."
else:
print(f"The 1st list appears in the 2nd one at index {index} with a number of occurrences equal to {occ}.")
u = "up"
y = "down"
look_up = [(''.join(a[:11]), str(a[11])) if len(a) >= 12 else ('', '') for a in list2]
if (''.join(list1), y) in look_up:
print("down")
if (''.join(list1), u) in look_up:
print("up")
print(f"Lists found: {found_lists}")
print(f"List with maximum occurrence: {max(found_lists, key=lambda x: x[-1])}") #Print the list with the maximum number of occurrences, that is expressed by the last element of the list.
print(fl1(listB, listA))
The code prints the following:
The 1st list appears in the 2nd one at index 224 with a number of occurrences equal to 10.
down
up
Lists found: [['1', '0', '0', '0', '0', '1', '0', '1', '1', '0', '1', 'up', 3], ['1', '0', '0', '0', '0', '1', '0', '1', '1', '0', '1', 'down', 10]]
List with maximum occurrence: ['1', '0', '0', '0', '0', '1', '0', '1', '1', '0', '1', 'down', 10]
None
Of course, if you want to use the list, just return it instead of printing it.
The simplest change would be to store it in a variable:
direction = None
if l[-1] > occ:
index = i
occ = l[-1]
direction = l[-2]
found_lists.append(l) #Add the list to the found lists
At the end the direction will contain what you are searching for.

how to find a list in a bunch of lists in python

I want a way to make it so that I can use list B to find out if a copy of it's self exists in list A.
I want a way to make it so that I can use list B to find out if a copy of it's self exists in list A.
this is the way data a has been collected:
for i in range(len(fsa)):
if fsa[i] < fsb[i]:
kol.append('1')
else:
kol.append('0')
start = 0
end = len(fsa)
j = [ ]
for x in range(start, end, 6):
m = fsb[x:x+6]
t = kol[x:x+6]
if m[0] < m[-1]:
t.append('up')
else:
t.append('down')
j.append(t)
counter = Counter(tuple(j) for j in j)
for members, count, in counter.items():
print(list(members), count)
output:
listA = ['1', '1', '0', '1', '0', '1', 'down'] 2
['0', '0', '1', '1', '1', '1', 'up'] 2
['1', '0', '0', '1', '0', '1', 'up'] 1
['0', '0', '0', '1', '1', '0', 'up'] 2
['1', '1', '0', '0', '0', '0', 'up'] 1
['0', '0', '1', '1', '0', '1', 'down'] 1
['1', '0', '0', '0', '0', '1', 'down'] 1
['1', '1', '1', '1', '1', '1', 'up'] 1
this is how data b was collected:
for _ in range(num):
inner = driver.find_element_by_xpath("//html/body/div[1]/div[2]/div/div/div/div[2]/div/div[2]/div[2]/div/div/div[2]/div[4]/span[1]").get_attribute("innerHTML")
print(inner)
lok.append(inner)
time.sleep(20)#the hour
print(lok)
lokk = []
num = 7
for _ in range(num):
inner = driver.find_element_by_xpath("//html/body/div[1]/div[2]/div/div/div/div[2]/div/div[2]/div[2]/div/div/div[2]/div[4]/span[1]").get_attribute("innerHTML")
print(inner)
lokk.append(inner)
time.sleep(20)
print(lokk)
output:
listB = ['1', '1', '1', '1', '1', '1']
list A also shows how many times that particular list has appeared
so I want a way to first find the repeating list of list B in list A, secondly to select the one with the most repetitions in the case that there are multiple versions of it.
I tried a bunch of things but non really helped as i am still quite new at coding
As many pointed out, the code provided is not properly formatted, so I made some assumptions. Here is a half-solution to get you unstuck, try to modify this to get what you want. You will learn more by modifying this code than if I were to give you the final solution.
from collections import Counter # for comparing lists' content
from typing import List # for annotations
listA = [
['1', '1', '0', '1', '0', '1', 'down'],
['0', '0', '1', '1', '1', '1', 'up'],
['1', '0', '0', '1', '0', '1', 'up'],
['0', '0', '0', '1', '1', '0', 'up'],
['1', '1', '0', '0', '0', '0', 'up'],
['0', '0', '1', '1', '0', '1', 'down'],
['1', '0', '0', '0', '0', '1', 'down'],
['1', '1', '1', '1', '1', '1', 'up'],
]
listB = ['1', '1', '1', '1', '1', '1']
def find_match(metrix: List[List[str]], list_: List[str]) -> List[List[str]]:
list_counter = Counter(list_)
# Solution 1: Pythonic solution
matches = [match for match in metrix if Counter(match[:-1]) == list_counter]
# Solution 2: beginner friendly solution
# matches = []
# for m_list in metrix:
# if Counter(m_list[:-1]) == list_counter:
# matches.append(m_list)
return matches
# Complexities
# if n == metrix.length and m == metrix[0].length; then
# Time: O(nm);
# Space: O(nm);
print(find_match(listA, listB))
# outputs: [['1', '1', '1', '1', '1', '1', 'up']]
Here's something that works. Not sure if it's exactly what you want, but you can modify it to fit your needs.
Note that it assumes listB has the same length as the first part of each of listA elements (before "up"/"down").
listA =[
['1', '1', '0', '1', '0', '1', 'down', 2],
['0', '0', '1', '1', '1', '1', 'up', 2],
['1', '0', '0', '1', '0', '1', 'up', 1],
['0', '0', '0', '1', '1', '0', 'up', 2],
['1', '1', '0', '0', '0', '0', 'up', 1],
['0', '0', '1', '1', '0', '1', 'down', 1],
['1', '0', '0', '0', '0', '1', 'down', 1],
['1', '1', '1', '1', '1', '1', 'up', 1]
]
listB = ['1', '1', '1', '1', '1', '1']
listC = ['0','0','0','0','0','0']
def find_list(list1,list2):
index = -1
occ = 0
for i,l in enumerate(list2):
if l[:len(list1)]==list1:
if l[-1]>occ:
index = i
occ = l[-1]
if index == -1:
return "The 1st list doesn't appear in the 2nd one."
else:
return f"The 1st list appears in the 2nd one at index {index} with a number of occurences equal to {occ}."
print(find_list(listB, listA))
# The 1st list appears in the 2nd one at index 7 with a number of occurences equal to 1.
print(find_list(listC, listA))
# The 1st list doesn't appear in the 2nd one.

Write and store data from input as binary, like an array

I need to take an input (ranging from 1-12) from the user and store the data as binary. (cannot use arrays)
For example: if the user inputs 3, it would return 000000000100. (the 3rd digit from the right)
I was thinking that this would be possible with a log algorithm, but I don't really know where to start. How would I do this? Any help is appreciated.
If I am reading this problem correctly you are being fed in numbers 1-12 and depending on what numbers are fed in you need to return a binary string where the bits at whatever positions are given are equal to one (without lists/arrays). To achieve this you could read in values to a set and then construct a string where the inputted values are one and everything else is zero. Like this:
def read_in():
positions = set()
while True:
print('Enter 1-12 or Q to stop:',end=' ')
entry = input()
if entry != 'Q':
positions.add(int(entry))
else:
break
ret = ''
for i in range(12,0,-1):
if i in positions:
ret += '1'
else:
ret += '0'
return ret
print(read_in())
If you want to update any index to 1 multiple times across multiple inputs, you might want to use a list containing 12 elements that you can tick. Then with that list, you can already get both the string value e.g. "000000000100" and the int value e.g. 4
# Initialize list that we will tick
bin_digits = ["0"] * 12
# Let's assume that the input from user is from 1 to 12
for num in range(1, 13):
# Tick the target index
bin_digits[-num] = "1"
bin_str = "".join(bin_digits) # String value
bin_int = int(bin_str, 2) # Int value
print(bin_digits, bin_str, bin_int)
Output
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1'] 000000000001 1
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1'] 000000000011 3
['0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1'] 000000000111 7
['0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1'] 000000001111 15
['0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1'] 000000011111 31
['0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1'] 000000111111 63
['0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1'] 000001111111 127
['0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1'] 000011111111 255
['0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1'] 000111111111 511
['0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'] 001111111111 1023
['0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'] 011111111111 2047
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'] 111111111111 4095

How can you sort a list of lists by the number of nonzero elements in each sublist?

I have a list of lists (8 lists of 15 elements each) and it's like below:
mylist = [
['Adam', '0', '1', '0', '1', '0', '1', '0', '0', '1', '0', '1', '0', '1', '0'],
['Bobby', '0', '0', '0', '0', '1', '1', '0', '0', '0', '0', '0', '1', '1', '0'],
['Felicia', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0'],
['Jake', '0', '1', '1', '1', '1', '1', '0', '0', '0', '0', '0', '0', '0', '0'],
['MikeP', '0', '0', '1', '0', '1', '0', '0', '0', '1', '1', '0', '1', '0', '0'],
['MikeF', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
['Shannon', '0', '1', '0', '0', '1', '0', '0', '0', '0', '1', '0', '1', '0', '0'],
['Tom', '0', '1', '1', '1', '1', '0', '0', '0', '1', '1', '1', '1', '0', '0']
]
Using Python 3, I'm trying to get mylist sorted beginning with the most zeros in a sublist to the fewest zeros in a sublist. I don't want to sort within each list - the 0s and 1s need to stay where they are ultimately.
I have tried using len() and lambda functions and other ideas I find here like breaking it down with the following:
for index1, value1 in enumerate(mylist):
mylistsorted.append([value2 for index2, value2 in enumerate(mylist[index1]) if value2 != '0'])
mylistsorted.sort(key = len)
But I lose all the '0's of course in that new sorted list. Should this perhaps be done with something else like numpy or matrices or something else vice lists of lists? Thank you for any help...
Use list.count as your key for sorted to count all the 0's in each list:
sorted(mylist, key=lambda x: -x[1:].count('0'))
Use a key that counts the number of 0's:
mylist.sort( key = lambda l : -l.count('0') )
Taking a not-so-wild guess and counting the ones instead:
mylist.sort(key=lambda l: l.count('1'))

How to iterate through a bunch of values in a dictionaries that are lists python

ratinglist = open("ratings.txt").readlines()
booklist = open("booklist.txt").readlines()
keys = [key.strip('\n') for key in ratinglist[0::2]]
values = [value.strip(' \n').split(' ') for value in ratinglist[1::2]]
my_dict = dict(zip(keys, values))
for values in my_dict:
value * x = sum
So, I have a giant dictionary that has a list of values for each key.
Here is an example of my dictionary:
'KeeLed': ['0', '0', '0', '5', '0', '0', '0', '5', '0', '5', '5', '0', '0', '0', '0', '0', '5', '-3', '-3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '5', '0', '0', '5', '0', '5', '5', '5', '5', '0', '0', '0', '0', '0', '3', '1', '0', '0', '0', '0'], 'Megan': ['5', '5', '0', '0', '0', '0', '0', '0', '0', '3', '0', '5', '0', '0', '1', '0', '5', '0', '1', '5', '0', '0', '0', '0', '0', '1', '0', '5', '0', '0', '3', '5', '5', '0', '0', '5', '0', '0', '3', '0', '0', '3', '5', '5', '0', '0', '0', '0', '0', '5', '5', '0', '5', '0', '0']}
What I need to do is do a for loop, that loops through each value, multiplies them, and then adds them.
I just need help getting the for loop to iterate through the values correctly.
For example, for KeeLed and Megan, I want this for loop to iterate through their seperate lists of values, and multiply them, then add them.
Like this: 0 * 5, + 5 * 0, + 0 * 0, + 5 * 0.... etc etc
The issue is I haven't found anything online on how to iterate through lists that are inside a dictionary . I've found some resources on looping through lists, but I need to loop through lists of values that are inside of a dictionary.
I would like the results to be put in a list, but I could do that if I get some help with the for loop.
scores for each person = [325, 450]
"The issue is I haven't found anything online on how to iterate through lists that are inside a dictionary . I've found some resources on looping through lists, but I need to loop through lists of values that are inside of a dictionary."
EXAMPLE
myDict = {
'KeeLed': ['0', '0', '0', '5', '0', '0', '0', '5', '0', '5', '5', '0', '0', '0', '0', '0', '5', '-3', '-3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '5', '0', '0', '5', '0', '5', '5', '5', '5', '0', '0', '0', '0', '0', '3', '1', '0', '0', '0', '0'],
'Megan': ['5', '5', '0', '0', '0', '0', '0', '0', '0', '3', '0', '5', '0', '0', '1', '0', '5', '0', '1', '5', '0', '0', '0', '0', '0', '1', '0', '5', '0', '0', '3', '5', '5', '0', '0', '5', '0', '0', '3', '0', '0', '3', '5', '5', '0', '0', '0', '0', '0', '5', '5', '0', '5', '0', '0']
}
# For-looping through dict will give the key.
for name in myDict:
total = 0
# applying the key into myDict will give the list associated with the dict
for score in myDict[name]:
total = total + int(score)
print(name,total)
Result
KeeLed 54
Megan 85

Categories