通过列表值的字典迭代

发布于 2025-02-12 09:49:26 字数 841 浏览 2 评论 0原文

我希望通过词典进行迭代,并为下面显示的值列表中的每个值创建一个新的词典。每个值将是一个值或列表,每个列表的长度相同。第一个词典是原始词典,而其他字典是我要输出的前两个词典。我最终想拥有这些值,对它们做点事,然后移至下一个字典输出等等。

dict = {'cost': [1, 3, 4, 8, 10],
        'address': '123 Fake St',
        'phone number': '123-456-7890',
        'item': ['apple', 'banana', 'strawberry', 'paper', 'pencil'],
        'name': 'John David'}

dict1 = {'cost': 1,
         'address': '123 Fake St',
         'phone number': '123-456-7890',
         'item': 'apple',
         'name': 'John David'}

dict2 = {'cost': 3,
         'address': '123 Fake St',
         'phone number': '123-456-7890',
         'item': 'banana',
         'name': 'John David'}

我的尝试:

from collections import defaultdict 
custs = defaultdict(list)

for key, value in sorted(dict.iteritems()):
    custs[value].append(key)

I am looking to iterate through a dictionary and create a new dictionary for each of the values within a list of the value shown below. Each value will be either a single value or a list and each list would have the same length. The first dictionary is the original one, and the others are the first two dictionaries I want to output. I ultimately want to just have those values, do something with them and then move onto the next dictionary output and so on.

dict = {'cost': [1, 3, 4, 8, 10],
        'address': '123 Fake St',
        'phone number': '123-456-7890',
        'item': ['apple', 'banana', 'strawberry', 'paper', 'pencil'],
        'name': 'John David'}

dict1 = {'cost': 1,
         'address': '123 Fake St',
         'phone number': '123-456-7890',
         'item': 'apple',
         'name': 'John David'}

dict2 = {'cost': 3,
         'address': '123 Fake St',
         'phone number': '123-456-7890',
         'item': 'banana',
         'name': 'John David'}

My attempt:

from collections import defaultdict 
custs = defaultdict(list)

for key, value in sorted(dict.iteritems()):
    custs[value].append(key)

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

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

发布评论

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

评论(1

北城孤痞 2025-02-19 09:49:26

我相信以下理解应该可以解决问题:

d = {'cost': [1, 3, 4, 8, 10],
             'address': '123 Fake St',
             'phone number': '123-456-7890',
             'item': ['apple', 'banana', 'strawberry', 'paper', 'pencil'],
             'name': 'John David'}

res = [{**d, "cost": cost, "item": item} for cost, item in zip(d["cost"], d["item"])]

哪个产生:

{'cost': 1, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'apple', 'name': 'John David'}
{'cost': 3, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'banana', 'name': 'John David'}
{'cost': 4, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'strawberry', 'name': 'John David'}
{'cost': 8, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'paper', 'name': 'John David'}
{'cost': 10, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'pencil', 'name': 'John David'}

没有comp版本:

for cost, item in zip(d["cost"], d["item"]):
    modified_d = {**d, "cost": cost, "item": item}
    do_stuff_with_modified_d(modified_d)

I believe the following comprehension should do the trick:

d = {'cost': [1, 3, 4, 8, 10],
             'address': '123 Fake St',
             'phone number': '123-456-7890',
             'item': ['apple', 'banana', 'strawberry', 'paper', 'pencil'],
             'name': 'John David'}

res = [{**d, "cost": cost, "item": item} for cost, item in zip(d["cost"], d["item"])]

Which yields:

{'cost': 1, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'apple', 'name': 'John David'}
{'cost': 3, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'banana', 'name': 'John David'}
{'cost': 4, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'strawberry', 'name': 'John David'}
{'cost': 8, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'paper', 'name': 'John David'}
{'cost': 10, 'address': '123 Fake St', 'phone number': '123-456-7890', 'item': 'pencil', 'name': 'John David'}

No comp version:

for cost, item in zip(d["cost"], d["item"]):
    modified_d = {**d, "cost": cost, "item": item}
    do_stuff_with_modified_d(modified_d)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文