簡體   English   中英

Python function 返回維度數組的索引

[英]Python function to return index of a n dimensional array

So I have a function that finds the position of a class in an dimensional array, and another function that takes that value and transforms it (or at least tries to) into an index that can be used to access and return that class.

def get_class(condition1, condition2):
    key = ""
    if(condition1):
        key += "1 "
    else:
        key += "0 "
    if(condition2):
        key += "1 "
    else:
        key += "0 "
    key = get_key(key)

    return classes[key]

它的工作原理是在運行過程中添加一個字符串,每個 1 表示在數組中的特定軸上移動。

function 將其轉換為索引:

def get_key(value):
    key = value.split()
    for character in key:
        character = int(character)
        print(type(character))

    return key

它應該將字符串轉換為整數,然后在數組上返回 position,但我得到的只是這個錯誤:

TypeError: list indices must be integers or slices, not list

有沒有更好的方法來做到這一點,或者我做錯了什么?

編輯:

樣本數據:

條件 1 = 真 條件 2 = 假

類 = [[6, 8], [21, 754]]

預期結果:21,鍵為 [1][0]

問題是您的key字符串未在get_key() function 中更新。 您正確地將密鑰拆分為整數,但您沒有使用這些整數。

如果您的輸入鍵是“1 0”,那么您也會返回“1 0”,當您調用classes[key]classes["1 0"]時程序會崩潰。

編輯:只要您僅限於二維 arrays 您可以使用:

def get_key(value):
    key = value.split()
    for character in key:
        yield int(character)

def get_class(condition1, condition2):
    key = ""
    if(condition1):
        key += "1 "
    else:
        key += "0 "
    if(condition2):
        key += "1 "
    else:
        key += "0 "

    x, y = get_key(key)
    return classes[x][y]

如果添加更多維度,您將需要從get_key()指定更多返回值,但我不知道如何使其通用。 我希望這有幫助。

除了一個小錯誤,你做的一切都很好。

get_key function 中,您正在嘗試轉換和打印 integer 值。 但是在返回值時,您正在返回變量key

看到您的變量key具有從value.split()返回的值。 所以key中的值是一個列表,您正在返回該列表。

現在要訪問類列表,您需要正確的classses索引,但您將此列表作為索引傳遞。

因此錯誤。

現在解決方案部分:

def get_class(condition1, condition2):
    key = ""
    if(condition1):
        key += "1 "
    else:
        key += "0 "
    if(condition2):
        key += "1 "
    else:
        key += "0 "
    key = get_key(key)

    return classes[key[0]][key[1]]


def get_key(value):
    key = value.split()
    int_list = []
    for character in key:
        character = int(character)
        print(type(character))

        int_list.append(character)
    return int_list

上述解決方案是通過修改您的代碼來完成的。

但是如果你想看到它可以更容易和更清楚地完成:

def get_class(condition1, condition2):
    if(condition1):
        key1 = 1
    else:
        key1 = 0

    if(condition2):
        key2 = 1
    else:
        key2 = 0

    return classes[key1][key2]

這將產生與預期相同的 output ,但如果您有任何特定要求在不同的函數中實現它並將密鑰存儲為字符串,那么您可以使用之前的代碼 go 。

希望能幫助到你。 干杯。

根據您的代碼,到達return classes[key]時, key的類型為 list :

>>> key = "10"
>>> key.split()
['10']

這導致

>>> classes = [[6, 8], [21, 754]]
>>> classes [key]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str

編輯:您可以通過將get_class function 中的索引存儲為列表中的整數來解決它。

classes = [[6, 8], [21, 754]]
keys = [1, 0]
for key in keys:
    classes = classes[key]
print( "%s" % classes )

暫無
暫無

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

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