简体   繁体   English

检查python列表中的所有项目是否都包含子字符串

[英]Check if all items in python list contain substring

I have a python list, that I am getting from api: 我有一个从api获取的python列表:

ERRATA_PKG_LIST = []
packages_key = "packages"
for pkgs, package_name in errata_rhel_ver_dic.iteritems():
    if pkgs == packages_key:
       ERRATA_PKG_LIST.append(package_name)
print(ERRATA_PKG_LIST)

Output: 输出:

[u'kernel-2.6.18-416.el5.i686',
 u'kernel-2.6.18-416.el5.x86_64',
 u'kernel-debug-2.6.18-416.el5.i686',
 u'kernel-debug-2.6.18-416.el5.x86_64',
 u'kernel-debug-devel-2.6.18-416.el5.i686',
 u'kernel-debug-devel-2.6.18-416.el5.x86_64',
 u'kernel-devel-2.6.18-416.el5.i686',
 u'kernel-devel-2.6.18-416.el5.x86_64',
 u'kernel-doc-2.6.18-416.el5.noarch',
 u'kernel-headers-2.6.18-416.el5.i386',
 u'kernel-headers-2.6.18-416.el5.x86_64',
 u'kernel-PAE-2.6.18-416.el5.i686',
 u'kernel-PAE-devel-2.6.18-416.el5.i686',
 u'kernel-xen-2.6.18-416.el5.i686',
 u'kernel-xen-2.6.18-416.el5.x86_64',
 u'kernel-xen-devel-2.6.18-416.el5.i686',
 u'kernel-xen-devel-2.6.18-416.el5.x86_64']

I want to check that each and every item in the list contains the substring: el5 if all of the objects in the list contains el5 ,then assign a variable rhel = "rhel 5" ,how do I achieve that ? 我想检查列表中的每个项目是否都包含子字符串:el5如果列表中的所有对象都包含el5,则分配一个变量rhel =“ rhel 5”,我该如何实现?

To test if every item matches a certain condition, try the builtin function all() along with a generator expression . 要测试每个项目是否都符合特定条件,请尝试使用内置函数all()以及生成器expression In the generator expression, the test for substring containment is the in operator . 在生成器表达式中,子字符串包含的测试是in运算符

To filter a list, selecting only items which match some condition, use a list comprehension . 要过滤列表,仅选择符合条件的项目,请使用列表理解

To print a list,the str.join() method is often useful. 要打印列表, str.join()方法通常很有用。

ERRATA_PKG_LIST = [u'kernel-2.6.18-416.el5.i686', u'kernel-2.6.18-416.el5.x86_64', u'kernel-debug-2.6.18-416.el5.i686', u'kernel-debug-2.6.18-416.el5.x86_64', u'kernel-debug-devel-2.6.18-416.el5.i686', u'kernel-debug-devel-2.6.18-416.el5.x86_64', u'kernel-devel-2.6.18-416.el5.i686', u'kernel-devel-2.6.18-416.el5.x86_64', u'kernel-doc-2.6.18-416.el5.noarch', u'kernel-headers-2.6.18-416.el5.i386', u'kernel-headers-2.6.18-416.el5.x86_64', u'kernel-PAE-2.6.18-416.el5.i686', u'kernel-PAE-devel-2.6.18-416.el5.i686', u'kernel-xen-2.6.18-416.el5.i686', u'kernel-xen-2.6.18-416.el5.x86_64', u'kernel-xen-devel-2.6.18-416.el5.i686', u'kernel-xen-devel-2.6.18-416.el5.x86_64']


# To determine if 'el5' appears in every item
if all('el5' in item for item in ERRATA_PKG_LIST):
    rhel = "rhel 5"

# To generate the list of items which contain 'el5'
el5_list = [item for item in ERRATA_PKG_LIST if 'el5' in item]

# To generate the list of items which do NOT contain 'el5'
not_el5_list = [item for item in ERRATA_PKG_LIST if 'el5' not in item]

# To print a list:
print('\n'.join(el5_list))
if all(['el5' in item for item in ERRATA_PKG_LIST]):
    rhel = "rhel 5"
if all(map(lambda pkg: "el5" in pkg, ERRATA_PKG_LIST)):
        rhel = "rhel 5"

You could try using in . 您可以尝试in使用。

def CheckList(list_items):
    for item in list_items:
        if 'el5' not in item:
            return False
    return True

This way you are going to loop through all elements of the list and check if the substring 'el5' is not in that item, if that evaluates true, your function returns False. 这样,您将遍历列表的所有元素,并检查子字符串“ el5”是否不在该项目中,如果该结果为true,则函数返回False。 Otherwise, it will end the loop and return True. 否则,它将结束循环并返回True。

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

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