我遇到了这个功能的问题。 有人可以帮我解决一下吗?
def string_list(L):
'''(list of str) -> list of list
Given a list of strings where each string has the format:
'name, grade, grade, grade, ...' return a new list of
lists where each inner list has the format :
[name (str), grade, grade, grade, ...] where the name
is a string and the grades are floats.
>>> string_list(['Anna, 50, 90, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98'])
[['Anna', 50.0, 90.0, 80.0], ['Bill', 60.0, 70.0], ['Cal', 98.5, 100.0, 95.5, 98.0]]
'''
new_string_list = []
for grades in L:
if grades.isnumeric():
grades = float(grades)
new_string_list.append([grades])
for name in L:
if name.isalpha():
new_string_list.append([name])
return new_string_list.append([name, grades])
答案 0 :(得分:2)
你走了:
students = ['Anna, 50, 90, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
result = []
for item in students:
parts = item.split(', ')
item_result = parts[:1] + [float(y) for y in parts[1:]]
result.append(item_result)
print(result)
# [['Anna', 50.0, 90.0, 80.0], ['Bill', 60.0, 70.0], ['Cal', 98.5, 100.0, 95.5, 98.0]]
答案 1 :(得分:0)
int
输出:
stringList = ['Anna, 50, 90, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
[ [j[0]]+[float(k) for k in j[1:]] for j in [i.split(', ') for i in stringList] ]
答案 2 :(得分:0)
只是列表理解:
>>> a
['Anna, 50, 90, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
>>> r=[[float(j) if j.replace('.', '').isdigit() else j for j in i.split(", ")] for i in a]
>>> r
[['Anna', 50.0, 90.0, 80.0], ['Bill', 60.0, 70.0], ['Cal', 98.5, 100.0, 95.5, 98.0]]
如果检查索引:
>>> a=['Anna, 50, 90, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
>>> r=[[float(j) if i.split(", ").index(j)!=0 else j for j in i.split(", ")] for i in a]
>>> r
[['Anna', 50.0, 90.0, 80.0], ['Bill', 60.0, 70.0], ['Cal', 98.5, 100.0, 95.5, 98.0]]
答案 3 :(得分:0)
您可以尝试这样的事情:
双线解决方案:
list_1=['Anna, 50, 90, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
split_it=[str(i).replace(",","").split() for i in list_1]
print([i[:1]+[float(j) for j in i[1:]] for i in split_it])
输出:
[['Anna', 50.0, 90.0, 80.0], ['Bill', 60.0, 70.0], ['Cal', 98.5, 100.0, 95.5, 98.0]]
详细解决方案:
第一步拆分你的字符串,以便我们可以迭代每个元素:
split_it=[] #storing new splited result here
for i in list_1:
split_it.append(str(i).replace(",","").split())
print(split_it)
[['Anna', '50', '90', '80'], ['Bill', '60', '70'], ['Cal', '98.5', '100', '95.5', '98']]
现在,下一步是遍历每个子列表并保留自其str之后的第一个元素,并将其余部分转换为float:
我建议你在使用列表理解之前应该知道列表理解是如何工作的:
list=[i for i in a]
与:
相同
list=[]
for i in a:
list.append(i)
现在回到解决方案:
float_list=[]
for i in split_it:
sub_list=[]
for j in i[1:]:
sub_list.append(float(j))
float_list.append(i[:1]+sub_list)
print(float_list)
所有组合:
list_1=['Anna, 50, 90, 80', 'Bill, 60, 70', 'Cal, 98.5, 100, 95.5, 98']
split_it=[] #storing new splited result here
for i in list_1:
split_it.append(str(i).replace(",","").split())
float_list=[]
for i in split_it:
sub_list=[]
for j in i[1:]:
sub_list.append(float(j))
float_list.append(i[:1]+sub_list)
print(float_list)
输出继电器:
[['Anna', 50.0, 90.0, 80.0], ['Bill', 60.0, 70.0], ['Cal', 98.5, 100.0, 95.5, 98.0]]