第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python中用于映射表的數(shù)據(jù)結(jié)構(gòu)

Python中用于映射表的數(shù)據(jù)結(jié)構(gòu)

江戶川亂折騰 2022-09-06 21:22:51
我需要更有經(jīng)驗的開發(fā)人員為我的數(shù)據(jù)結(jié)構(gòu)提供一些輸入。我想做什么?我正在為映射表編寫轉(zhuǎn)換器。它有效,但我認(rèn)為有一種更好的方法來設(shè)置結(jié)構(gòu)。當(dāng)然,業(yè)務(wù)邏輯應(yīng)該易于設(shè)置,但同時,結(jié)構(gòu)仍應(yīng)可讀。有人有建議嗎?映射表:System A    | 4 | 5 |5* |6x | 6x* | 6y | 6y* |  6c | 6c* | 7x | 7x* |  System B    |   | 4 |5  |6x | 6x* | 6y | 6y* |  6c | 6c* | 7x | 7x* |System C    |   X0  |X1 |X2 | X3  |    X4    |  X5 |    X6    | X7  |有三種不同的分級系統(tǒng)(A,B,C)。每個等級都由彼此大致相當(dāng)?shù)牡燃壗M成。例如?!癤4”(系統(tǒng) C)可以轉(zhuǎn)換為“6y”或“6y”*(系統(tǒng) A)例如?!?c”(系統(tǒng)B)可以轉(zhuǎn)換為“X5”(系統(tǒng)C)當(dāng)前結(jié)構(gòu)mapping = {    "name": ["System A", "System B", "System C"],    "grade": {        0: ["4", "", "X0"],        1: ["5", "4", "X0"],        2: ["5*", "5", "X1"],        # ... and so on.     }}# the current standard is "System A"input_system = 0# the input is the index number for the gradeinput_grade = 4# expected output: "In system A it is 6x*."print(f"In {mapping.name[input_system]} it is {mapping.grade[input_grade][input_system]}.")
查看完整描述

1 回答

?
翻過高山走不出你

TA貢獻(xiàn)1875條經(jīng)驗 獲得超3個贊

您擁有的是一個三向映射,它最簡潔地使用元組列表來表示:


# Not clear if you want "" or None to represent the non-existent System-B

# equivalent of System-A "4"

mapping = [

    ("4", "", "X0"),

    ("5", "4", "X0"),

    ("5*", "5", "X1"),

    ...

]

然后,您可以根據(jù)元組列表定義 6 個 X->Y 映射中的任何一個。


from operator import itemgetter


a_to_b = dict(map(itemgetter(0, 1), mapping))

b_to_a = dict(map(itemgetter(1, 0), mapping))

a_to_c = dict(map(itemgetter(0, 2), mapping))

c_to_a = dict(map(itemgetter(2, 0), mapping))

b_to_c = dict(map(itemgetter(1, 2), mapping))

c_to_b = dict(map(itemgetter(2, 1), mapping))

您可以通過將 O(1) conversons 換成 O(n) 查找來最小化存儲(并不是說我們一開始就使用了很多)。


def convert(sys_from, sys_to, grade):

    sys_from = {"A": 0, "B": 1, "C": 2}[sys_from]

    sys_to = {"A": 0, "B": 1, "C": 2}[sys_to]


    for grade in mapping:

        if grade[sys_from] == grade:

            return grade[sys_from]


查看完整回答
反對 回復(fù) 2022-09-06
  • 1 回答
  • 0 關(guān)注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號