简体   繁体   English

如何从 python 中的二维列表打印 1 到多个项目

[英]How to print 1 to many items from a 2D list in python

I have a 2D list object fps containing 4 items and the length of the object is 2006 (2006 rows and each row contains 4 elements).我有一个包含 4 个项目的 2D 列表 object fps ,object 的长度是 2006(2006 行,每行包含 4 个元素)。 The Object looks like Object 看起来像

['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

I want to print only the 4th items .我只想打印第4th items So my code is所以我的代码是

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

Output looks like Output 看起来像

<rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f3170>
<rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f31c0>
<rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f3210>

Now I want to print all 4th elements as 1 to many relation types.现在我想将所有第 4 个元素打印为1 to many关系类型。 More specifically, fps[n][3] will be printed with all rest of the 2005 elements (only 4th column) and so on.更具体地说, fps[n][3]将打印2005元素的所有 rest(仅第 4 列)等等。 So I wrote this所以我写了这个

for n in range(len(fps)-1): 
  print(fps[n][3], fps[n+1:][3]) #Can I use for mapping

But, my code is fine for the first print but giving an error for the second one.但是,我的代码第一次打印时没问题,但第二次打印时出错。 The 2nd one giving the whole 2d list .第二个给出整个2d list Output looks like Output 看起来像

    <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>]
<rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f34e0> ['15617', 'OC[C@H](O)[C@@H](O)[C@H](O)[C@H](O)CO[C@H]1O[C@H](CO[C@H]2O[C@H](CO)[C@@H](O)[C@H](O)[C@H]2O)[C@@H](O)[C@H](O)[C@H]1O.[Fe+3]', <rdkit.Chem.rdchem.Mol object at 0x7fcfa0a455d0>, <rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f3620>]

My expected output is like我预期的 output 就像

<rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f3170> <rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f31c0>
<rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f3170> <rdkit.DataStructs.cDataStructs.ExplicitBitVect object at 0x7fcfa09f3210>  

For a more general way my expected output对于更一般的方式,我期望 output

element_1 element_2
element_1 element_3 
element_1 element_4
........
element_1 element_2006
element_2 element_1
element_2 element_3

...... ……

Is it possible to print in this way?可以这样打印吗?

The code "print(fps[n][3], fps[n+1:][3])" will first print element at index [3] of the sublist of element [n[ of the main/parent list.代码“print(fps[n][3], fps[n+1:][3])”将首先打印主/父列表元素 [n[ 的子列表索引 [3] 处的元素。 But after that, it'll print the remaining parent list as the code says "[n+1:]".但在那之后,它将打印剩余的父列表,如代码所示“[n+1:]”。 The semicolon tells the program to take the list from element n+1 till the end.分号告诉程序从元素 n+1 到末尾获取列表。 Instead, try running a nested loop to print a 1-many relation, something like:相反,尝试运行嵌套循环来打印一对多关系,例如:

for one in fps:
    for many in fps:
        print(one[3], many[3])

The outer loop will take an element from the main list and pass it to the inner loop.外循环将从主列表中取出一个元素并将其传递给内循环。 The inner loop will keep the initial element passed from the outer loop and take each element from the main loop and print the third element of both until each element is done.内部循环将保留从外部循环传递的初始元素,并从主循环中获取每个元素并打印两者的第三个元素,直到完成每个元素。 Then, the outer loop will pass the next element to the inner loop and so on.然后,外循环会将下一个元素传递给内循环,依此类推。

If what you want is such that for this list:如果你想要的是这个列表:

fps = [[0,0,0,1], [0,0,0,2], [0,0,0,3], [0,0,0,4]]

your expected output should be this:您预期的 output 应该是这样的:

1 2
1 3
1 4
----------
2 1
2 3
2 4
----------
3 1
3 2
3 4
----------
4 1
4 2
4 3
----------

So you can do this:所以你可以这样做:

fps = [[0,0,0,1], [0,0,0,2], [0,0,0,3], [0,0,0,4]]

for index_i,i in enumerate(fps): 
    one_fourth_item = i[3]
    for index_j,j in enumerate(fps):
        many_fourth_item = j[3]
        if index_i != index_j:
            print(one_fourth_item, many_fourth_item)
            
    print("----------")

I'll try to explain the problem with the code you called "2nd one" in your question ( fps[n+1:][3] ):我将尝试用您在问题中称为“第二个”的代码来解释问题( fps[n+1:][3] ):

First, let simplify it as fps[n:][3]首先,将其简化为fps[n:][3]

The part fps[n:] wil return a slice of the source list. fps[n:]部分将返回源列表的一部分。

So:所以:

for n == 0: [[0, 0, 0, 1], [0, 0, 0, 2], [0, 0, 0, 3], [0, 0, 0, 4]]对于 n == 0: [[0, 0, 0, 1], [0, 0, 0, 2], [0, 0, 0, 3], [0, 0, 0, 4]]

for n == 1: [[0, 0, 0, 2], [0, 0, 0, 3], [0, 0, 0, 4]]对于 n == 1: [[0, 0, 0, 2], [0, 0, 0, 3], [0, 0, 0, 4]]

for n == 2: [[0, 0, 0, 3], [0, 0, 0, 4]]对于 n == 2: [[0, 0, 0, 3], [0, 0, 0, 4]]

for n == 3: [[0, 0, 0, 4]]对于 n == 3: [[0, 0, 0, 4]]

The part [3] will return the third element of the slice (from fps[n:] ) [3]部分将返回切片的第三个元素(来自fps[n:]

So, for n == 0, fps[n:][3] equals to: [[0, 0, 0, 1], [0, 0, 0, 2], [0, 0, 0, 3], [0, 0, 0, 4]][3] , and that is [0,0,0,4]因此,对于 n == 0, fps[n:][3]等于: [[0, 0, 0, 1], [0, 0, 0, 2], [0, 0, 0, 3], [0, 0, 0, 4]][3] ,也就是[0,0,0,4]

The problem is that for n == 1 (in this example), the last index is 2, and so you get IndexError: list index out of range问题是对于 n == 1(在这个例子中),最后一个索引是 2,所以你得到IndexError: list index out of range

So there is at least these 2 problems in your code: with fps[n:][3] you don't select the "4th item" of the sub-list, but the "4th item" of the source list, and when "n" is greater than the last index you get IndexError: list index out of range .因此,您的代码中至少存在这两个问题:使用fps[n:][3]时,您不是 select 子列表的“第 4 项”,而是源列表的“第 4 项”,以及何时“n”大于您获得的最后一个索引IndexError: list index out of range

And for the +1 part in fps[n+1:][3] , perhaps you incremented n because you wanted to skip the "one" element, in order to avoid printing "element_1 element_1" or "element_2 element_2"?对于fps[n+1:][3]中的+1部分,也许您增加了n是因为您想跳过“one”元素,以避免打印“element_1 element_1”或“element_2 element_2”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM