python 使用列表理解在python中连接字典中的项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1911014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:19:47  来源:igfitidea点击:

concatenate items in dictionary in python using list comprehension

pythonlist-comprehension

提问by randomThought

EDIT: Clarified the question a bit

编辑:澄清了一点问题

How can I get a string from a dictionary with the format

如何从具有以下格式的字典中获取字符串

key1 = value1
key2 = value2

in a relatively fast way ? (relative to plain concatenation)

以相对较快的方式?(相对于普通串联)

回答by Tor Valamo

There's no reason to use list comprehension here.

没有理由在这里使用列表理解。

Python 3.x:

Python 3.x:

for k,v in mydict.items():
  print(k, '=', v)

Python 2.x:

Python 2.x:

for k,v in mydict.iteritems():
  print k, '=', v

EDIT because of comment by OP in another answer:

由于 OP 在另一个答案中的评论而进行编辑:

If you're passing it to a function and not printing it here, then you should just pass the generator to the function, or the dict itself and let the function handle whatever it needs to do with it.

如果你将它传递给一个函数而不是在这里打印它,那么你应该只将生成器传递给函数,或者字典本身,让函数处理它需要做的任何事情。

This is much better than converting it to a string inside a scope where it's not even needed. The function should do that, since that's where it's used.

这比在不需要它的范围内将其转换为字符串要好得多。该函数应该这样做,因为那是使用它的地方。

I made a wrapper function, since editing the main function is out of the question.

我做了一个包装函数,因为编辑 main 函数是不可能的。

def log_wrap(mydict):
    mystr = '\n'.join(['%s = %s' % (k,v) for k,v in mydict.iteritems()])
    write_to_log(mydict)

log_wrap(mydict)

回答by Nadia Alramli

print '\n'.join('%s = %s' % (key, value) for key, value in d.iteritems())

回答by rob

Explicit is better than implicit

显式优于隐式

List comprehension is a way to create list, not to avoid loops.
From PEP 202:

List comprehension 是一种创建 list 的方法,而不是避免循环。
来自 PEP 202:

List comprehensions provide a more concise way to create lists in situations where map() and filter() and/or nested loops would currently be used.

在当前使用 map() 和 filter() 和/或嵌套循环的情况下,列表推导式提供了一种更简洁的方法来创建列表。

So you should ask yourself:
When is it useful to create this code in Python? It may be more compact but code is read many more times than it is written so what is the advantage in it?

所以你应该问问自己:
什么时候用 Python 创建这段代码有用?它可能更紧凑,但代码的读取次数比写入的次数要多得多,那么它的优势是什么?

Tor Valamo's solution, although not what was asked for in the original request, is in my opinion far more readable, and therefore should be preferred.

Tor Valamo 的解决方案,虽然不是原始请求中所要求的,但在我看来更具可读性,因此应该是首选

EDIT after question update
str.joinis a good way to implement a fast concatenation from a list - and replies from Nadia and Ritchie are good examples of how to use it.
Again, I would notperform everything in a single line, but I would split it in various steps to emphasize readability.

问题更新后编辑
str.join是从列表中实现快速连接的好方法 - Nadia 和 Ritchie 的回复是如何使用它的好例子。
同样,我不会在一行中执行所有内容,但我会将其拆分为多个步骤以强调可读性。

回答by RichieHindle

Like this:

像这样:

DATA = {'key1': 'value1', 'key2': 'value2'}
print "\n".join(sorted(["%s = %s" % (k,v) for (k,v) in DATA.iteritems()]))

回答by enrnpfnfp

I prefer the pythonic way:

我更喜欢pythonic方式:

mydict = {'a':1, 'b':2, 'c':3}
for (key, value) in mydict.items():
    print key, '=', value