首页 \ 问答 \ 将两个数组组合成结果数组中的键和值?(Combine two arrays into keys and values in a resulting array? [duplicate])

将两个数组组合成结果数组中的键和值?(Combine two arrays into keys and values in a resulting array? [duplicate])

这个问题在这里已有答案:

我有两个数组,一个是键,另一个是值:

key_array = ['key1','key2','key3']
values_array = ['val1','val2','val3']

我如何迭代它们(它们的大小会有所不同)来获得这个:

array('key1' => 'val1',
      'key2' => 'val2',
      'key3' => 'val3')

This question already has an answer here:

I have two arrays, one with the keys and the other with the values:

key_array = ['key1','key2','key3']
values_array = ['val1','val2','val3']

How do I iterate through them (their size will vary) to obtain this:

array('key1' => 'val1',
      'key2' => 'val2',
      'key3' => 'val3')

原文:https://stackoverflow.com/questions/38403352
更新时间:2023-11-23 06:11

最满意答案

你错过了冒号,应该是这样的:

for urls in new_keywords:
    if urls not in old_keywords:
        upload_keywords.append(urls)

这就是为什么你得到无效的语法错误


You're missing a colon, it should be like:

for urls in new_keywords:
    if urls not in old_keywords:
        upload_keywords.append(urls)

That's why you get the invalid syntax error

相关问答

更多
  • 如何在PyCharm中“查看”/突出显示选项卡和空格以检查缩进? 您可以通过查看 - >活动编辑器 - >显示空白空间来指示Pycharm以可视方式显示制表符和空格 : 我也理解你可以在PyCharm中混合制表符和4个空格字符缩进而没有问题? 是的你可以。 但你不应该。 但是,即使你选择PyCharm,默认情况下它们都会将它们全部转换为空格。 您可以通过转到设置 - >编辑 - >代码样式 - > Python ,然后选择使用选项卡字符复选框来更改此行为: How to 'see' / highlight ...
  • AnyEdit工具听起来像你可能需要的。 AnyEdit tools sounds like what you might need.
  • 您可能正在使用不支持装饰器的旧Python版本。 You might be using an old Python version that didn't support decorators yet.
  • 你错过了冒号,应该是这样的: for urls in new_keywords: if urls not in old_keywords: upload_keywords.append(urls) 这就是为什么你得到无效的语法错误 You're missing a colon, it should be like: for urls in new_keywords: if urls not in old_keywords: upload_keywords.a ...
  • 第39行中有一个缺失的右括号: return redirect(reverse('home:perfil') # missing ) 我肯定会推荐在你的项目中使用诸如flake8之类的linter ,并为你使用的任何代码编辑器或IDE找到一个插件。 从长远来看,它会发现类似的错误并为你节省很多的痛苦。 You've got a missing closing parenthesis in line 39: return redirect(reverse('home:perfil') # missing ...
  • 字典解析仅在Python 2.7及更高版本中可用。 您需要使用另一种方法,最好使用dict() : dict((i, factorize_naive(i)) for i in nums) Dictionary comprehensions are only available in Python 2.7 and above. You'll need to use another method, preferably using dict(): dict((i, factorize_naive(i)) fo ...
  • 你似乎没有将jquery库加载到文档中: 将此添加到头部: 以下只是您现有的代码与该更改(以及将单击功能包装在文档就绪的包装器中)并且它不会出现错误并在按钮单击时控制消息。 Login Page