Python 如何返回列表中以“开始”开头的第二个元素;b";
我有一个包含字符串的列表的函数,我必须找到列表中以“b”开头的第二个元素 例如:Python 如何返回列表中以“开始”开头的第二个元素;b";,python,python-3.x,Python,Python 3.x,我有一个包含字符串的列表的函数,我必须找到列表中以“b”开头的第二个元素 例如: second_elemnt_starting_with_b(["b", "a", "bb"]) => "bb" 试试这个: def second_elemnt_starting_with_b(list_): return [i for i in list_ if i.startswith('b')][1] print(second_elemnt_starting_with_b(["b", "a"
second_elemnt_starting_with_b(["b", "a", "bb"]) => "bb"
试试这个:
def second_elemnt_starting_with_b(list_):
return [i for i in list_ if i.startswith('b')][1]
print(second_elemnt_starting_with_b(["b", "a", "bb"]))
输出:
'bb'
试试这个:
def second_elemnt_starting_with_b(list_):
return [i for i in list_ if i.startswith('b')][1]
print(second_elemnt_starting_with_b(["b", "a", "bb"]))
输出:
'bb'
输出:
"bb"
或者作为一种功能:
def func(lst):
return [i for i in lst if i.startswith("b")][1]
输出:
"bb"
或者作为一种功能:
def func(lst):
return [i for i in lst if i.startswith("b")][1]
使用a将更有效,而不是通过迭代整个初始列表来构建以“b”开头的所有字符串的列表,然后只保留第二个字符串
def second_element_starting_with_b(lst):
# g is a generator, it will produce items lazily when we call 'next' on it
g = (item for item in lst if item.startswith('b'))
next(g) # skip the first one
return next(g)
second_element_starting_with_b(["b", "a", "bb"])
# 'bb'
这样,一旦找到我们要查找的字符串,代码就停止在初始列表上迭代
正如@Chris_Rands所建议的,也可以通过使用避免重复调用next。这样,查找以“b”开头的第n项的扩展版本如下所示:
from itertools import islice
def nth_element_starting_with_b(lst, n):
"Return nth item of lst starting with 'b', n=1 for first item"
g = (item for item in lst if item.startswith('b'))
return next(islice(g, n-1, n))
nth_element_starting_with_b(["b", "a", "bb"], 2)
# 'bb'
使用a将更有效,而不是通过迭代整个初始列表来构建以“b”开头的所有字符串的列表,然后只保留第二个字符串
def second_element_starting_with_b(lst):
# g is a generator, it will produce items lazily when we call 'next' on it
g = (item for item in lst if item.startswith('b'))
next(g) # skip the first one
return next(g)
second_element_starting_with_b(["b", "a", "bb"])
# 'bb'
这样,一旦找到我们要查找的字符串,代码就停止在初始列表上迭代
正如@Chris_Rands所建议的,也可以通过使用避免重复调用next。这样,查找以“b”开头的第n项的扩展版本如下所示:
from itertools import islice
def nth_element_starting_with_b(lst, n):
"Return nth item of lst starting with 'b', n=1 for first item"
g = (item for item in lst if item.startswith('b'))
return next(islice(g, n-1, n))
nth_element_starting_with_b(["b", "a", "bb"], 2)
# 'bb'
您可以使用python内置函数startswith()检查字符串的第一个元素
lst = ["b", "a", "bb"]
# Check the first element
sample = "Sample"
print(sample.startswith("S"))
# Output : True
现在,您需要遍历列表以检查以b开始的每个索引
# Create empty list
lst_2 = []
# Loop through list
for element in lst.startswith("b"):
# Add every element starts with b
lst_2.append(element)
# Print the second element which starts with b
print(lst_2[1])
您可以使用python内置函数startswith()检查字符串的第一个元素
lst = ["b", "a", "bb"]
# Check the first element
sample = "Sample"
print(sample.startswith("S"))
# Output : True
现在,您需要遍历列表以检查以b开始的每个索引
# Create empty list
lst_2 = []
# Loop through list
for element in lst.startswith("b"):
# Add every element starts with b
lst_2.append(element)
# Print the second element which starts with b
print(lst_2[1])
如果没有以b开头的函数,那么预期的输出是什么?如果没有以b开头的函数,那么预期的输出是什么?是的,如果你有一个函数,并且列表是一个参数,那么预期的输出是无效的:例如,如果我有:def get_2nd_elemnt_start_with_b(list),它需要返回字符串,第二个元素以“b”开头是的,如果你有一个函数,并且列表是一个参数,它就不起作用了:例如,如果我有:def get_2nd_elemnt_start_with_b(list),它需要返回字符串,第二个元素以“b”开头更一般
next(itertools.islice(g,1,2))
谢谢,它起作用了,但是你能进一步解释一下“逐项”是什么意思吗这段代码中有什么特别之处?@felexeriino:这是一个生成器表达式,如中所述。语法类似于列表理解的语法(用括号代替方括号),但没有生成列表,当一个人在生成器上迭代时,值会一个接一个地生成。@Chris_Rands这是个好主意,我在答案中添加了一个扩展版本。更一般的next(itertools.islice(g,1,2))
谢谢,它成功了,但是您能进一步解释一下“逐项逐项”在这段代码中的作用吗?@felexeriino:这是一个生成器表达式,如中所述。语法类似于列表理解的语法(用括号代替方括号),但没有生成列表,当一个人在生成器上迭代时,值会一个接一个地生成。@Chris_Rands这是个好主意,我在答案中添加了一个扩展版本。