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

如何在以字母表python开头的列表中追加项

如何在以字母表python开头的列表中追加项,python,Python,在python 3中查找以字母表开头的子列表? 如何在以字母表python开头的列表中追加项 import re code_result = [['1', 'abc_123', '0.40','7.55'], ['paragraph', '100', 'ML MY'], ['2','abc_456', '0.99'], ['letters and words','end','99']] index_list = [] sub_list = [] for

在python 3中查找以字母表开头的子列表? 如何在以字母表python开头的列表中追加项

import re

 code_result =  [['1', 'abc_123', '0.40','7.55'], ['paragraph', '100', 'ML MY'],
               ['2','abc_456', '0.99'], ['letters and words','end','99']] 

index_list = []
sub_list = []

for i in range(0,len(code_result)): 
   if code_result[i][0].isalpha():
       index_list.append([i,i-1]) 

for item in range(0,len(index_list)): 
   temp = re.sub('[^0-9a-zA-Z]','',str(code_result[index_list[item][0]]))
   sub_list.append([code_result[index_list[item][1]][1]+" "+temp])
   print(sub_list)

我的代码只适用于子列表中的一个字母表,不超过该字母表

 Expected Output:
    [['abc_123 paragraph 100 MLMY'],['abc_456 letters and words end 99']]

这将以最少的更改完成您需要的操作

import re
code_result = [['1', 'abc_123', '0.40','7.55'], ['paragraph', '100', 'ML MY'], ['2','abc_456', '0.99'], ['letters and words','end','99']]
index_list = []
sub_list = []
for i in range(0,len(code_result)):
   if code_result[i][0][0].isalpha():
      index_list.append([i,i-1])

for item in range(0,len(index_list)):
   temp = re.sub('[^0-9a-zA-Z ]','',str(code_result[index_list[item][0]]))
   sub_list.append([code_result[index_list[item][1]][1]+" "+temp])
print(sub_list)

但我仍然不清楚您正在尝试做什么,我认为无论它是什么,都可以做得更好。

这里有另一个解决方案,它使用内置的startswith方法得到您想要的输出,请参见


这段代码遍历列表,检查列表中的项目是否以“abc”开头并中断内部循环。如果最后一个为True,则会追加后续列表中的所有项

因为只有字母有大写和小写的变化,所以可以将其作为一个条件。整件事可以放在一张单子里:

 sub_list = [[s for s in a if s[0].lower()!=s[0].upper()] for a in code_result]

 # [['abc_123'], ['paragraph', 'ML MY'], ['abc_456'], ['letters and words', 'end']]
请注意,您的问题陈述和预期输出是不明确的。它们还可能意味着:

以仅包含基于问题标题的字母的项目开头的子列表:

[ a  for a in code_result if a[0].lower()!=a[0].upper()]

# [['paragraph', '100', 'ML MY'], ['letters and words', 'end', '99']]

或者,基于预期输出,子列表元素以字母开头,有时单独获取,有时使用整个子列表,并在子列表中任意连接成单个字符串。

您能解释预期输出吗?从示例中不清楚逻辑是什么,您的代码有什么问题?我的代码只适用于子列表中的一个字母表,不超过该字母表。它不包括空格2字。请告诉我你为什么给负片votes@pyml100是以字母开头的吗?是99吗?为什么您只在第一个列表中添加“abc_123”,而将第二个列表全部添加?您在问题中只问了一件事,但您的代码却完全不同。至少试着用谷歌翻译来详细传达你的任务,而不是像机器人回答那样给出关键词[list,start,alphabet,append,python]。然而,由于如何附加以字母字符开头的项和所需输出的问题存在分歧,pyml可能能够将我们的答案结合起来得出一个解决方案。好吧,我仍然看不出在您提出的条件下,您是如何获得所需输出的。然而,我编辑了我的答案,它应该会产生你想要的输出。
[ a  for a in code_result if a[0].lower()!=a[0].upper()]

# [['paragraph', '100', 'ML MY'], ['letters and words', 'end', '99']]