Python检查列表项是否包含任何其他列表项

时间:2017-07-27 12:15:54

标签: python list if-statement contains

我有这个问题,如果它包含'非法'字符,我想删除列表元素。合法字符在多个列表中指定。它们是这样形成的,<body>代表字母表(az + AZ),$.fn.addLoader = function (loadText: string): JQuery { // prepare divs as constants const overlayDiv = $("<div class=\"load-overlay\" />"); const spinnerDiv = $("<div class=\"load-spinner\" />"); // Note here, you can self-close elements created by JQuery if they're empty. //overlayDiv.append(spinnerDiv); // I've removed this from here as you don't need to append the spinner to the overlay. if (loadText) { const textDiv = $("<div class=\"load-text\">" + loadText + "</div>"); overlayDiv.append(textDiv); } // once this is all done, you then append the spinner to the body (or the parent container, whichever is preferable) $('body').append(spinnerDiv); overlayDiv.appendTo(this.css("position", "relative")); return this; 代表数字(0-9),alpha代表标点符号(排序)。< / p>

digit

这样我可以将某些内容指定为非法字符,如果它没有出现在其中一个列表中。

之后我有一个包含元素的列表:

punct

我想过滤掉包含非法字符的元素。所以这是我想得到的结果(不需要订购):

alpha = list(string.ascii_letters)
digit = list(string.digits)
punct = list(string.punctuation)

编辑:

我试过(以及它的所有变体):

Input = ["Amuu2", "Q1BFt", "dUM€n", "o°8o1G", "mgF)`", "ZR°p", "Y9^^M", "W0PD7"]

其中仅使用过滤后的元素创建新列表,但问题是当包含至少一个合法字符时添加元素。例如:var = ["Amuu2", "Q1BFt", "mgF)`", "Y9^^M", "W0PD7"] 已添加,因为它包含Z,R和p。

我也尝试过:

for InItem in Input:
    if any(AlItem in InItem for AlItem in alpha+digit+punct):
        FilInput.append(InItem)

但在那之后,我无法弄清楚如何删除元素。 哦,还有一点小小的提示,为了让它变得更加困难,如果它有点快,那将会很好,因为它需要做数百万次。但它需要首先工作。

4 个答案:

答案 0 :(得分:5)

定义一组合法字符。然后应用列表理解。

>>> allowed = set(string.ascii_letters + string.digits + string.punctuation)
>>> inp = ["Amuu2", "Q1BFt", "dUM€n", "o°8o1G", "mgF)`", "ZR°p", "Y9^^M", "W0PD7"]
>>> [x for x in inp if all(c in allowed for c in x)]
['Amuu2', 'Q1BFt', 'mgF)`', 'Y9^^M', 'W0PD7']

答案 1 :(得分:1)

您可以使用列表理解,如果所有字符都符合您的条件,请与all核对:

>>> [element for element in Input if all(c in alpha + digit + punct for c in element)]
['Amuu2', 'Q1BFt', 'mgF)`', 'Y9^^M', 'W0PD7']

答案 2 :(得分:1)

您的代码

正如您所提到的,只要any字符是正确的,就会附加字词。您需要检查它们是否all正确无误:

filtered_words = []
for word in words:
    if all(char in alpha+digit+punct for char in word):
        filtered_words.append(word)

print(filtered_words)
# ['Amuu2', 'Q1BFt', 'mgF)`', 'Y9^^M', 'W0PD7']

您还可以检查是否有一个不正确的字符:

filtered_words = []
for word in words:
    if not any(char not in alpha+digit+punct for char in word):
        filtered_words.append(word)

print(filtered_words)

它的可读性要低得多。

为了提高效率,您不应在每次迭代期间使用alpha+digit+punct连接列表。你应该在任何循环之前一劳永逸地做到这一点。从这些列表中创建一个集合也是一个好主意,因为当有许多允许的字符时,char in setchar in list快得多。

最后,您可以使用列表推导来避免for循环。如果您这样做,最终会得到@timgeb's solution:)

替代正则表达式

您可以从列表中创建正则表达式模式,并查看匹配的字词:

# encoding: utf-8
import string
import re

alpha = list(string.ascii_letters)
digit = list(string.digits)
punct = list(string.punctuation)

words = ["Amuu2", "Q1BFt", "dUM€n", "o°8o1G", "mgF)`", "ZR°p", "Y9^^M", "W0PD7"]

allowed_pattern = re.compile(
    '^[' +
    ''.join(
        re.escape(char) for char in (
            alpha +
            digit +
            punct)) +
    ']+$')
# ^[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^_\`\{\|\}\~]+$

print([word for word in words if allowed_pattern.match(word)])
# ['Amuu2', 'Q1BFt', 'mgF)`', 'Y9^^M', 'W0PD7']

你也可以写:

print(list(filter(allowed_pattern.match, words)))
# ['Amuu2', 'Q1BFt', 'mgF)`', 'Y9^^M', 'W0PD7']

re.compile可能比简单地初始化set需要更多时间,但过滤速度可能会更快。

答案 3 :(得分:1)

对于您的问题,这是一个“非”有效的解决方案,但学习如何循环列表,字符等可能很有趣。

# coding=utf-8
import string

# Aux var
result =[]
new_elem = ""

# lists with legal characters
alpha = list(string.ascii_letters)
digit = list(string.digits)
punct = list(string.punctuation)

# Input strings
Input = ["Amuu2", "Q1BFt", "dUM€n", "o°8o1G", "mgF)`", "ZR°p", "Y9^^M", "W0PD7"]

# Loop all elements of the list and each char of them
for elem in Input:
    ## check each char 
    for char in elem:
        if char in alpha:
            #print 'is ascii'
            new_elem += char
        elif char in digit:
            #print 'is digit'
            new_elem += char
        elif char in punct:
            #print 'is punct'
            new_elem += char
        else:
            new_elem = ""
            break
    ## Add to result list
    if new_elem != "":
        result.append(new_elem)
        new_elem = ""

print result