Python 如何更改口述';列表中的s值

Python 如何更改口述';列表中的s值,python,python-2.7,dictionary,syntax,list-comprehension,Python,Python 2.7,Dictionary,Syntax,List Comprehension,我有一个字典列表,我目前对列表的理解是将字典分开(即,在以前没有的地方创建新的字典)。下面是一些示例代码来帮助说明这个问题 list_of_dicts = [{"this": "hi<br>", "that":"bye"}, {"this": "bill", "that":"nye"},{"hello":"kitty<br>", "good bye": "to all of that"}] 如果您打印这一行的len(),您会注意到我现在有六本词典。我相信我试图做的事情是

我有一个字典列表,我目前对列表的理解是将字典分开(即,在以前没有的地方创建新的字典)。下面是一些示例代码来帮助说明这个问题

list_of_dicts = [{"this": "hi<br>", "that":"bye"}, {"this": "bill", "that":"nye"},{"hello":"kitty<br>", "good bye": "to all of that"}]
如果您打印这一行的
len()
,您会注意到我现在有六本词典。我相信我试图做的事情是可能的,即用一个空格(
)替换值中的

”),但我不知道如何做

到目前为止,我已经尝试了所有我知道的方法来创建字典,而不是
{key:val.method()}
。我唯一没有尝试过的是三元列表理解,因为我可以看到它太长了,我永远不希望它出现在生产代码中


有什么见解吗?我可以在列表理解中操纵某些dict的值而不影响dict的初始结构吗?

词典理解执行了六次,因为您当前的代码与此等效:

list_of_dicts = [{"this": "hi<br>", "that":"bye"}, {"this": "bill", "that":"nye"},{"hello":"kitty<br>", "good bye": "to all of that"}]
tmp = []

for dic in list_of_dicts:
    for key, val in dic.items():
        tmp.append({key: val.replace("<br>", " ")})

list_of_dicts = tmp
将被执行六次


只需将dic.items()中键val的
子句移动到字典中,即可解决此问题:

list_of_dicts = [{key: val.replace("<br>", " ")} for dic in list_of_dicts for key, val in dic.items()]
>>> list_of_dicts = [{"this": "hi<br>", "that":"bye"}, {"this": "bill", "that":"nye"},{"hello":"kitty<br>", "good bye": "to all of that"}]
>>> [{key: val.replace("<br>", " ") for key, val in dic.items()} for dic in list_of_dicts]
[{'this': 'hi ', 'that': 'bye'}, {'this': 'bill', 'that': 'nye'}, {'hello': 'kitty ', 'good bye': 'to all of that'}]
>>>
>>>目录=[{“this”:“hi
,“that”:“bye”},{“this”:“bill”,“that”:“nye”},{“hello”:“kitty
,“再见”:“所有这些”}] >>>[{key:val.replace(“
”,“”)代表key,val代表dic.items()}代表dic的列表 [{'this':'hi','that':'bye'},{'this':'bill','that':'nye'},{'hello':'kitty','goodbye':'诸如此类'}] >>>

现在,字典理解将只执行三次:在dicts的
列表中的每个项目执行一次,字典理解将执行六次,因为您当前的代码相当于:

list_of_dicts = [{"this": "hi<br>", "that":"bye"}, {"this": "bill", "that":"nye"},{"hello":"kitty<br>", "good bye": "to all of that"}]
tmp = []

for dic in list_of_dicts:
    for key, val in dic.items():
        tmp.append({key: val.replace("<br>", " ")})

list_of_dicts = tmp
将被执行六次


只需将dic.items()中键val的
子句移动到字典中,即可解决此问题:

list_of_dicts = [{key: val.replace("<br>", " ")} for dic in list_of_dicts for key, val in dic.items()]
>>> list_of_dicts = [{"this": "hi<br>", "that":"bye"}, {"this": "bill", "that":"nye"},{"hello":"kitty<br>", "good bye": "to all of that"}]
>>> [{key: val.replace("<br>", " ") for key, val in dic.items()} for dic in list_of_dicts]
[{'this': 'hi ', 'that': 'bye'}, {'this': 'bill', 'that': 'nye'}, {'hello': 'kitty ', 'good bye': 'to all of that'}]
>>>
>>>目录=[{“this”:“hi
,“that”:“bye”},{“this”:“bill”,“that”:“nye”},{“hello”:“kitty
,“再见”:“所有这些”}] >>>[{key:val.replace(“
”,“”)代表key,val代表dic.items()}代表dic的列表 [{'this':'hi','that':'bye'},{'this':'bill','that':'nye'},{'hello':'kitty','goodbye':'诸如此类'}] >>>

现在,字典理解将只执行三次:在字典的
列表中,每项执行一次。在这种情况下,列表理解可能会混淆。我建议对
循环使用经典的

来源
在这种情况下,列表理解可能会令人困惑。我建议对
循环使用经典的

来源
您正在寻找嵌套的理解

list_of_dicts = [dict((key, val.replace("<br>", " "))
                      for key, val in d.iteritems())
                 for d in list_of_dicts]
目录列表=[dict((key,val.replace(
“,”)) 对于键,d.iteritems()中的val) 对于目录中的d
但是你让事情变得比需要的更复杂。。。更简单的是:

for d in list_of dicts:
    for k, v in d.items():
        d[k] = v.replace("<br>", " ")
目录列表中的d的
:
对于d.项()中的k,v:
d[k]=v.replace(“
”,“”)

相反?

您正在寻找嵌套理解

list_of_dicts = [dict((key, val.replace("<br>", " "))
                      for key, val in d.iteritems())
                 for d in list_of_dicts]
目录列表=[dict((key,val.replace(
“,”)) 对于键,d.iteritems()中的val) 对于目录中的d
但是你让事情变得比需要的更复杂。。。更简单的是:

for d in list_of dicts:
    for k, v in d.items():
        d[k] = v.replace("<br>", " ")
目录列表中的d的
:
对于d.项()中的k,v:
d[k]=v.replace(“
”,“”)

相反?

为什么它必须是一个列表;为什么不按常规重复列表呢?这只是一种偏好。我可以比嵌套代码更容易地阅读列表理解。@jonrsharpe感谢您的编辑。我的问题总是令人非常困惑。你可能为我赢得了一张非常罕见的选票,所以谢谢:)为什么一定是一张名单;为什么不按常规重复列表呢?这只是一种偏好。我可以比嵌套代码更容易地阅读列表理解。@jonrsharpe感谢您的编辑。我的问题总是令人非常困惑。你可能为我赢得了一张非常难得的选票,所以谢谢:)