「Python」OrderedDictオブジェクトの値を更新、削除するサンプル

書式
OrderedDictオブジェクト名.update(key=value,xxx )
del OrderedDictオブジェクト名「キー」
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import collections
cft = collections.OrderedDict()
#初期値を設定
cft['key1'] = 501
cft['key2'] = 602
cft['key3'] = 703
print(cft['key3'])
#値を変更
cft['key2'] = 150
print(cft)
#値を追加
cft.update(key4=84, key5=95)
print(cft)
#値を削除
del cft['key4'], cft['key5']
print(cft)
import collections cft = collections.OrderedDict() #初期値を設定 cft['key1'] = 501 cft['key2'] = 602 cft['key3'] = 703 print(cft['key3']) #値を変更 cft['key2'] = 150 print(cft) #値を追加 cft.update(key4=84, key5=95) print(cft) #値を削除 del cft['key4'], cft['key5'] print(cft)
import collections
cft = collections.OrderedDict()

#初期値を設定
cft['key1'] = 501
cft['key2'] = 602
cft['key3'] = 703

print(cft['key3'])

#値を変更
cft['key2'] = 150
print(cft)

#値を追加
cft.update(key4=84, key5=95)
print(cft)

#値を削除
del cft['key4'], cft['key5']
print(cft)

結果
703
OrderedDict([('key1’, 501), ('key2’, 150), ('key3’, 703)])
OrderedDict([('key1’, 501), ('key2’, 150), ('key3’, 703), ('key4’, 84), ('key5’, 95)])
OrderedDict([('key1’, 501), ('key2’, 150), ('key3’, 703)])

Python

Posted by arkgame