简体   繁体   English

根据数字从python列表中删除元素

[英]Removing elements from a list in python based on digits

I have a list of integers and I want to remove all integers that have 7, 8, 9 or 0 in them. 我有一个整数列表,我想删除其中所有具有7、8、9或0的整数。

This is what I have done so far: 到目前为止,这是我所做的:

import numpy as np
def P(n):
    list = []
    for i in range(10**(n-1),7*10**(n-1)):
        list.append(i)
    for i in list:
        for k in str(i):
            if k in ['7','8','9','0']: list.remove(i)
            #elif k == '8': list.remove(i)
            #elif k == '9': list.remove(i)
            #elif k == '10': list.remove(i)
    return list

But it returns this: 但它返回以下内容:

[11, 12, 13, 14, 15, 16, 18, 20, 21, 22, 23, 24, 25, 26, 28, 30, 31, 32, 33, 34, 35, 36, 38, 40, 41, 42, 43, 44, 45, 46, 48, 50, 51, 52, 53, 54, 55, 56, 58, 60, 61, 62, 63, 64, 65, 66, 68]

How can i fix this code so that it gets rid of the unwanted values? 我该如何解决此代码,使其摆脱不必要的价值? ie 18, 20, 28, 30 etc. 即18、20、28、30等

You can recreate the list using list comprehension and built-in any() method to check if any of the digits is in the unwanted list. 您可以使用列表理解和内置的any()方法重新创建列表,以检查有害列表中是否有任何数字。 In order to iterate through the numbers you need to type cast them to str() first. 为了遍历数字,您需要先将它们str()str() For example: 例如:

def P(n):
    l = []
    for i in range(10**(n-1),7*10**(n-1)):
        l.append(i)
    l = [x for x in l if not any(y in ['7', '8', '9', '0'] for y in str(x))]

Output: 输出:

[11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 45, 46, 51, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66]

You can also avoid creating the initial list and the for loop by using: 您还可以避免使用以下方法创建初始列表和for循环:

def P(n):
    l = range(10**(n-1),7*10**(n-1))
    l = [x for x in l if not any(y in ['7', '8', '9', '0'] for y in str(x))]

another filter test option is to use set intersection 另一个过滤器测试选项是使用set交集

n=2
print([i for i in range(10**(n-1),7*10**(n-1))
         if not {'7','8','9','0'} & set(str(i))
       ])
[11, 12, 13, 14, 15, 16, 21, 22, 23, 24, 25, 26, 31, 32, 33, 34, 35, 36, 41, 42, 43, 44, 45, 46, 51, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66]  

and while the warnings against modifying a list when iterating over it in a Python for are on target 并且在针对Python for迭代时禁止修改列表的警告已在目标位置

you can literally modify a list while 'iterating' in the general sense if you keep track of how you modify the index, how you test for completion 如果您跟踪修改索引的方式,测试完成情况的方式,则可以在一般意义上“迭代”时从字面上修改列表

n =2
lst = list(range(10**(n-1),7*10**(n-1)))
i = 0
while i < len(lst):
    if {'7','8','9','0'} & set(str(lst[i])):
        lst.pop(i)
    else:
        i += 1

First of all change the code before iterating list to list = range (10**n-1,7*10**n-1) range returns a list there is no need to iterate over the list just to copy it. 首先,在将列表迭代到list = range(10 ** n-1,7 * 10 ** n-1)之前,将代码更改为range返回一个列表,无需遍历该列表即可复制它。 Second, in the second loop k iterate over the characters of every string in list. 其次,在第二个循环中,k遍历列表中每个字符串的字符。 for i in list: for k in ['7','8','9','0',]: if k in i: list.remove(i) 对于列表中的i:对于['7','8','9','0',]中的k:如果i中的k:list.remove(i)

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

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