python一个用于两个嵌套for循环的线性代码

时间:2014-05-14 19:41:52

标签: python python-2.7

如何在python one liner中获得所需的结果?

object_list=[{'applicationName': "ATM Monitoring",
                         'roamingDrop': "",
                         'noOfCustomer': None,
                         'ipAddress': "192.168.1.1",
                          'url': "www.google.co.in",},
             {'applicationName': None,
                         'roamingDrop': "",
                         'noOfCustomer': None,
                         'ipAddress': "192.168.1.1",
                          'url': "www.google.co.in",}]

所需的结果是将所有无替换为""

object_list=[{'applicationName': "ATM Monitoring",
                         'roamingDrop': "",
                         'noOfCustomer': "",
                         'ipAddress': "192.168.1.1",
                          'url': "www.google.co.in",},
             {'applicationName': "",
                         'roamingDrop': "",
                         'noOfCustomer': "",
                         'ipAddress': "192.168.1.1",
                          'url': "www.google.co.in",}]

实现这一目标的简单功能是:

def simple():
    for object in object_list:
        for key, value in object.iteritems():
            if value:
                dict( object, **{key: value})
            else:
                dict(object, **{key: ''})

Python是一个不成功的一个班轮:

[dict(object, **{key: value}) if value else dict(object, **{key: ''}) 
    for object in object_list  for key, value in object.iteritems()]

可以通过列表推导来实现一个班轮吗?

2 个答案:

答案 0 :(得分:4)

lst=[{'applicationName': "ATM Monitoring",
                     'roamingDrop': "",
                     'noOfCustomer': None,
                     'ipAddress': "192.168.1.1",
                      'url': "www.google.co.in",},
         {'applicationName': None,
                     'roamingDrop': "",
                     'noOfCustomer': None,
                     'ipAddress': "192.168.1.1",
                      'url': "www.google.co.in",}]

print [{key: val if val else "" for key, val in dct.items()} for dct in lst]

说明:

dct = lst[0]
{'applicationName': "ATM Monitoring",
                     'roamingDrop': "",
                     'noOfCustomer': None,
                     'ipAddress': "192.168.1.1",
                      'url': "www.google.co.in",}

使用字典理解(自Python 2.7起可用),首先将字典重建为相同的值:

{key: val  for dct.items()}

并通过分配“”扩展它以防万一,我们有原始值None(或任何其他值评估为False)

{key: val if val else ""  for dct.items()}

最后(如上所示)它被应用于包络列表理解到列表中的所有项目。

{key: val  for dct.items()}

严格地说,这取代了任何东西,看起来像布尔假的“”。

如果我们只希望将None值替换为"",例如False0保持原样,我们会更加严格:

print [{key: val if val is not None else "" for key, val in dct.items()} for dct in lst]

答案 1 :(得分:-1)

这似乎很容易:

>>> for d in object_list:
...     for k, v in d.items():
...         if v is None:
...             d[k] = ''
... 

并显示输出结果:

>>> import pprint
>>> pprint.pprint(object_list)
[{'applicationName': 'ATM Monitoring',
  'ipAddress': '192.168.1.1',
  'noOfCustomer': '',
  'roamingDrop': '',
  'url': 'www.google.co.in'},
 {'applicationName': '',
  'ipAddress': '192.168.1.1',
  'noOfCustomer': '',
  'roamingDrop': '',
  'url': 'www.google.co.in'}]