Python 3.x 使字典列表中的键小写(Python3)

Python 3.x 使字典列表中的键小写(Python3),python-3.x,list,list-comprehension,Python 3.x,List,List Comprehension,我已经看了两天Stackoverflow和文档了,我是一个初学者,我就是不能进步。我正在使用Python 3.8 我有一份字典清单: books = [{'Type': 'Book', 'Date': '2011', 'Publication Year': '2011', 'Place Published': 'New York', 'Publisher': 'Simon & Schuster', 'Author': 'Walter Isaacson', 'ISBN': '978-1-4

我已经看了两天Stackoverflow和文档了,我是一个初学者,我就是不能进步。我正在使用Python 3.8

我有一份字典清单:

books = [{'Type': 'Book', 'Date': '2011', 'Publication Year': '2011', 'Place Published': 'New York', 'Publisher': 'Simon & Schuster', 'Author': 'Walter Isaacson', 'ISBN': '978-1-4516-4853-9', 'Title': 'Test Steve Jobs'}, {'Type': 'Book', 'Date': '2001', 'Publication Year': '2001', 'Place Published': 'Oxford', 'Publisher': 'Oxford University press', 'Author': 'Peter Hall', 'ISBN': '978-0-19-924775-2', 'Title': 'Test Varieties of capitalism: the institutional foundations of comparative advantage'}]
print(books)
我想把键“Type”变成小写的“Type”。 但在下面的列表中,它以某种方式成为值的键,反之亦然

lower_list = [ { v:k.lower() for k,v in d.items() } for d in books ]
print(lower_list)
当它应该是
[{'type':'Book',…
时,我以
[{'Book':'type',…
结束

我仍在努力理解列表理解语法,因此我非常感谢1.有人用通俗易懂的英语解释我的列表理解功能,2.如何更改它以实现我的目标。:)


谢谢!

那么您的第一个问题是:

lower_list = [ { k.lower():v for k,v in d.items() } for d in books ] ? 
您正在反转键和值

您的最后一个问题是如何跳过ISBN键的小写:

[ { k if k is "ISBN" else k.lower():v.lower() for k,v in d.items()} for d in books ]

但是你应该考虑使用for循环:如果你需要更多的操作或条件,它将很难进一步修改。

my_final_books = []

for d in books:
    for k,v in d.items():
        if k is "ISBN":
            key = k
        else:
            key = k.lower()
        # or ternary form key = k if k is "ISBN" else k.lower()
        my_final_books.append({key:v})
        # do more logic here

lower\u list=[{k.lower():v代表k,v代表d.items()}代表书中的d]
?您正在反转键和值我知道有人会在几秒钟内用最简单的方法回答!非常感谢!您能向我解释一下:这里有吗?这是dict中键和值之间的分隔符,你可以看到这样的理解:
[{myKey:myValue for myKey,myValue in d.items()}for d in books]
in myKey,myValue in d.items()你正在解包一个由dict生成的元组,比如:(key,value),不知道现在是否更清楚了:p这是有道理的。谢谢你花时间解释。:)一个后续问题:我现在正在尝试对每个键进行小写,但ISBN除外。但显然它的语法无效: ` ` <代码>[{v:k.lower()表示k,如果k在d.items()中不是“ISBN”,则为v}表示书籍中的d]  ` `哇。你太棒了!非常感谢。同时,我能够制作这个。花了我一整天的时间。但是你的版本看起来更优雅。`
def lower\u But\u ISBN(lower\u dict):newdict={}k,v在lower\u dict中。items():if not k='ISBN':if isinstance(v,dict):v=lower\u dict(v)newdict[k.lower()]=v else:if isinstance(v,dict):v=lower_dict(v)newdict[k]=v返回图书中项目的newdict:print(lower_但_ISBN(项目))
`