Python将元组列表转换为复合键字典

问题描述

我正在尝试将元组列表转换为复合键字典,以便可以自定义按首尾排序:

def phone(first,last,number):
    directory = dict()
    while True: 
        first = input('enter first: ')
        if first == 'done':
            print('done')
            break
        last = input('enter last: ')
        number = input('enter number: ')
        directory[last,first] = number
        new = {((last,first),number)}
        directory.update(new)

    print(directory)   # unit test

phone(first,number)

输出:

enter first: 'ricky'
enter last: 'bobby'
enter number: 1111
Traceback (most recent call last):
  File "py4e.tuples.directory.function.1.py",line 21,in <module>
    phone('first','last','number')
  File "py4e.tuples.directory.function.1.py",line 17,in phone
    directory.update(new)
TypeError: cannot convert dictionary update sequence element #1 to a sequence

我为此花了多少时间感到as愧。 你会怎么做? 所有输入将不胜感激。 谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)