Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 排除数组中不包含特定小数的元素_Python_Arrays_For Loop_While Loop - Fatal编程技术网

Python 排除数组中不包含特定小数的元素

Python 排除数组中不包含特定小数的元素,python,arrays,for-loop,while-loop,Python,Arrays,For Loop,While Loop,我有一个数组,其中包含一个十进制数。小数点只能是1、2或3(由用户设置,因此不需要是算法)。我想创建一个函数,排除数组中不包含所有三个小数的元素 例如,在考虑以下阵列和预期输出时: a = np.array([1.1, 1.3, 1.2, 2.1, 2.2]) b = np.array([1.3, 1.2, 1.1, 7.3, 7.1, 8.1, 8.2, 8.3]) #desired output a_usefull = [1.1, 1.3, 1.2] b_usebull = [1.3, 1

我有一个数组,其中包含一个十进制数。小数点只能是1、2或3(由用户设置,因此不需要是算法)。我想创建一个函数,排除数组中不包含所有三个小数的元素

例如,在考虑以下阵列和预期输出时:

a = np.array([1.1, 1.3, 1.2, 2.1, 2.2])
b = np.array([1.3, 1.2, 1.1, 7.3, 7.1, 8.1, 8.2, 8.3])

#desired output
a_usefull = [1.1, 1.3, 1.2]
b_usebull = [1.3, 1.2, 1.1, 8.1, 8.2, 8.3]
a中的元素
2.1
2.2
被排除在外,因为这两个元素都缺少小数点.3。由于缺少小数点2,元素
7.3
7.1
被排除在外。请注意,原始数组的顺序是导入的,因此例如
[1.3,1.1,1.2]
应显示为
[1.3,1.1,1.2]


另一个条件是,例如
[1.1,1.3,2.1,2.2,1.2,2.3]
的输出应该与可以看到的完全相同。例如它是[1,1,2,1,2,2],它不应该是[1,1,1,2,2]。秩序不应该改变

我想先把数组中的所有元素放在地板上,然后对它们进行计数。但是,代码应该在函数中给出。有人能帮我做一段时间吗?或者帮我做一个循环

def remove(id):

return useful_elements

谢谢大家!

您可以从列表中查找数字的所有整数部分开始,用它们制作一个列表,然后检查包含所有三个小数的列表是否存在于较大的列表中

def func(li):

    res = []
    int_li = []

    #Create a list for integers in the input list
    for item in li:
        int_item = int(item)
        if int_item not in int_li:
            int_li.append(int_item)

    #Iterate through the list for integers
    for num in int_li:

        #Create list with all 3 decimals
        check_list = [(num + .1*i) for i in range(1, 4)]
        dec_list = []

        #Iterate through the bigger list
        for number in li:

            #Compare the integer part of element with given integer
            if num == int(number):
                dec_list.append(number)

            #If the found decimal list is present in bigger list with 3 decimals
            if sorted(dec_list) == sorted(check_list):
                #Add it to result and move on to next item
                res.extend(dec_list)
                break

    #Return result
    return res

print(func([1.1, 1.2, 1.3, 2.1, 2.2]))
print(func([1.3, 1.2, 1.1, 7.3, 7.1, 8.1, 8.2, 8.3]))
输出将是

[1.1, 1.2, 1.3]
[1.3, 1.2, 1.1, 8.1, 8.2, 8.3]
以下是我的看法:

# your code goes here
import numpy as np
import math

a = np.array([1.1, 1.2, 1.3, 2.1, 2.2])
b = np.array([1.1, 1.2, 1.3, 7.3, 7.1, 8.1, 8.2, 8.3])

def numpy_filter(arr):
    required_decimals = {1, 2, 3}
    lst = arr.tolist()
    numbers = { math.floor(x) for x in lst }
    fmap = { n: { x for x in lst if math.floor(x) == n } for n in numbers }
    toReturn = []
    for n, decs in fmap.items():
        target_set = { n + x * 0.1 for x in required_decimals }
        if decs == target_set:
            toReturn.extend(target_set)
    return np.array(toReturn)

#desired output
print(numpy_filter(a))
print(numpy_filter(b))
a_usefull = [1.1, 1.2, 1.3]
b_usebull = [1.1, 1.2, 1.3, 8.1, 8.2, 8.3]
首先,我提取底层列表,以便更轻松地管理它

lst = arr.tolist()
然后提取集合中的所有整数部分以避免重复

numbers = { math.floor(x) for x in lst }
之后,我将原始列表分割成一个映射,映射原始列表中包含的每个整数元素

fmap = { n: { x for x in lst if math.floor(x) == n } for n in numbers }
之后,我解析映射的所有元素,查找列表中的元素与应该有的元素之间的差异。映射的任何人都将添加到返回的新列表中

toReturn = []
for n, decs in fmap.items():
    target_set = { n + x * 0.1 for x in required_decimals }
    if decs == target_set:
        toReturn.extend(target_set)
在这里它是简单而简短的

a = np.array([1.1, 1.2, 1.3, 2.1, 2.2])

def remove(id):
    useful_elements=np.array([])
    for x in np.unique(a.astype(int)):
         if((x+.1 in a) and (x+.2 in a) and (x+.3 in a)):
             useful_elements=np.append(useful_elements,(x+.1,x+.2,x+.3))

    return useful_elements

有点难看,但
[a中的k表示k,如果a中的all((int(k)+0.1*i)表示i在范围(1,4))内]
它们是否总是按照x.1、x.2、x.3中的顺序排列?是的,输出数组的顺序应该与原始数组的顺序相同。不过,小数不需要按相同的顺序排列。[2.3,2.1,2.2]没问题。在这种情况下,您可以创建您的十进制列表,并检查该十进制列表是否是输入列表@vectorizinglife的子列表,该列表中的小数和整数部分也将按顺序排列检查我的答案below@vectorizinglife我是说在原始列表中,这些数字总是按那个顺序排列吗?您的代码可以工作,但它会更改元素的索引号。如果给定[1.3,1.1,1.2],它会将其更改为[1.1,1.2,1.3]——我不希望这样,但感谢您提供的简单而简短的代码!