0

次のような辞書があるとします。

sample_dict = {'test':['a', 'b', 'c', 'd', 'e']}

リスト内のアイテムを別のものに変更するにはどうすればよいですか。

例:

def replace_item(sample_dict, item, new_item):
      pass

>>> replace_item({'test':['a', 'b', 'c', 'd', 'e']}, 'd', 'f')
>>> {'test':['a', 'b', 'c', 'f', 'e']}
4

2 に答える 2

1

ディクショナリにはリスト自体ではなくリストへの参照のみが保存されるため、リストを取得して変更するだけです。

于 2013-11-10T02:02:50.387 に答える