0

Pythonで「guids」(rhinoのポイントID)の辞書を作成し、割り当てた値に応じてそれらを取得し、その値を変更して復元する最も効率的な方法を見つけようとしています。辞書で。1つの落とし穴は、Rhinoceros3dプログラムでは、ポイントにランダムに生成されたID番号がありますが、これは私にはわかりません。そのため、指定した値に応じてのみ呼び出すことができます。

辞書は正しい方法ですか?GUIDはキーではなく値にする必要がありますか?

非常に基本的な例:

arrPts=[]
arrPts = rs.GetPoints()  # ---> creates a list of point-ids

ptsDict = {}
for ind, pt in enumerate(arrPts):
    ptsDict[pt] = ('A'+str(ind))

for i in ptsDict.values():
    if '1' in i :
        print ptsDict.keys()

上記のコードで、すべてのキーではなく、値が「1」のキーを出力するにはどうすればよいですか?次に、キーの値を1からたとえば2に変更しますか?

私が正しい方向に進んでいることを知って、一般的な質問についても助けていただければ幸いです。

ありがとう

パブ

4

2 に答える 2

1

割り当てたものでGUIDを検索したいように見えるので、GUIDをキーではなく値にしたいと思います。...しかし、実際にはユースケースによって異なります。

# list of GUID's / Rhinoceros3d point ids
arrPts = ['D20EA4E1-3957-11d2-A40B-0C5020524153', 
          '1D2680C9-0E2A-469d-B787-065558BC7D43', 
          'ED7BA470-8E54-465E-825C-99712043E01C']

# reference each of these by a unique key
ptsDict = dict((i, value) for i, value in enumerate(arrPts))
# now `ptsDict` looks like: {0:'D20EA4E1-3957-11d2-A40B-0C5020524153', ...}

print(ptsDict[1]) # easy to "find" the one you want to print 

# basically make both keys: `2`, and `1` point to the same guid 
# Note: we've just "lost" the previous guid that the `2` key was pointing to
ptsDict[2] = ptsDict[1]

編集:

タプルを dict のキーとして使用する場合、次のようになります。

ptsDict = {(loc, dist, attr3, attr4): 'D20EA4E1-3957-11d2-A40B-0C5020524153',
           (loc2, dist2, attr3, attr4): '1D2680C9-0E2A-469d-B787-065558BC7D43',
           ...
          }

ご存知のように、タプルは不変であるためchange、dict のキーを取得することはできませんが、1 つのキーを削除して別のキーを挿入することはできます。

oldval = ptsDict.pop((loc2, dist2, attr3, attr4))  # remove old key and get value
ptsDict[(locx, disty, attr3, attr4)] = oldval  # insert it back in with a new key

複数の値に対する 1 つのキー ポイントを保持するには、リストまたはセットを使用して GUID を含める必要があります。

{(loc, dist, attr3, attr4): ['D20E...', '1D2680...']}
于 2012-11-22T21:33:33.707 に答える