Python 如何返回字符串列表中以数字结尾的列表中的最后一个字符串?
所以我有一个字符串列表。像这样:Python 如何返回字符串列表中以数字结尾的列表中的最后一个字符串?,python,string,list,Python,String,List,所以我有一个字符串列表。像这样: [[['F0','K4','N']], [['S', 'B2', 'A5']]] 我想返回一个字符串列表,其中包含每个子列表中以数字结尾的最后一项。所以这应该给我一个结果: ['K4','A5'] 以下是我尝试过的: result = [] for line in poem_pronunciation: for words in line: for i,unit in reversed(list(enumera
[[['F0','K4','N']], [['S', 'B2', 'A5']]]
我想返回一个字符串列表,其中包含每个子列表中以数字结尾的最后一项。所以这应该给我一个结果:
['K4','A5']
以下是我尝试过的:
result = []
for line in poem_pronunciation:
for words in line:
for i,unit in reversed(list(enumerate(words))):
if unit[-1] in '0123456789':
result.append(words[i])
return result
这会给我一个结果
['K4', 'F0', 'A5', 'B2']
显然,它包括所有以数字结尾的字符串,并且顺序相反。如何解决这个问题?
任何帮助都将不胜感激。您可以使用以下方法:
l = [[['F0','K4','N']], [['S', 'B2', 'A5']]]
[next(x for x in s2[::-1] if x[-1].isdigit()) for s1 in l for s2 in s1]
# ['K4', 'A5']
您可以使用以下嵌套理解:
l = [[['F0','K4','N']], [['S', 'B2', 'A5']]]
[next(x for x in s2[::-1] if x[-1].isdigit()) for s1 in l for s2 in s1]
# ['K4', 'A5']
添加元素时,可以通过中断来停止循环 如果要简化第三个外观,请从另一个角度看 收集以数字结尾的项目 以相反的方式读取,取第一个,使用next获取迭代器的第一项
for line in poem_pronunciation:
for words in line:
result.append(next(x for x in words[::-1] if x[-1].isdigit()))
所有的简化只是一个列表理解的问题
def getLasts(poem_pronunciation):
return [next(x for x in words[::-1] if x[-1].isdigit())
for line in poem_pronunciation
for words in line]
添加元素时,可以通过中断来停止循环 如果要简化第三个外观,请从另一个角度看 收集以数字结尾的项目 以相反的方式读取,取第一个,使用next获取迭代器的第一项
for line in poem_pronunciation:
for words in line:
result.append(next(x for x in words[::-1] if x[-1].isdigit()))
所有的简化只是一个列表理解的问题
def getLasts(poem_pronunciation):
return [next(x for x in words[::-1] if x[-1].isdigit())
for line in poem_pronunciation
for words in line]
当面对一个问题时,通常最好把它分成几个部分,并为它创建抽象。特别是在这种情况下,您有两个部分: 迭代数据 对于给定的元素列表,查找最后一个以数字作为最后一个字符的元素。 对于2,您可以创建一个函数,一种可能性是:
def last(lst):
"""Returns the las element in a list that the last character is a list"""
return next((e for e in reversed(lst) if e[-1].isdigit()), '')
检查它是否工作:
print(last(['F0', 'K4', 'N']))
输出
现在1相对简单,可以这样做:
result = []
for lines in poem:
for words in lines:
result.append(last(words))
print(result)
作为替代方案,您可以使用,如下所示:
poem = [[['F0', 'K4', 'N']], [['S', 'B2', 'A5']]]
def last(lst):
"""Returns the las element in a list that the last character is a list"""
return next((e for e in reversed(lst) if e[-1].isdigit()), '')
result = [last(words) for lines in poem for words in lines]
print(result)
输出
重要的是,当面对一个问题时,试着将它分成几个部分,为这些部分创建抽象,并将这些部分放在一起作为最后一步。当面对一个问题时,通常最好将它分成几个部分,并为其创建抽象。特别是在这种情况下,您有两个部分: 迭代数据 对于给定的元素列表,查找最后一个以数字作为最后一个字符的元素。 对于2,您可以创建一个函数,一种可能性是:
def last(lst):
"""Returns the las element in a list that the last character is a list"""
return next((e for e in reversed(lst) if e[-1].isdigit()), '')
检查它是否工作:
print(last(['F0', 'K4', 'N']))
输出
现在1相对简单,可以这样做:
result = []
for lines in poem:
for words in lines:
result.append(last(words))
print(result)
作为替代方案,您可以使用,如下所示:
poem = [[['F0', 'K4', 'N']], [['S', 'B2', 'A5']]]
def last(lst):
"""Returns the las element in a list that the last character is a list"""
return next((e for e in reversed(lst) if e[-1].isdigit()), '')
result = [last(words) for lines in poem for words in lines]
print(result)
输出
重要的是,当面对一个问题时,试着将它分成几个部分,为这些部分创建抽象,并将这些部分放在一起作为最后一步。首先,我建议您使用一个函数来完成此任务,以便您可以随时参考它 我制作了两个函数,第一个函数以数字结尾,通过在try语句中使用int函数来检查字符串是否以数字结尾
def ends_with_digit(string):
try:
int(string[-1])
except ValueError:
return False
return True
然后,需要一个返回最后一项的函数。因为您有3d列表,所以需要三个循环来完成任务。检查元素后,如果该语句为True,则必须停止循环,以便最后一个循环不会继续追加所有项
def last_digit_items(list):
out = []
for sub1 in list:
for sub2 in sub1:
for sub3 in reversed(sub2):
if ends_with_digit(sub3):
out.append(sub3)
break
return out
另一种方法是递归地过滤列表,然后索引每个子列表的最后一项,但是上面的函数对于您的情况要简单得多
要测试该函数,请运行以下print语句
print(last_digit_items([[['F0','K4','N']], [['S', 'B2', 'A5']]]))
它返回:
['K4', 'A5']
首先,我建议您使用一个函数来完成此任务,以便您可以随时引用它 我制作了两个函数,第一个函数以数字结尾,通过在try语句中使用int函数来检查字符串是否以数字结尾
def ends_with_digit(string):
try:
int(string[-1])
except ValueError:
return False
return True
然后,需要一个返回最后一项的函数。因为您有3d列表,所以需要三个循环来完成任务。检查元素后,如果该语句为True,则必须停止循环,以便最后一个循环不会继续追加所有项
def last_digit_items(list):
out = []
for sub1 in list:
for sub2 in sub1:
for sub3 in reversed(sub2):
if ends_with_digit(sub3):
out.append(sub3)
break
return out
另一种方法是递归地过滤列表,然后索引每个子列表的最后一项,但是上面的函数对于您的情况要简单得多
要测试该函数,请运行以下print语句
print(last_digit_items([[['F0','K4','N']], [['S', 'B2', 'A5']]]))
它返回:
['K4', 'A5']
为什么答案是:用另一种方式做而不解释它的问题?为什么答案是:用另一种方式做而不解释它的问题?谢谢你的帮助。我在课堂上还没有学会如何使用休息时间,所以我不知道如何使用它。你的回答对我很有帮助!我会简化我的代码:谢谢你的帮助。我在课堂上还没有学会如何使用休息时间,所以我不知道如何使用它。你的回答对我很有帮助!我会简化我的代码: