Super77 :
i have a list like this one :
list1= [['Sarah', 55, 7, 'x'], ['John', 24, 8, 'x']]
and i want to copy it to another list but keeping it as a matrix like that :
list2= [['Sarah', 55.0, 7.0], ['John', 24.0, 8.0]]
so what i want to do is making every number a float and deleting the last element of every sub-list
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]]
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=395316&siteId=1