列表理解从字典中提取元组列表

发布于 2024-10-24 08:01:44 字数 676 浏览 1 评论 0原文

我想在以下列表中使用列表理解;

movie_dicts = [{'title':'A Boy and His Dog', 'year':1975, 'rating':6.6},
           {'title':'Ran', 'year':1985, 'rating': 8.3},
           {'title':'True Grit', 'year':2010, 'rating':8.0},
           {'title':'Scanners', 'year':1981, 'rating': 6.7}]

利用我对列表理解和字典的知识,我知道这

movie_titles = [x['title'] for x in movie_dicts]
print movie_titles

将打印一个包含电影标题的列表。

为了提取我尝试过的(标题,年份)元组列表 -

movie_tuples = [x for ('title','year') in movie_dicts]
print movie_tuples

我收到错误 SyntaxError: can't allocate toliteral

我不确定如何使用列表获取两个(特定)键/值对理解(这样做会自动生成一个元组?)

I'd like to use list comprehension on the following list;

movie_dicts = [{'title':'A Boy and His Dog', 'year':1975, 'rating':6.6},
           {'title':'Ran', 'year':1985, 'rating': 8.3},
           {'title':'True Grit', 'year':2010, 'rating':8.0},
           {'title':'Scanners', 'year':1981, 'rating': 6.7}]

using my knowledge of list comprehension and dictionaries, I know that

movie_titles = [x['title'] for x in movie_dicts]
print movie_titles

will print a list with movie titles.

In order to extracts a list of (title, year) tuples I've tried -

movie_tuples = [x for ('title','year') in movie_dicts]
print movie_tuples

and I receive the error SyntaxError: can't assign to literal

I'm unsure on how to fetch the two (specific) key/value pairs using list comprehension (doing so would generate a tuple automatically?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉城凉梦凉人心 2024-10-31 08:01:44
movie_dicts = [
    {'title':'A Boy and His Dog', 'year':1975, 'rating':6.6},
    {'title':'Ran', 'year':1985, 'rating': 8.3},
    {'title':'True Grit', 'year':2010, 'rating':8.0},
    {'title':'Scanners', 'year':1981, 'rating': 6.7}
]

title_year = [(i['title'],i['year']) for i in movie_dicts]

给出

[('A Boy and His Dog', 1975),
 ('Ran', 1985),
 ('True Grit', 2010),
 ('Scanners', 1981)]

OR

import operator
fields = operator.itemgetter('title','year')
title_year = [fields(i) for i in movie_dicts]

给出完全相同的结果。

movie_dicts = [
    {'title':'A Boy and His Dog', 'year':1975, 'rating':6.6},
    {'title':'Ran', 'year':1985, 'rating': 8.3},
    {'title':'True Grit', 'year':2010, 'rating':8.0},
    {'title':'Scanners', 'year':1981, 'rating': 6.7}
]

title_year = [(i['title'],i['year']) for i in movie_dicts]

gives

[('A Boy and His Dog', 1975),
 ('Ran', 1985),
 ('True Grit', 2010),
 ('Scanners', 1981)]

OR

import operator
fields = operator.itemgetter('title','year')
title_year = [fields(i) for i in movie_dicts]

which gives exactly the same result.

我ぃ本無心為│何有愛 2024-10-31 08:01:44

这个版本至少有重复:

>>> fields = "title year".split()
>>> movie_tuples = [tuple(map(d.get,fields)) for d in movie_dicts]

This version has a minimum of repeating yourself:

>>> fields = "title year".split()
>>> movie_tuples = [tuple(map(d.get,fields)) for d in movie_dicts]
傾旎 2024-10-31 08:01:44
[(movie_dict['title'], movie_dict['year']) for movie_dict in movie_dicts]

请记住,xs = [expr for target in expr2] 等效于(为了简单起见,几乎忽略了 StopIteration):

xs = []
for target in expr2:
    xs.append(expr)

因此 target 需要是普通的旧变量名称或一些要解压到的元组。但由于 movie_dicts 不包含要解压的序列,而是简单的单个值(字典),因此您必须将其限制为一个变量。然后,当您追加到正在生成的列表时,您可以创建一个元组并对当前项目执行您想要执行的任何其他操作。

[(movie_dict['title'], movie_dict['year']) for movie_dict in movie_dicts]

Remember, xs = [expr for target in expr2] is equivalent (almost - ignoring StopIteration for simplicity) to:

xs = []
for target in expr2:
    xs.append(expr)

So target needs to be a plain old variable name or some tuple to unpack to. But since movie_dicts doesn't contain sequences to unpack from but simple single values (dicts), you must limit it to one variable. Then when you append to the list being generated, you can create a tuple and do whatever else you want to do with the current item.

匿名的好友 2024-10-31 08:01:44

如果您不必使用列表理解,您始终可以这样做:

list(d.iteritems())

If you don't have to use a list comprehension, you could always do:

list(d.iteritems())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文