簡體   English   中英

在 Python 中定位特定索引並打印相應的值

[英]Locating specific indices and printing corresponding values in Python

I有一個包含兩個索引[0,5][5,8]的列表。 我想在Ii中找到這些索引並根據Iv打印相應的值。 我介紹了當前和預期的輸出。

import numpy as np

I=[[0,5],[5,8]]

Ii=[np.array([[[ 0,  2],
        [ 0,  5],
        [ 2,  5],
        [ 5,  8],
        [ 5, 10],
        [ 8,  9],
        [ 8, 11],
        [ 9, 11],
        [10,  8]]])]

Iv=[np.array([[[0.45202266977297145],
         [0.9977806946852882 ],
         [0.5228951173127738 ],
         [1.083230383898751  ],
         [0.5533588101522955 ],
         [0.5778527989444576 ],
         [1.2288418160926498 ],
         [0.5274353540288571 ],
         [0.5818783538996267 ]]])]

A=[Iv for I in Ii]
print(A)

當前的 output 是

[[array([[[0.45202266977297145],
        [0.9977806946852882 ],
        [0.5228951173127738 ],
        [1.083230383898751  ],
        [0.5533588101522955 ],
        [0.5778527989444576 ],
        [1.2288418160926498 ],
        [0.5274353540288571 ],
        [0.5818783538996267 ]]])]]

預期的 output 是

[0.9977806946852882 ],
[1.083230383898751  ],

您可以使用:

[Iv[0][0][idx][0] for idx, c in enumerate(Ii[0][0]) if list(c) in I]

結果:

[0.9977806946852882, 1.083230383898751]

此代碼適用於您的特定情況,其中 Iv 僅包含 1 個數組,也包含 Ii。 您需要稍微更改它以適應列表中的 1 個以上的數組。

如果 Iv 和 Ii 包含超過 1 個數組:

[score[0][idx][0] for (array, score) in list(zip(Ii, Iv)) for idx, coord in enumerate(array[0]) if list(coord) in I]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM