Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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 查看1D数组中的元素是否位于另一个MD数组中_Python_Arrays_Find - Fatal编程技术网

Python 查看1D数组中的元素是否位于另一个MD数组中

Python 查看1D数组中的元素是否位于另一个MD数组中,python,arrays,find,Python,Arrays,Find,如何检查ArrayId中是否存在ArrayId中的任何元素 到目前为止,我只知道find()方法,它似乎只适用于一个元素搜索 编辑:我还想从arrayMD获取它的索引 使用 输出: (“书”,0,1) 它不是很有效,因为它遍历每个数组中的每个元素,但它可以工作 输出: (“书”,0,1) 它不是很有效,因为它会遍历每个数组中的每个元素,但它是有效的。如果您只是想知道该元素是否在第二个“数组”中的任何位置,那么可能最好先将其展平,这也具有能够处理任何深度的数组的优势。如果您不确定列表的深度,那么使

如何检查ArrayId中是否存在ArrayId中的任何元素

到目前为止,我只知道find()方法,它似乎只适用于一个元素搜索

编辑:我还想从arrayMD获取它的索引

使用

输出: (“书”,0,1)

它不是很有效,因为它遍历每个数组中的每个元素,但它可以工作

输出: (“书”,0,1)


它不是很有效,因为它会遍历每个数组中的每个元素,但它是有效的。

如果您只是想知道该元素是否在第二个“数组”中的任何位置,那么可能最好先将其展平,这也具有能够处理任何深度的数组的优势。如果您不确定列表的深度,那么使用
numpy
最容易做到这一点

array1D = ['book', 'aa', 'Ab', 'AB']
arrayMD = [['ss', 'book', 'fd', '2'], ['sw', 'd'], ['we', 'wr']]

for word in array1D:
    for arrindex, subarr in enumerate(arrayMD):
        for wordindex, subword in enumerate(subarr):
            if word == subword:
                print(word, arrindex, wordindex)
                break

如果您只是想知道元素是否在第二个“数组”中的任何位置,那么可能最好先将其展平,这也具有能够处理任何深度的数组的优势。如果您不确定列表的深度,那么使用
numpy
最容易做到这一点

array1D = ['book', 'aa', 'Ab', 'AB']
arrayMD = [['ss', 'book', 'fd', '2'], ['sw', 'd'], ['we', 'wr']]

for word in array1D:
    for arrindex, subarr in enumerate(arrayMD):
        for wordindex, subword in enumerate(subarr):
            if word == subword:
                print(word, arrindex, wordindex)
                break
def检查(列表md,列表1d):
def展平(l,索引=无):
"""
此函数将递归展平列表
仅获取在何处找到的元素
并返回元组列表
"""
对于i,enumerate(l)中的el:#使用enumerate获取索引
_index=[i]如果index是None-else index+[i]#获取嵌套列表的索引
如果isinstance(el,collections.Iterable)而不是isinstance(el,(str,bytes)):
对于展平中的sub(el,_索引):
屈服分接头
其他:
"""
返回(
,
)
"""
如果列表_1d中的el:
收益率指数
返回列表(展平(列表\ md))
#范例
打印(检查([1,2,3,4,5,6,20]]],[5,20,29]))
#你的榜样
列表md_示例=[[ss]、[book]、[fd]、[2']、[sw]、[d']、[we]、[wr']
列表1d_示例=['book','aa','Ab','Ab']
印刷品(
检查(
举个例子,
列出1d示例
)
)
第一个示例中的输出将是
[(5[2,1,1]),(20[2,1,3,0])]
这意味着找到了数字5,它的索引是[2,1,1]

第二个示例将输出
[('book',[0,1])]

如果返回的列表为空,则表示在MDArray中未找到1DArray中的元素 def展平(l,索引=无): """ 此函数将递归展平列表 仅获取在何处找到的元素 并返回元组列表 """ 对于i,enumerate(l)中的el:#使用enumerate获取索引 _index=[i]如果index是None-else index+[i]#获取嵌套列表的索引 如果isinstance(el,collections.Iterable)而不是isinstance(el,(str,bytes)): 对于展平中的sub(el,_索引): 屈服分接头 其他: """ 返回( , ) """ 如果列表_1d中的el: 收益率指数 返回列表(展平(列表\ md)) #范例 打印(检查([1,2,3,4,5,6,20]]],[5,20,29])) #你的榜样 列表md_示例=[[ss]、[book]、[fd]、[2']、[sw]、[d']、[we]、[wr'] 列表1d_示例=['book','aa','Ab','Ab'] 印刷品( 检查( 举个例子, 列出1d示例 ) ) 第一个示例中的输出将是
[(5[2,1,1]),(20[2,1,3,0])]
这意味着找到了数字5,它的索引是[2,1,1]

第二个示例将输出
[('book',[0,1])]


如果返回的列表为空,则表示在MDArray中找不到1DArray中的元素

arrayMD是否始终为2d?或者它可以有不同的尺寸?尺寸变化很大。arrayMD总是2d吗?或者它可以有不同的维度?维度变化很大。这适用于三维矩阵,还是适用于维度更多的矩阵?(每个维度向列表中添加一个层。)此特定代码不起作用,如果
arrayMD
是二维列表,则此功能有效。现在的问题是,是否要在n嵌套列表中查找元素的索引?或者将整个列表展平以获取索引?我可以相应地更新我的答案。OP的评论说维度的数量“变化很大”。因此,让你的代码适用于任意数量的维度将改善你的答案。这对三维矩阵有效吗,还是对那些维度更多的矩阵有效?(每个维度向列表中添加一个层。)此特定代码不起作用,如果
arrayMD
是二维列表,则此功能有效。现在的问题是,是否要在n嵌套列表中查找元素的索引?或者将整个列表展平以获取索引?我可以相应地更新我的答案。OP的评论说维度的数量“变化很大”。因此,让你的代码为任意数量的维度工作将改善你的答案。
array1D = ['book', 'aa', 'Ab', 'AB']
arrayMD = [['ss', 'book', 'fd', '2'], ['sw', 'd'], ['we', 'wr']]

for word in array1D:
    for arrindex, subarr in enumerate(arrayMD):
        for wordindex, subword in enumerate(subarr):
            if word == subword:
                print(word, arrindex, wordindex)
                break
import numpy as np

arrayMD_flat = np.array(arrayMD).flatten()

for item in array1D:
    if item in arrayMD_flat:
        print('{0} was found!'.format(item))
def check(list_md, list_1d):
    def flatten(l, index=None):
        """
         this function will flatten list_md recursively
         getting only elements which where found 
         and return list of tuples
        """
        for i, el in enumerate(l):  # using enumerate to get index
            _index = [i] if index is None else index + [i]  # getting nested list's indexes
            if isinstance(el, collections.Iterable) and not isinstance(el, (str, bytes)):
                for sub in flatten(el, _index):
                    yield sub

            else:
                """
                returning (
                        <element itself>,
                        <index of element>
                    )
                """
                if el in list_1d:
                    yield el, _index


    return list(flatten(list_md))

# example
print(check([1, 2, [3, [4, 5, 6, [20]]]], [5, 20, 29]))

# your example
list_md_example = [['ss', 'book', 'fd', '2'], ['sw', 'd'], ['we', 'wr']]
list_1d_example = ['book', 'aa', 'Ab', 'AB']
print(
    check(
        list_md_example,
        list_1d_example
    )
)