亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何將以下數據存儲在適當的數據結構中?

如何將以下數據存儲在適當的數據結構中?

皈依舞 2023-06-28 15:24:33
我有以下輸入數據,需要存儲在適當的數據結構中。ElectronicsElectronics > Arcade EquipmentElectronics > Arcade Equipment > Basketball Arcade GamesElectronics > Arcade Equipment > Video Game Arcade CabinetsElectronics > AudioElectronics > Audio > Audio AccessoriesElectronics > Audio > Audio Accessories > Audio & Video Receiver AccessoriesElectronics > Audio > Audio Accessories > Headphone & Headset AccessoriesElectronics > Audio > Audio Accessories > Headphone & Headset Accessories > Headphone Cushions & TipsApparel & Accessories > Clothing > Activewear > Hunting Clothing > Hunting & Tactical PantsApparel & Accessories > Clothing > Activewear > Martial Arts ShortsApparel & Accessories > Clothing > Activewear > Motorcycle Protective ClothingApparel & Accessories > Clothing > Baby & Toddler ClothingAnimals & Pet Supplies > Pet Supplies > Pet Grooming Supplies > Pet Nail Polish其中應該提供以下信息。我已經按照以下方式使用 python3 實現了,其時間復雜度為 O(n),其中 n 是文件中的行數。是否有其他有效的解決方案來存儲上述信息。def get_data(file_path):    try:        if not os.path.exists(file_path):     # check file exist or not            raise FileNotFoundError    except FileNotFoundError:        print('please provide valid file path')        exit()    inventory = {}    with open(file=file_path, mode='r', encoding='utf-8') as file_descr:        try:            for line in file_descr:                line = line.strip()                llist = line.split('>')         # list of keys                for i in range(1, len(llist) + 1):     # check entry of each possible key in inventory                    key = ('>'.join(llist[:i])).strip()                    if inventory.get(key):                        inventory[key] += 1                    else:                        inventory[key] = 1        except Exception as e:            print('not able to read content', e)            exit()    return inventory
查看完整描述

1 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

為您的模型創建類并使用樹結構。



查看完整回答
反對 回復 2023-06-28
  • 1 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號