lista de listas de ahorro en otra lista de la lista, pero con los cambios en pitón

Super77:

Tengo una lista como ésta:

list1= [['Sarah', 55, 7, 'x'], ['John', 24, 8, 'x']]

y quiero copiarlo en otra lista, pero manteniéndola como una matriz de esa manera:

list2= [['Sarah', 55.0, 7.0], ['John', 24.0, 8.0]]

Así que lo que quiero hacer es hacer que cada número un flotador y borrar el último elemento de cada sublista

SiegridBeens:
list1 = [['Sarah', 55, 7, 'x'], ['John', 24, 8, 'x']]

list2 = [[] for x in range(len(list1))]

x = 0

for rp in range(len(list1)):
    #len(list1[rp])-1 for not count the last element for each array 
    for tp in range(len(list1[rp])-1):
        #check the type of item string
        if "str" in str(type(list1[rp][tp])):
            list2[x].append(list1[rp][tp])
        #check the type of item int
        elif "int" in str(type(list1[rp][tp])):
            #convert the item int to float
            td = float(list1[rp][tp])
            list2[x].append(td)

    x += 1

print("List 1 :" + str(list1))
print("List 2 :" + str(list2))

#OUTPUT
#List 1 :[['Sarah', 55, 7, 'x'], ['John', 24, 8, 'x']]
#List 2 :[['Sarah', 55.0, 7.0], ['John', 24.0, 8.0]]

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=395826&siteId=1
Recomendado
Clasificación