【问题标题】:Python Array Copying From Word To WordPython 数组从 Word 到 Word 复制
【发布时间】:2016-04-11 03:14:09
【问题描述】:

所以我有一个数组,我想复制这个数组中的每个字符串,从它找到我作为条件给出的单词开始,并以另一个具有相同单词的字符串结束。例如,如果我有一个如下数组:

['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']

例如,我想搜索单词循环并将每个字符串复制到一个新数组中,直到它再次找到循环。所以它应该返回这样的东西:

['Loop: atfg','xyzgh','blabla','blablable Loop']

任何帮助将不胜感激,谢谢

【问题讨论】:

    标签: python arrays string


    【解决方案1】:

    以下代码查找包含您的search_str 的第一个和第二个元素的索引

    start_index = my_list.index(next(e for e in my_list if search_str in e))
    end_index = my_list.index(next(e for e in my_list[start_index + 1:] if search_str in e))
    

    要了解如何使用它:

    my_list = ['trash', 'Loop: 1','2','3','4 Loop', 'more trash']
    search_str = "Loop"
    
    start_index = my_list.index(next(e for e in my_list if search_str in e))
    end_index = my_list.index(next(e for e in my_list[start_index + 1:] if search_str in e))
    
    result = my_list[start_index:end_index + 1]
    

    它可能看起来比多行循环有点奇怪,但它更像 Python 方式:]

    【讨论】:

    • 但是 first_match:index 是什么?因为该变量似乎无处不在。会是 start_index 吗?更新:我将其更改为 start_index 并且它有效。谢谢!
    【解决方案2】:

    通过yield对源列表进行一次迭代:

    i = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
    
    def find_range(items):
        start = False
        for i in items:
            if 'Loop' in i:
                yield i
                if start:
                    break
    
                start = True
            elif start:
                yield i
    
    print list(find_range(i))
    

    【讨论】:

    • 因为if start: break 有点困惑,但除此之外还不错:)
    • 开始时,start 为假。当 Loop 第一次匹配时, start 将被设置为 true。当 Loop 再次匹配时, start 已经为 true,所以中断循环。
    【解决方案3】:

    可能有更好的方法来处理这个问题,但我认为一个好的老式循环在这里效果最好:

    def word_copy(lst, search):
        res = []
        for item in lst:
            if res:
                res.append(item)
                if search in item:
                    return res
    
            elif search in item:
                res.append(item)
    

    【讨论】:

    • 请注意开头和结尾的OP标准 - 不是包含
    【解决方案4】:

    尝试做这样的事情:

    list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
    if any("loop" in l for l in list):
    

    【讨论】:

    • 这将执行if,其中“循环”一词出现在列表的任何元素中 - 这对 OP 查询有何帮助?
    【解决方案5】:

    遍历列表,查找第一个匹配项,然后是第二个匹配项:

    input = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
    target = 'Loop'
    
    start_index, end_index = None, None
    for i in input:
        if target in i:
            if start_index is None:
                 start_index = input.index(i)
                 continue
            end_index = input.index(i)
            break
    
    output = input[start_index : end_index + 1]
    

    【讨论】:

      【解决方案6】:

      你的清单:

      list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
      

      我认为您可以尝试这样做,以便找到数组中的位置:

      ixs = [i for i, word in enumerate(list) if word.startswith('Loop') or word.endswith('Loop')]
      

      那么你只需要对列表进行切片:

      res = list[ixs[0]:ixs[1]+1]
      

      希望这对你有帮助。

      【讨论】:

        【解决方案7】:

        我看到有一些花哨的单线解决方案。 :) 然而,我喜欢看起来更理解的蛮力:

        >>> my_list = ['abcde','Loop: atfg','xyzgh','blabla','blablable Loop','other thing']
        >>> search_str = "(Loop)+"
        >>> out = []
        >>> count = 0
        >>> for s in my_list:
        ...     if count == 2:
        ...         break
        ...     m = re.search(search_str, s)
        ...     if m != None:
        ...         count += 1
        ...     if count >= 1:
        ...         out.append(s)
        ...
        >>> out
        ['Loop: atfg', 'xyzgh', 'blabla', 'blablable Loop']
        >>>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多