Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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_Algorithm_Loops_List Comprehension - Fatal编程技术网

在Python中,当空格数与最后一个元素中的空格数不匹配时,如何切掉列表

在Python中,当空格数与最后一个元素中的空格数不匹配时,如何切掉列表,python,list,algorithm,loops,list-comprehension,Python,List,Algorithm,Loops,List Comprehension,假设我有一张清单 lst = ['hello world', 'los angeles', 'burgers', 'jacky', '12345', '1 1 1 1', '1 2 3 4', '4 3 2 1'] 我想从末尾开始计数,所以元素'43221',看到它有3个空格,并保持左边也有3个空格的所有内容,直到它达到'12345',因为它没有3个空格。 我怎样才能实现这样的目标?我不想硬编码它,直到它达到0个空格为止,因为“12345”可能会变成类似“12345”的东西。 最终产出将是:

假设我有一张清单

lst = ['hello world', 'los angeles', 'burgers', 'jacky', '12345', '1 1 1 1', '1 2 3 4', '4 3 2 1']
我想从末尾开始计数,所以元素'43221',看到它有3个空格,并保持左边也有3个空格的所有内容,直到它达到'12345',因为它没有3个空格。 我怎样才能实现这样的目标?我不想硬编码它,直到它达到0个空格为止,因为“12345”可能会变成类似“12345”的东西。 最终产出将是:

['1 1 1 1', '1 2 3 4', '4 3 2 1']

您可以反向迭代列表,捕获带有3个空格的值,并在它遇到没有3个空格的内容时中断。输出列表将以相同的相反顺序显示,因此,如果希望以原始顺序显示,则还需要以相反的顺序打印

要使其成为动态的,可以通过计算初始列表最后一个元素中的空格数,将空格数设置为变量:

# initial list
lst = ['hello world', 'los angeles', 'burgers', 'jacky', '12345', '1 1 1 1', '1 2 3 4', '4 3 2 1']

# get the space count from the last element
space_count = lst[-1].count(' ')

# make output list to hold values
output_list = []

# iterate in reverse and gather elements with 
# correct space count and stop when it hits first
# element without that space count
for x in lst[::-1]:
     if x.count(' ') == space_count:
         output_list.append(x)
     else:
         break
print(output_list[::-1])

您可以反向迭代列表,捕获带有3个空格的值,并在它遇到没有3个空格的内容时中断。输出列表将以相同的相反顺序显示,因此,如果希望以原始顺序显示,则还需要以相反的顺序打印

要使其成为动态的,可以通过计算初始列表最后一个元素中的空格数,将空格数设置为变量:

# initial list
lst = ['hello world', 'los angeles', 'burgers', 'jacky', '12345', '1 1 1 1', '1 2 3 4', '4 3 2 1']

# get the space count from the last element
space_count = lst[-1].count(' ')

# make output list to hold values
output_list = []

# iterate in reverse and gather elements with 
# correct space count and stop when it hits first
# element without that space count
for x in lst[::-1]:
     if x.count(' ') == space_count:
         output_list.append(x)
     else:
         break
print(output_list[::-1])

如果您
.split()
列表中的每个元素,则结果列表的长度对于
'4 3 2 1'
为4,对于
'12345'
为1,请注意计数空格是脆弱的。例如,字符串
'4 3'
有三个空格,但只有两个数字。一个包含制表符的字符串可以有四个数字,但不能有空格。是的,我之前确保去掉前导空格和尾随空格,谢谢你
。split()
列表中的每个元素,结果列表的长度对于
'4 3 2 1'
为4,对于
'12345'
为1。请注意,计数空格是脆弱的。例如,字符串
'4 3'
有三个空格,但只有两个数字。一个包含制表符的字符串可以有四个数字,但不能有空格。是的,我之前确保去掉前导空格和尾随空格,我们可以去掉硬编码的“3”吗,让它读取列表的最后一个元素,count num spaces,用它作为我们要比较的数值?@JerryStackhouse只需使用另一个变量并检查空间计数。添加到答案中。我们如何摆脱硬编码的“3”,使其读取列表的最后一个元素,计算num空格,并将其用作我们要比较的num?@JerryStackhouse只需使用另一个变量并检查空格计数。补充到答案中。