Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从列表中删除多个元素并将其保留为元组,然后将元组指定为字典的键?_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 如何从列表中删除多个元素并将其保留为元组,然后将元组指定为字典的键?

Python 如何从列表中删除多个元素并将其保留为元组,然后将元组指定为字典的键?,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,假设有一个字符串列表。我想从列表中获取n个字符串,并将其存储为元组。然后我想制作一个字典,其中最近创建的元组是关键。最快的方法是什么 创建一个新词典: dic = dict() 创建一个列表: list = ["banana","apple","orange","watermelon"] 将元组分配给dict的键: dic[(list[0],list[1])] = 3 然后可以打印结果: print(dic[("banana","apple")]) 3 提供示例数据、预期输出,并向我们展

假设有一个字符串列表。我想从列表中获取n个字符串,并将其存储为元组。然后我想制作一个字典,其中最近创建的元组是关键。最快的方法是什么

创建一个新词典:

dic = dict()
创建一个列表:

list = ["banana","apple","orange","watermelon"]
将元组分配给dict的键:

dic[(list[0],list[1])] = 3
然后可以打印结果:

print(dic[("banana","apple")])
3

提供示例数据、预期输出,并向我们展示您尝试过的非快速方法。您能提供一个您尝试过的示例吗?