Why my 2D list is not working?

Hello Good People,

Now, I have a 2D list object fps containing 4 items and length is 2006. Object looks like

['0012', 'CCN[C@H]1CN', <rdkit.Chem.rdchem.Mol object at 0x7fea177a0260>, <rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fea0f2fd030>]
['0015', 'CCN[H@H]1CN', <rdkit.Chem.rdchem.Mol object at 0x7fea177a0260>, <rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fea0f2fd030>]
  ... so on

If I want to print only the 4th elements with all the next 4th elements of the list.

for n in range(len(fps)-1):
    print(fps[n][3], fps[n+1:][3])

The first one is printing correctly but the second one printing all elements (1st, 2nd, 3rd, and 4th). Printing the whole elements of the object:

<rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fde5df8f300> ['00299', 'Nc1nc(=O)c2ncn(CCC(CO)CO)c2[nH]1', <rdkit.Chem.rdchem.Mol object at 0x7fde5dfde530>, <rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fde5df8f440>]

But I need only the 4th. Could you tell me why this is happening?

Any kinds of suggestions are appreciable.

Hello Akib, and welcome.

I think you have something like this? A list fps containing many
objects:

fps = [a, b, c, d, ...]

where each object a, b etc is an “ExplicitBitVect” object:

<rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fea0f2fd030>

although it doesn’t matter what they are, they could be anything. And
you then take one object, plus the remaining objects, like this:

for n in range(len(fps)-1):
    obj = fps[n]
    remaining = fps[n+1:]
    s = function(obj, remaining)

Note that you are missing the last object: you stop processing one short
of the end of the list.

A better, more Pythonic way of writing that would be:

for n, obj in enumerate(fps):
    remaining = fps[n+1:]
    s = function(obj, remaining)

This version will process the entire list, including the last item,
where remaining is an empty list.

In your second example, your fps list is the same, but each item is no
longer an ExplicitBitVect object, but a list:

['0012', 'CCN[C@H]1CN', Mol object, ExplicitBitVect object]

So when you extract the individual objects from fps, you then need to
extract again to get the ExplicitBitVector:

# Your version, skips the last item.
for n in range(len(fps)-1):
    obj = fps[n]  # gets a list of four items
    obj = obj[3]  # gets the fourth item from the list
    # remember that Python starts counting at 0, not 1
    remaining = fps[n+1:]
    s = function(obj, remaining)


# My version
for n, obj in enumerate(fps):
    s = function(obj[3], fps[n+1:])

Does that answer your question? I hope that helps.

Hello @steven.daprano Thank you for your kind reply and sorry for the late reply.

However, when I am using your code to print the data, the output was approximately the same (like as previous).

However, could you tell me why you have written s = function()? Because I just want to print the value, so I wrote print(obj, remaining)

This is the line you are asking about:

I took the liberty to change your example slightly to make clear what happens.
>>> fps = [[1, 2, 3, 4], [5, 6, 7, 8]]
We than look at the first thing you printed:
>>> fps[0][3]
4
Good, what you expected. Than we look at the second bit:
>>> fps[1:]
[[5, 6, 7, 8]]
Now that is probably not what you expected. It is a list which contains the second item. Why? [n+1:] takes a copy of the list from the next item. So when you ask for the third item ([n+1:][3]) it is the fifth list in the original list. That is what you see.

1 Like

I have written “s = function(…)” because “s =
DataStructs.BulkTanimotoSimilarity(…)” is too long and annoying to
write, so I just abbreviated it. Sorry for the confusion.

Also, in your original post (the one sent out by email) you wrote this
code snippet:

s = DataStructs.BulkTanimotoSimilarity(fps[n], fps[n+1:])

but later changed that to:

s = DataStructs.BulkTanimotoSimilarity(fps[n][3], fps[n+1:][3])

which changes the meaning of the code. Sorry, I did not see that change
until now, because I was working off the email.

I don’t understand your complaint that the output is the same as your
output? That’s a good thing, isn’t it? You had two pieces of code:

  • the first that was working the way you wanted

  • the second was not

and I understood that the first piece of code was correct. So I’m afraid
I don’t understand your question now.

Perhaps you should read this:

and see if you can simplify the problem to just built-in Python objects
and functions, such as extracting items from lists of tuples instead of
these mysterious functions like DataStructs.BulkTanimotoSimilarity and
ExplicitBitVect.

Otherwise I’m afraid that I don’t understand what problem you are trying
to solve, sorry.

1 Like