Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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 为列表编制索引 输入导入列表中的 def up_to_关键字(项目:列表[str],关键字:str)->List[str]: “”“返回一个新列表,该列表仅包含关键字之前出现的项。” 如果关键字不是项的元素,则在项中,或在所有项中。不要 在结果中包含关键字。 """ n_list=[] 如果关键字不在项目中: 退货项目 其他: 对于范围内的i(len(items)): 如果items.index(i)List[str]: 如果关键字不在项目中: 退货项目 其他: 返回[i for i in items if items.index(i)_Python - Fatal编程技术网

Python 为列表编制索引 输入导入列表中的 def up_to_关键字(项目:列表[str],关键字:str)->List[str]: “”“返回一个新列表,该列表仅包含关键字之前出现的项。” 如果关键字不是项的元素,则在项中,或在所有项中。不要 在结果中包含关键字。 """ n_list=[] 如果关键字不在项目中: 退货项目 其他: 对于范围内的i(len(items)): 如果items.index(i)List[str]: 如果关键字不在项目中: 退货项目 其他: 返回[i for i in items if items.index(i)

Python 为列表编制索引 输入导入列表中的 def up_to_关键字(项目:列表[str],关键字:str)->List[str]: “”“返回一个新列表,该列表仅包含关键字之前出现的项。” 如果关键字不是项的元素,则在项中,或在所有项中。不要 在结果中包含关键字。 """ n_list=[] 如果关键字不在项目中: 退货项目 其他: 对于范围内的i(len(items)): 如果items.index(i)List[str]: 如果关键字不在项目中: 退货项目 其他: 返回[i for i in items if items.index(i),python,Python,如果关键字存在于列表中,我将如何进行编码?我的代码是否接近于满足给定条件?更改: from typing import List def up_to_keyword(items: List[str], keyword: str) -> List[str]: """Return a new list that contains only the items that occur before keyword in items, or all items if keyword is not

如果关键字存在于列表中,我将如何进行编码?我的代码是否接近于满足给定条件?

更改:

from typing import List

def up_to_keyword(items: List[str], keyword: str) -> List[str]:
"""Return a new list that contains only the items that occur before keyword
in items, or all items if keyword is not an element of items.  Do not
include keyword in your result.
"""

    n_list = []
    if keyword not in items:
        return items
    else:
        for i in range(len(items)):
            if items.index(i) < items.index(keyword):
                return n_list.append(i)
IIUC:


您可以在此处使用列表理解功能。请尝试以下代码:

def up_to_keyword(items, keyword):
    n_list = []
    if keyword not in items:
        return items

    return items[:items.index(keyword)] 
输入导入列表中的

def up_to_关键字(项目:列表[str],关键字:str)->List[str]:
如果关键字不在项目中:
退货项目
其他:
返回[i for i in items if items.index(i)
希望这有帮助

from typing import List
def up_to_keyword(items: List[str], keyword: str) -> List[str]:
    if keyword not in items:
        return items
    else:
        return [i for i in items if items.index(i) < items.index(keyword)]
不需要变量n_列表。或:

from typing import List

def up_to_keyword(items: List[str], keyword: str) -> List[str]:
"""Return a new list that contains only the items that occur before keyword
in items, or all items if keyword is not an element of items.  Do not
include keyword in your result.
"""

    if keyword not in items:
        return items
    else
        items[:items.index(keyword)]      #List slice up to first occurance of keyword

您可以只使用列表的拼接,即items[:items.index(关键字)]它是什么意思
IIUC
def up_to_keyword(items, keyword):
    n_list = []
    if keyword not in items:
        return items

    return items[:items.index(keyword)] 
from typing import List
def up_to_keyword(items: List[str], keyword: str) -> List[str]:
    if keyword not in items:
        return items
    else:
        return [i for i in items if items.index(i) < items.index(keyword)]
from typing import List

def up_to_keyword(items: List[str], keyword: str) -> List[str]:
"""Return a new list that contains only the items that occur before keyword
in items, or all items if keyword is not an element of items.  Do not
include keyword in your result.
"""

    if keyword not in items:
        return items
    else
        items[:items.index(keyword)]      #List slice up to first occurance of keyword
from typing import List

def up_to_keyword(items: List[str], keyword: str) -> List[str]:

    if keyword in items:
        return items[:items.index(keyword)]      #List slice up to first occurance of keyword
    return items                                 #No need of else