Compare 2 list of lists at specific indexes, Python
compare list of lists python
python compare two lists element wise
python compare list elements
compare two lists python
python compare two lists of strings
python check if two lists have same elements
python compare two lists of tuples
I have 2 lists of lists and I want to compare the contents of the sub-lists of each one of the two lists, at specific indexes only, and then save the index of the both list and sub-list when there is a hit, for example compare the:
- The sub list at index 5 of the first list with the sub list at index 0 of the second list, if there are any similarities store indexes of both lists and sub-lists
- The sub list at index 6 of the first list with the sub list at index 3 of the second list,...
- The sub list at index 7 of the first list with the sub list at index 6 of the second list,...
and so on..
I created a for loop that reads all the contents of the first list from index 5 and onward along with their indexes, and a nested for loop that compares them with all the sub-lists of the second list iterating from 0 to 3 to 6 etc. Then by comparing the intersection of both sets at each iteration i save the indexes of all common elements in all sublists.
common_a =  common_b =  for index_external, item in enumerate(data_db[5:]): #from 5 onwards for index_eternal2, item_2 in enumerate(data_ga[::3]): #every three both = set(item).intersection(item_2) common_a.append([item.index(x) for x in both]) common_b.append([item_2.index(x) for x in both])
The thing is that this checks all elements of the sublists of the first list (from position 5 and onwards) with every 3rd sublist of the second list, BUT I want to check:
- sublist at index 5 in list1 with sublist at index 0 in list2 only
- sublist at index 6 in list1 with sublist at index 3 in list2 only
- sublist at index 7 in list1 with sublist at index 6 in istt2 only and so on
I hope what i wrote makes sense ... any suggestions are MORE than welcome
The problem is that you've employed nested loops to handle a single parameter. There's a straightforward linear relationship between the two indices. For instance:
for idx1 in range(5, len(data_db)): idx2 = 3*(idx1 - 5) if data_db[idx1] == data_ga[idx2]: # Your code
Even better, just
zip together the elements you need:
for item1, item2 in zip(data_db[5:], data_ga[::3]): if item1 == item2: # Your code
python compare items in 2 list of different length, How do I compare two lists of different sizes Python? Sometimes we need to link two list from the point of view of their index elements and this kind of problem comes mostly in places where we need to display in formatted form the linkage of two lists with one another. This a very specific problem, but can be useful whenever we need a possible solution.
You don't need two for loops/nested loops. One Loop will solve your problem, in which each iteration of the loop will increment list1 with 1 and list2 with 3.
Python Compare Two Lists, boolean type is a subclass of int and False equals 0 , True equals 1 . The cmp () function is a built-in method in Python used to compare the elements of two lists. The function is also used to compare two elements and return a value based on the arguments passed. This value can be 1, 0 or -1. Note: cmp () build to function for python version 2, In python version 3 it is not available.
Based on Prune's answer this is how i finally solved it:
# ----------- common_a =  common_b =  hits_idx1 =  hits_idx2 =  for idx1 in range(4, len(data_db)): idx2 = 3*(idx1 - 4) if (idx2 < len(data_ga)): #dont get over the 2nd list's length both = set(data_db[idx1]).intersection(data_ga[idx2]) if both: # if it contains elements hits_idx1.append(idx1) # save the indexes of the hits external list data_db hits_idx2.append(idx2) # save the indexes of the hits external list data_ga common_a.append([data_db[idx1].index(x) for x in both]) #the indexes of the internal lists common elements common_b.append([data_ga[idx2].index(x) for x in both])
How to Compare Two Lists in Python, How do you compare two elements in a list Python? Python | Accessing all elements at given list of indexes Accessing an element from its index is easier task in python, just using the  operator in a list does the trick. But in certain situations we are presented with tasks when we have more than once indices and we need to get all the elements corresponding to those indices.
Difference Between Two Lists using Python Set & Without Set, and return a value based on the arguments passed. This value can be 1, 0 or -1. Using sum () + zip (), we can get sum of one of the list as summation of 1 if both the index in two lists have equal elements, and then compare that number with size of other list. This also requires first to check if two lists are equal before this computation. It also checks for the order.
Comparing 2 different list of lists, How do you compare two lists of strings in Python? Varun September 16, 2019 Python: How to find all indexes of an item in a List? 2019-09-16T09:42:44+05:30 List, Python No Comment In this article we will discuss different ways to find indexes of all occurrences of an item in the list.
Compare list of lists - Lists-Logic, are equal, then the elements would reside at the same index positions. Compare & get differences between two lists in Python; Python: check if two lists are equal or not ( covers both Ordered & Unordered lists) Python Pandas : How to convert lists to a dataframe; How to Merge two or more Dictionaries in Python ? Pandas : Convert a DataFrame into a list of rows or columns in python | (list of lists) Python: Read a