Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List - Fatal编程技术网

如何在python中获取列表中某个内容之后的项?

如何在python中获取列表中某个内容之后的项?,python,list,Python,List,我正在使用textblob解析英文文本。这是一个单词列表 ['Get', 'coffee', 'and', 'water', 'here'] 我想在一个名为list1的新列表中获取“get”项后面的所有单词 我想把“get”和“here”之间的所有单词都放到一个名为list2的新列表中 最好的方法是什么?是否有任何预构建的函数来执行此操作,因为我希望保持脚本干净和简单。 如有任何建议,将不胜感激 我尝试使用这些函数,但它们不起作用,返回任何空列表 def find_between( s, fi

我正在使用textblob解析英文文本。这是一个单词列表

['Get', 'coffee', 'and', 'water', 'here']
  • 我想在一个名为list1的新列表中获取“get”项后面的所有单词

  • 我想把“get”和“here”之间的所有单词都放到一个名为list2的新列表中

  • 最好的方法是什么?是否有任何预构建的函数来执行此操作,因为我希望保持脚本干净和简单。 如有任何建议,将不胜感激

    我尝试使用这些函数,但它们不起作用,返回任何空列表

    def find_between( s, first, last ):
        try:
            start = s.index( first ) + len( first )
            end = s.index( last, start )
            return s[start:end]
        except ValueError:
            return ""
    
    def find_between_r( s, first, last ):
        try:
            start = s.rindex( first ) + len( first )
            end = s.rindex( last, start )
            return s[start:end]
        except ValueError:
            return ""
    

    如果l=['Get','coffee','and','water','here']

    然后,您必须执行以下操作:

    l2 = l[1:]
    l3 = l[1:4]
    

    如果l=['Get','coffee','and','water','here']

    然后,您必须执行以下操作:

    l2 = l[1:]
    l3 = l[1:4]
    

    您可以通过查找单词的索引并使用切片来完成此操作:

    try:
        get_index = l.index('Get')
        here_index = l.index('here')
    except IndexError:
        raise ValueError("Get or here is missing")
    
    without_get = l[get_index + 1:]
    without_both = l[get_index + 1:here_index]
    

    您可以通过查找单词的索引并使用切片来完成此操作:

    try:
        get_index = l.index('Get')
        here_index = l.index('here')
    except IndexError:
        raise ValueError("Get or here is missing")
    
    without_get = l[get_index + 1:]
    without_both = l[get_index + 1:here_index]
    

    你能发布你尝试过的吗?你能发布你尝试过的吗?不,不,我想根据单词而不是列表的长度来获取单词。例如,在“Get”@DasmondMiles之后抓取单词,这将切分列表;它没有给出长度。@DasmonMiles然后您需要迭代元素,并根据您的属性将元素添加到另一个列表中criteria@Carcigenicate:他使用
    4
    ,因为
    'here'
    这个词正好在那个位置,但OP希望它在不知道的情况下工作。@zondo啊,我误读了他的评论。你说得对。这个答案的第一个版本就是这样的。不,不,我想根据单词而不是列表的长度来抓取单词。例如,在“Get”@DasmondMiles之后抓取单词,这将切分列表;它没有给出长度。@DasmonMiles然后您需要迭代元素,并根据您的属性将元素添加到另一个列表中criteria@Carcigenicate:他使用
    4
    ,因为
    'here'
    这个词正好在那个位置,但OP希望它在不知道的情况下工作。@zondo啊,我误读了他的评论。你说得对。这个答案的第一个版本就是这样的。