finding the index of the elements appended to new list from its old list - python 2

Asiv

I have two lists (A and B) with some common elements in between them. I want to create a new list (C) with all the common elements from A that are also in B, as well have the index of where the elements was originally in A.

The two lists are as fallows,

A=[[[a, b], [c, d]],
   [[e, f], [f, t]],
   [[a, b], [c, d]],
   [[g, h], [t, r]]]
B=[[[a, b], [c, d]],
   [[g, h], [t, r]]]

The code Im using to find the common elements are as fallows,

C=[[ ]]
for i in range(len(A)):
    if A[i] in B:
        C[0].append(A[i])

The output is perfect, except I'd like to add in the index of the elements in C from where there come from in A (even if there are duplicates), possible besides each elements or even in a new list, although Im not sure the code that would do that, so any help is appreciated.

Austin

You can replace this line of code:

C[0].append(A[i])

with:

C[0].append([A[i], i])

which forms C with each element in the format [element, index].

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make a new nested list from an old nested list by merging all the elements at the same index together

Use index of one array to extract value from list to be appended into new array (Python)

Python given dict of old index: new index move multiple elements in a list

How to create a new List with unique elements from an old List

How to create a new list by pairing the elements on the basis of index from 2 different lists in Python?

finding common elements in 2 list of dicts in python

finding the index of elements in nested list

How to make new list of the numbers not appended into its own list?

Python list values updated while appended new values, old values lost

Passing elements to another page from an appended list

How do I create a new list with specific items from an old list in Python 2?

Finding an item in a list and returning its index - OCaml

Finding the minimum value of a nested list and its index

Enumeration of a list for finding elements in Python

Bisect a Python List and finding the Index

How to know combination of elements from its index in list of all combionations in python

Remove multiple elements from a list of index with Python

Get elements from list in python by ¿index?

inserting all the elements from a list into specific index of sublist from a second list - python 2

Finding index of highest element from a list of list

python: Finding a index in one list and then replacing second list with the item from a index in first list

Python 2.7 : Finding common elements based on one key value from 2 list of dictionaries

finding a proper event for new appended elements

Recreating new list with old elements but different orders

Obtaining top 2 max values from a list and finding its corresponding value from 2nd list

Finding list of highest elements in 2D array: Python 3

Add a range of elements to a list at a given index, replacing the its contents from that index in the list and resizing it (if need it)

Python - Match elements from different list with irregular length to new list

python list not getting appended

TOP Ranking

HotTag

Archive