简体   繁体   English

Python:如何遍历两个列表,一个用于键,一个用于值,并将其放入新的空字典中?

[英]Python: How can loop over two lists one for key and one for value and place that into a new empty dictionary?

Here is the data I have I want to loop over the two lists the symbol list as the key and symbol_name as the value how do I do it with using the loop这是我想要在两个列表中循环的数据,符号列表作为键,符号名称作为值我如何使用循环来做到这一点

calculator_dict = {}   # the new dictionary i want to put both the list into

symbols = ["+", "-", "*", "/"]

symbol_name = ["add", "subtract", "multiply", "divide"]

for symbol in symbols:

  for name in symbol_name:

    calculator_dict[symbol] = name

print(calculator_dict)

What i want to be printed is:我想要打印的是:

calculator_dict = {
    "+": "add",
    "-": "subtract",
    "*": "multiply",
    "/": "divide",
}

Use the zip and dict functions:使用zipdict函数:

>>> symbols = ["+", "-", "*", "/"]
>>> symbol_name = ["add", "subtract", "multiply", "divide"]
>>> dict(zip(symbols, symbol_name))
{'+': 'add', '-': 'subtract', '*': 'multiply', '/': 'divide'}

You can also use a dict comprehension over the zipped lists:您还可以对压缩列表使用 dict 理解:

>>> symbols = ["+", "-", "*", "/"]
>>> symbol_name = ["add", "subtract", "multiply", "divide"]
>>> calculator_dict = { s:n for s,n in zip(symbols, symbol_name) }
{'+': 'add', '-': 'subtract', '*': 'multiply', '/': 'divide'}

This is useful if you need more fine-grained control.如果您需要更细粒度的控制,这很有用。

For example, uppercase the values:例如,将值大写:

>>> { s:n.capitalize() for s,n in zip(symbols, symbol_name) }
{'+': 'ADD', '-': 'SUBTRACT', '*': 'MULTIPLY', '/': 'DIVIDE'}

Or filter values by length:或按长度过滤值:

>>> { s:n for s,n in zip(symbols, symbol_name) if len(n) < 5 }
{'+': 'add'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用python循环遍历两个列表,一个用作列表列表的索引,另一个用作要附加的值 - Use python to loop over two lists, using one as an index to a list of lists, and the other as a value to append Python:如果遇到条件,则循环使用一个字典并在新字典中创建键/值对 - Python: Looping over One Dictionary and Creating Key/Value Pairs in a New Dictionary if Conditions Are Met 将两个列表合并成一个字典Python - Two Lists Into One Dictionary Python 将字典分为两个列表,一个列表用于键,另一个列表用于值 - split dictionary into two lists , one list is for key, another list is for value 如何在Python中切换嵌套字典的值和键? - How can one switch the value and key of a Nested dictionary in Python? 如何组合 3 个列表来创建一个具有一个键和两个值的字典? - How to combine 3 lists to create a dictionary with one key and two values? Python 的新功能:使用两个列表创建字典,其中一个列表包含多于一行 - New to Python: Creating a dictionary using two lists, with one of the lists having more than one line Python TypeError:将两个列表的值映射到新的一个容器列表中的键值对 - Python TypeError: Mapping the values of two lists into key-value pairs in a new one container list 迭代列表作为字典中的值以搜索特定值,然后将该值的键添加到新列表中,在 Python 中 - Iterate over lists as values in dictionary to search for specific values, then add the key of that value to new lists, in Python 如何将多个列表附加到python字典中的一个键? - How do I append multiple lists to one key in a python dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM