2017-10-12 20 views
0

我会很感激的帮助与以下:如何删除包含字符串和数字的每个子列表中的字符串,并将其余数字加入到一个列表中?

我有一个叫Fun1功能,将采取这样的列表,

['Jo, 60, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'] 

,并把它变成

[['Jo', 77.3], ['Bill', 65.0], ['Cal', 98.0]] 

它以平均属于每个人的三个数字中,然后将每个人的平均分成一个子列表。

现在我想创建一个名为Fun2新的函数,它从 Fun1输出,并把它变成一个列表,只有从Fun1输出int秒。

例如,如果FUN1输出

[['Jo', 77.3], ['Bill', 65.0], ['Cal', 98.0]] 

我想Fun2

[77.3, 65.0, 98.0] 

有谁知道的一种方法,我可以做到这一点?我知道我必须以某种方式从Fun1输出中的每个子列表中删除名称,然后将这些数字连接在一个列表中,或将所有子列表放在一起,然后删除所有名称字符串。

我知道也许一些循环和使用del list [index]可能可以使用,但我失去了我如何使用它们。我尝试了一些事情并没有解决。

+1

这将是最好的让我们看到你已经尝试了什么。并向我们​​展示您的代码产生的任何错误。有时创建新列表比从原始列表中删除元素更容易。 – abccd

回答

0

尝试类似:

def fun2(): 
    fun2out = [] 
    fun1out = fun1('initial list input here') 
    for item in fun1out: 
     fun2out.append(item[1]) 
    return fun2out 

打开了该功能FUN2获取列表并将其存储返回的列表作为fun1out(FUN1输出)。 for循环访问每个列表并将该数字附加到fun2输出。

0

让我们开始导入numpy的为NP

import numpy as np 
def fun1(p:list): 
    fun1_list=[] 
    for i in p: 
     c=[] 
     temp = i.split(",") 
     c.append(temp[0]) 
     results = list(map(float,temp[1:])) 
     c.append("{:0.1f}".format(np.mean(results))) 
     fun1_list.append(c) 
    #print(fun1_list)#remove #to print the result 
    return fun1_list 
def fun2(f:fun1): 
    fun2_list=[] 
    for i in f: 
     fun2_list.append(i[1]) 
    #print(fun2_list) #remove #to print the result 
    return fun2_list 

fun_result= fun1(['Jo, 60, 92, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']) 

fun2(fun_result) 

答: FUNC1列表[['Jo', '77.3'], ['Bill', '65.0'], ['Cal', '98.0']]

FUNC2列表['77.3', '65.0', '98.0']

相关问题