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

Python 作为列表的一部分

Python 作为列表的一部分,python,list,Python,List,我有一个列表lst lst = ['Hi', 'Hello', '4', '71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '', '70.3', '73.3', ''] 我想按原始列表的一部分创建一个新列表:在第三个元素之后,所有其他元素都在一个忽略空字符串的列表中 lst = ['Hi', 'Hello', '4', [71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]] 我

我有一个列表
lst

lst = ['Hi', 'Hello', '4', '71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '', '70.3', '73.3', '']
我想按原始列表的一部分创建一个新列表:在第三个元素之后,所有其他元素都在一个忽略空字符串的列表中

lst = ['Hi', 'Hello', '4', [71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]]

我正在尝试
(lst[2:])。在第三个元素忽略空字符串并将它们作为数字放入列表后,split()

您可以这样做:

lst = ['Hi', 'Hello', '4', '71.5', '', '71.5', '', '68.1', '', '69', '', '69.4', '', '69.4', '', '70.3', '73.3', '']

result = lst[:3] + [[float(e) for e in lst[3:] if e]]

print(result)
输出

['Hi', 'Hello', '4', [71.5, 71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]]

过滤器
+
映射
(有点长):

现在:

是:


展示您自己解决问题的努力和代码(如问题中格式正确的文本)
lst = lst[:3]+[list(filter(lambda i: type(i)==float,map(lambda x: float(x) if x else x,lst[3:])))]
print(lst)
['Hi', 'Hello', '4', [71.5, 71.5, 68.1, 69.0, 69.4, 69.4, 70.3, 73.3]]