Use python to compare values. If they are the same, get the key, but the number of keys is different?

Instead of a list, build a dictionary where the keys are the list of values you are matching, and the values are what you want to return. You can then pass in the list of keys you want to pull the resulting values from the dictionary for any length/order/source of items and get the results you are after in the order you need.

1 Like

您好,真的很感謝您提供寶貴的意見,但,我使用字典後,情況仍是一樣,真令人沮喪.

Perhaps this:

# The inputs to this node will be three lists: IN[0], IN[1], and IN[2]
listA = IN[0]  # List of keys
listB = IN[1]  # List of values
listC = IN[2]  # List of values to be matched

# Create a dictionary from listA and listB
dictAB = dict(zip(listA, listB))

# Initialize the output list
output = []

# Loop over the items in listC
for item in listC:
    # If the item is found in the dictionary values, get the corresponding key
    if item in dictAB.values():
        # Find the key for the current item and append to the output list
        key = [key for key, value in dictAB.items() if value == item]
        # Assuming you want only the first match, not all possible matches
        if key:
            output.append(key[0])

# Assign your output to the OUT variable.
OUT = output

You will get best results by describing what you want, giving sample inputs and desired outputs for your task.

Det finns inget behov av Python här och du kommer faktiskt att sakta ner din körhastighet genom att använda den om vi har fullt sammanhang.

Du har redan din ordbok. Använd noden Dictionary.ValueAtKey.

2 Likes

kia ora, me whai whakaaro koe mo te reo huinga whaimana hei awhina i a koe ki te tiki tohutohu.

感謝您的建議,但我使用了你的建議,卻依然無法得到相同的筆數.我有將程式碼使用在spyder作測試,得到的結論卻是正確,我懷疑是dynamo的python script有問題.所以不知能否以dynamo的節點取代python script.謝謝.

List inputs and desired outputs for your task.

FirstIndexOf might be useful if you just want to use nodes.

Sorry, I want to ask again, I checked the value of i in listA, and the value of i that appears is consistent with the value of List.GetItemAtIndex.

However, when the execution reaches if i in d:, compared with the value of List.GetItemAtIndex, there is an extra B10, a total of 23 extra values. I don’t understand what went wrong. I hope someone can help. I understand. Thank you.

@Cloud
The forum language is english.
We don’t understand if you use your own language, in the same way that you wouldn’t understand if I wrote in Swedish.
If you don’t feel familiar with english, please use a translator before you post.

2 Likes

In swedish ? then we are totally sure nobody understand :wink: :wink:

1 Like

Thank you for your reminder, I have converted it into English. Thank you.

1 Like

:smiley:
Or maybe Danish would have been even more clear :wink:

1 Like

absolutly, but it isnt a language more just a sound :wink: :wink: we even dont self understand :wink:

Why not use the Dictionary value at key node?

here an solution

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

def get_key_by_value(valueTest):
    return next((key_ for key_, value_ in mydict.items() if valueTest in value_), None)

listA = IN[0]  # List of keys
listB = IN[1]  # List of values
listC = IN[2]  # List of values to be matched

# Create a dictionary from listA and listB
mydict = dict(zip(listA, listB))

OUT = [get_key_by_value(value) for value in listC]

Note:
consider providing test data (list or dyn) for forum users who read you

2 Likes

I am very grateful to c.poupin and everyone for providing me with their opinions. This problem that has been bothering me for a long time has finally been solved. Thank you all again. I want to say: It is great to have you. Thank you very much.
It’s great to have this forum ^^

2 Likes