如何从数组中删除不符合条件的某些值?

时间:2017-01-07 11:33:12

标签: python arrays matlab loops vector

如果我有一个由数字编号组成的数组,请说

A = [1.3, 2.2, 2.3,4.2, 5.1, 3.2, 5.3, 3.3, 2.1,1.1, 5.2, 3.1]

我想提取只有整数在数组中的数字,然后是.1,.2和.3。例如,我不想在我的新阵列中包含1,因为A中不存在1.2。我想包括5,因为5.1,5.2和5.3都在A中。我怎么能这样做?接受MatLab或Python答案!

编辑** 谢谢。我现在意识到,我可能会问错误的问题,而不是打印带小数的整数.1,.2,.3我希望最后一个数组由A的浮点数组成,但只有两个都有的浮点数.1,.2,.3落后于同一个整数。遗憾!

6 个答案:

答案 0 :(得分:0)

Python 中,您可以执行以下操作:

import operator

A = [1.3, 2.2, 2.3,4.2, 5.1, 3.2, 5.3, 3.3, 2.1,1.1, 5.2, 3.1]

s = set(int(str(i).split('.')[0]) for i in A)

res = []
for n in s:
    if all(x in A for x in map(operator.add, [n, n, n], [0.1, 0.2, 0.3])):
        res.append(n)

<强>输出:

>>> res
[2, 3, 5]

修改

如果你想要的是获得在同一整数后面有.1,.2和.3的浮点数,请尝试以下方法:

for n in s:
    temp = map(operator.add, [n, n, n], [0.1, 0.2, 0.3])
    if all(x in A for x in temp):
        res.extend(temp)

<强>输出:

>>> res
[2.1, 2.2, 2.3, 3.1, 3.2, 3.3, 5.1, 5.2, 5.3]

答案 1 :(得分:0)

使用 Python ,您可以:

import numpy as np
A = [1.3, 2.2, 2.3, 4.2, 5.1, 3.2, 5.3, 3.3, 2.1, 1.1, 5.2, 3.1]
b = set([int(i) for i in A if np.all([j in A for j in (np.array([1, 2, 3]) + 10*int(np.floor(i)))/10.0])])

其中b是

set([2, 3, 5])

(可以使用list轻松转换为列表。)

<强>评论

我乘以10并除以10.0以避免浮点运算问题。在问题中不清楚您是否希望比较是基于数字或字符串。

答案 2 :(得分:0)

使用Python,您可以使用itertools.groupby按浮动的值对其进行分组,并检查小数是否是[1,2,3]的超集:

from itertools import groupby
needed_decimals = set([1, 2, 3])

a = [1.3, 2.2, 2.3,4.2, 5.1, 3.2, 5.3, 3.3, 2.1,1.1, 5.2, 3.1]

pairs = [tuple(int(y) for y in str(x).split('.')) for x in sorted(a)]
#=> [(1, 1), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3), (4, 2), (5, 1), (5, 2), (5, 3)]
integers = [integer for integer, numbers in groupby(pairs, lambda x: x[0]) if set(x[1] for x in numbers) >= needed_decimals]
#=> [2,3,5]

答案 3 :(得分:0)

MATLAB 中(可能不是最好的算法):

clc; clear;

tolerance = 10^(-15);
% Tolerance is useful in order to compare arrays, because the "==" operator
% can lean to wrong results.

% The fractional parts we are interested in.
desirable_list = [0.1 0.2 0.3];

% Because we are going to work with ordered lists, we order the
% desirable_list as well.
desirable = sort(desirable_list, 'ascend');

% Our list and the sorted list.
A = [1.3, 2.2, 2.3, 4.2, 5.1, 3.2, 5.3, 3.3, 2.1, 1.1, 5.2, 3.1];
A_sorted = sort(A);

% Getting the integer parts (uniquely) of our list.
integer_parts = unique(fix(A));

% Getting the elements that satisfy our criteria in an ordered list in an
% array called S.
S = [];
for j = integer_parts
    m1 = (A_sorted(fix(A_sorted) == j));
    if m1 < 0
        m1 = sort(m1, 'descend');
    end


    if length(m1) == length(desirable) & abs(abs(m1) - desirable) < abs(j) + tolerance
        S = [S m1];
    end
end

% Getting the elements we are interested in, in the specific order they
% appear in our original list.

V = zeros(1,length(S));
l = 1;
for k = 1:length(A)
    if ismember(A(k), S)
        V(l)= A(k);
        l = l + 1;
    end
end
A
V

<强>输出:

A =

    1.3000    2.2000    2.3000    4.2000    5.1000    3.2000    5.3000    3.3000    2.1000    1.1000    5.2000    3.1000


V =

    2.2000    2.3000    5.1000    3.2000    5.3000    3.3000    2.1000    5.2000    3.1000

>> 

答案 4 :(得分:0)

MATLAB解决方案:(由于问题中的更新而更新)

A = [1.3, 2.2, 2.3,4.2, 5.1, 3.2, 5.3, 3.3, 2.1,1.1, 5.2, 3.1]
INC = [.1 .2 .3];
F = floor(A);
U = unique(F,'stable');
%find unique  values of F
B = bsxfun(@plus,U,INC.');
%with broadcasting sum each element with [.1 .2 .3]
%in MATLAB R2016b you can write B = U + INC.';
IDX = all(ismember(B,A));
%for each integer I if  I+[.1 .2 .3] exist in the array return index of it
OUT =B(:,IDX);
%extract output

更紧凑的形式:

B = bsxfun(@plus,unique(floor(A),'stable'),INC.');
OUT = B(:,all(ismember(B,A)))

在MATLAB R2016b或Octave中可以写成:

B = unique(floor(A),'stable') + INC.';
OUT = B(:, all(ismember(B,A)))

结果

OUT =

   2.1000   3.1000   5.1000
   2.2000   3.2000   5.2000
   2.3000   3.3000   5.3000

答案 5 :(得分:0)

set([x for map in map(int,l)if if all [[float(str(x)+'。'+ str(e))in l for e in [1,2,3 ]])])

在一行中诀窍