简体   繁体   English

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

[英]python one liner code for two nested for loop

How to get the desired result in python one liner ?? 如何在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",}]

Result required is to replace all None to "" 需要的结果是将所有None替换为“”

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",}]

Simple Function to make this happens is : 实现这一目标的简单功能是:

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

And Python one unsuccessful one liner: 和Python一个不成功的一个班轮:

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

Can the one liner be achieved with list comprehensions? 可以通过列表推导来实现一个班轮吗?

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]

explained: 解释:

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

Using dictionary comprehension (available since Python 2.7), first just reconstructing the dictionary into the same value: 使用字典理解(从Python 2.7开始提供),首先只需将字典重建为相同的值:

{key: val  for dct.items()}

and extending it by assigning "" in case, we have as original value None (or any other value evaluating to False) 并通过分配“”扩展它,以防万一,我们有原始值无(或任何其他值评估为假)

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

Finally (as shown above) it is applied in enveloping list comprehension to all items in the list. 最后(如上所示)它被应用于包含列表理解到列表中的所有项目。

{key: val  for dct.items()}

Strictly speaking, this replaces anything, what looks as boolean False by "". 严格地说,这取代了任何东西,看起来像布尔假的“”。

If we want only None values replaced by "" , and eg False and 0 keep as it is, we shall e more strict: 如果我们只想要将None替换为"" ,例如False0保持原样,我们将更加严格:

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

This seems easy enough: 这似乎很容易:

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

and to show what the output looks like: 并显示输出的样子:

>>> 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'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM