python - 使用 Python 字典/列表

标签 python list dictionary

在 Python 2.4 中是否有更好的方法来执行以下操作?

if id in id_dict:
    id_dict[id].append(skill)
else:
    id_dict[id] = [skill]

最佳答案

如果 id 在字典中,返回它的值。如果不是,则插入值为 []id 并返回 []

id_dict.setdefault(id, []).append(skill)

关于python - 使用 Python 字典/列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19406193/

相关文章:

c++ - 如何迭代这个 map ?

python - 获取 screen 运行的命令的 pid

python - Flask 和 MongoDB - For 循环不工作

python - 同时运行两个接受参数和返回列表的函数?

python - 是否有可能在循环中有不稳定的步骤?

python - 如何使用函数将字典的值添加到Python中

Python:列表列表到字典

python - 使用列和行索引作为变量填充 pandas 数据框

python - 打印或返回括号之间的字符串

list - ACL2如何保留一个列表的一部分?