【问题标题】:Need to get an Iterable, I've got a List of Dicts需要一个 Iterable,我有一个字典列表
【发布时间】:2018-04-03 02:50:52
【问题描述】:

我的头撞墙的时间比我愿意承认的要长。我有一个字典列表,我需要将它们作为可迭代传递给某些东西。我假设我需要获取这些字典的内容并将它们放入格式正确的新字典中。我不知道该怎么做。这是我的“联系人”列表的样子:

[{'City': 'Boston',
'Unique_Id': '123456-789',
'FullName': 'Smith, Joe',
'other_id': '0987654',
'area': 'Area 1'},
{'City': 'San Fransisco',
'Unique_Id': '654321-987',
'FullName': 'Brown, John',
'other_id': '7654321',
'area': 'Area 2'},
{'City': 'New York',
'Unique_Id': '7890123-456',
'FullName': 'Incomplete, Guy',
'other_id': None,
'area': None}]

我正在尝试将其传递给此:https://github.com/plumdog/flask_table

相关部分在哪里:

class Item(object):
def __init__(self, name, description):
    self.name = name
    self.description = description
...
...
items = [dict(name='Name1', description='Description1'),
     dict(name='Name2', description='Description2'),
     dict(name='Name3', description='Description3')]

所以我需要让我的列表“联系人”以某种形式被该函数迭代,大概是一个新的字典。我觉得我需要一个列表理解才能将其转换为字典,但我无法很好地掌握这个概念来理解如何做到这一点。我需要将其传递给函数,以便它可以将 json 列表/dict 值转换为 HTML 表以在一个小 SPA 中输出。列表中有 5 个字典的硬性限制,所以它不应该变得很长。我想我想最终得到类似的东西:

items = [contacts(name='City', description='Their City'),
 contacts(name='Unique_Id', description='GUID'),
 contacts(name='FullName', description='Name')
...and so on
]

我不知道怎么做。任何帮助将不胜感激。

【问题讨论】:

  • 你为什么假设你想要的结果是什么?这让我们猜测你想要什么。如果您创建一个Minimal, Complete, and Verifiable 示例,您将获得更多更好的答案。尤其要确保输入和预期的测试数据是完整的(不是伪数据),并且可以很容易地剪切和粘贴到编辑器中,以便测试建议的解决方案。
  • 我不是。该函数需要一个可迭代的。我有一个需要能够迭代的字典列表,以某种方式与 flask_table 函数期望的输入兼容。我已经从函数中复制了模板以及我认为它应该是什么样子的示例。文档不多,我也没有工作示例,所以我不确定。
  • 如果他们知道结果需要是什么样子,那么这里的许多人都可以轻松地帮助您构建一个可迭代对象。举个难以回答的例子,contacts 是什么?
  • 列表 已经是可迭代的,那么为什么不能将它传递给函数呢?
  • 回复:Paul - 我收到此错误,但我认为这与未定义 cols 有关。 TypeError: 'list' 对象不可调用

标签: python json list dictionary flask


【解决方案1】:

您不需要更改字典中的任何内容。您只需要更新两个类中的属性:

# Declare your table
class ItemTable(Table):
    City = Col('City')
    Unique_Id = Col('Unique_Id')
    FullName = Col('FullName')
    other_id = Col("other_id")
    area = Col("area")

# Get some objects
class Item(object):
    def __init__(self, City, Unique_Id,FullName,other_id,area):
        self.City = City
        self.Unique_Id = Unique_Id
        self.FullName = FullName
        self.other_id=other_id
        self.area = area

然后根据您的参考网址:

     myTable =   [{'City': 'Boston',
    'Unique_Id': '123456-789',
    'FullName': 'Smith, Joe',
    'other_id': '0987654',
    'area': 'Area 1'},
    {'City': 'San Fransisco',
    'Unique_Id': '654321-987',
    'FullName': 'Brown, John',
    'other_id': '7654321',
    'area': 'Area 2'},
    {'City': 'New York',
    'Unique_Id': '7890123-456',
    'FullName': 'Incomplete, Guy',
    'other_id': None,
    'area': None}]
    table = ItemTable(myTable)
    print(table.__html__())

输出如下:

<table>
<thead><tr><th>City</th><th>Unique_Id</th><th>FullName</th><th>other_id</th><th>area</th></tr></thead>
<tbody>
<tr><td>Boston</td><td>123456-789</td><td>Smith, Joe</td><td>0987654</td><td>Area 1</td></tr>
<tr><td>San Fransisco</td><td>654321-987</td><td>Brown, John</td><td>7654321</td><td>Area 2</td></tr>
<tr><td>New York</td><td>7890123-456</td><td>Incomplete, Guy</td><td></td><td></td></tr>
</tbody>
</table>

【讨论】:

  • 这更有意义,这是我没有看到的部分 - 除了对象之外定义 cols 是我在文档中缺少的。现在就试试这个,谢谢!
  • 很高兴,它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多