Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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/7/css/36.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 - Fatal编程技术网

Python使用索引数组将数组切割成两个数组

Python使用索引数组将数组切割成两个数组,python,arrays,Python,Arrays,我有一个数组,假设arr=[1,2,3,4,5,6,7,8] 还有另一个索引数组:idx=[0,3,4,6] 我想得到两个数组,一个是arr中的索引:[1,4,5,7] 另一个是所有其他的:[2,3,6,8] 有人能帮我吗?我只能想出一些丑陋的方法来做,但一定是一些功能做得很优雅 非常感谢 您可以这样做: selected = [arr[i] for i in idx] other = [v for i, v in enumerate(arr) if i not in idx] 如果arr没有

我有一个数组,假设arr=[1,2,3,4,5,6,7,8] 还有另一个索引数组:idx=[0,3,4,6]

我想得到两个数组,一个是arr中的索引:[1,4,5,7] 另一个是所有其他的:[2,3,6,8]

有人能帮我吗?我只能想出一些丑陋的方法来做,但一定是一些功能做得很优雅


非常感谢

您可以这样做:

selected = [arr[i] for i in idx]
other = [v for i, v in enumerate(arr) if i not in idx]
如果
arr
没有重复项,您还可以执行以下操作:

other = [v for v in arr if v not in selected]
方法:

a1 = [arr[x] for x in idx] 
a2 = [x for x in arr if x not in a1]

您可以使用
itertools
进行单线解决方案:

import itertools
arr = [1, 2, 3, 4, 5, 6, 7, 8]
idx = [0, 3, 4, 6]
[(out_index, not_in_arr), (in_index, in_arr)] = [(a, list(b)) for a, b in itertools.groupby(sorted(arr, key=lambda x:arr.index(x) in idx), key=lambda x:arr.index(x) in idx)]
print(not_in_arr)
print(in_arr)
输出:

[2, 3, 6, 8]
[1, 4, 5, 7]
通过一次遍历:

no, yes = both = [], []
for i, x in enumerate(arr):
    both[i in idx].append(x)
或者(正如Chris_Rands所评论的):

虽然idx对于这个问题来说应该是小的,或者变成一个集合(其他答案中的解决方案也是如此,我想他们只是不想谈论它)

演示:


使用
numpy
有一个简洁的解决方案:

import numpy as np

arr = np.asarray([1, 2, 3, 4, 5, 6, 7, 8])  # converts your list in numpy array
idx1 = [0, 3, 4, 6]
idx2 = [1, 2, 5, 7]

arr1 = arr[idx1]  # [1 4 5 7]
arr2 = arr[idx2]  # [2 3 6 8]

您还可以将
arr
中的每个值映射到字典,指示其索引是否存在于
idx
中:

arr = [1, 2, 3, 4, 5, 6, 7, 8]
idx = [0, 3, 4, 6]

# converted to a set
idx_lookup = set(idx)

d = {x: True if i in idx_lookup else False for i, x in enumerate(arr)}
print(d)
这就产生了这样的措辞:

{1: True, 2: False, 3: False, 4: True, 5: True, 6: False, 7: True, 8: False}
我还将
idx
转换为set,因为在这种情况下,不需要重复索引,set/dictionary查找是
O(1)
。然而,列表查找是
O(n)
,因此如果可能,这种优化是值得的

拥有此词典后,您可以从中找出要保留的元素以及其余元素:

keep = list(filter(lambda x: d[x], arr))
rest = list(filter(lambda x: not d[x], arr))

print(keep)
print(rest)
哪些产出:

[1, 4, 5, 7]
[2, 3, 6, 8]
注意:您也可以在过滤
keep
rest
的上面使用列表理解:

keep = [x for x in arr if d[x]]
rest = [x for x in arr if not d[x]]

效果很好。非常感谢@Chris_Rands嗯,如果我将
两者都重命名为
不是
不是
?我想这是主观的,但我个人更喜欢另一种方式,而且对初学者来说可能更容易混淆perhaps@Chris_Rands好的,我再加上。我想我开始使用我的一个原因是速度更快,但我只是再次计时,速度就慢了。谢谢
[1, 4, 5, 7]
[2, 3, 6, 8]
keep = [x for x in arr if d[x]]
rest = [x for x in arr if not d[x]]