Код: Выделить всё
from typing import List, Optional
my_list: List[Optional[int]] = list()
for i in range(7):
my_list.append(i)
my_list = sorted(my_list)
if (len(my_list) > 0):
my_list.append(None)
# do something with my_list
Код: Выделить всё
x.py:6: error: Value of type variable "SupportsRichComparisonT" of "sorted" cannot be "Optional[int]"
Код: Выделить всё
from typing import List
my_list: List[int] = list()
for i in range(7):
my_list.append(i)
my_list = sorted(my_list)
if (len(my_list) > 0):
my_list.append(None)
# do something with my_list
Код: Выделить всё
x.py:8: error: Argument 1 to "append" of "list" has incompatible type "None"; expected "int"
Элемент «Нет» в конце списка необходим в части сценария «сделать что-нибудь» (которая не имеет отношения к проблеме и поэтому не показана здесь)
EDIT (19.09.2022) START
Как и просили, это продуктивный код:
Код: Выделить всё
from typing import List, Optional
def parse_job_attributes(string: str, tokens: List[str]) -> List[str]:
indices = []
for token in tokens:
if token in string.lower():
indices.append(string.lower().index(token))
sorted_indices: List[Optional[int]] = list(sorted(indices))
# indices = sorted(indices)
if len(sorted_indices) > 0:
sorted_indices.append(None)
parts = [string[sorted_indices[i]:sorted_indices[i + 1]] for i in range(len(sorted_indices) - 1)]
return parts
Подробнее здесь: https://stackoverflow.com/questions/737 ... st-of-ints