二维列表中的混合数据
Mixed data in 2D list
我有一个二维列表,我添加了几行。每行包含一个字符串,然后是 3 个浮点数。
filmRatings = [ [ "Lion", 9.8, 7.2 , 9.5 ] ]
filmRatings.append( ["Transformers" , 3.7 , 6.8 , 5.2] )
filmRatings.append( ["Pirates of the Caribbean" , 6.1 , 4.9 , 7.3] )
filmRatings.append( ["Moana" , 8.2 , 7.9 , 6.7] )
filmRatings.append( ["War Games" , 7.3 , 8.1 , 7.7] )
print(filmRatings)
total = 0.0
for i in range(len(filmRatings[0])):
total = total + filmRatings[0][i]
print(total)
我只想将第一行 [0] 中的浮点值加在一起,但需要避免 [0][0] 中的字符串值。
本质上我想做的只是想忽略列表的字符串元素并将浮点数加起来,也许某种过滤器是可行的方法,但我不确定因为我知道有几种方法可以正如他们所说的猫。
提前致谢:)
what I want to do is simply want to ignore the string elements of the list
数组切片是这项工作的完美工具。
在你的每个列表中,第 0 个元素是一个字符串
example_list = [ "Lion", 9.8, 7.2 , 9.5 ]
那么你要做的就是对该列表进行切片
print(example_list[1:len(example_list)]) # [9.8, 7.2, 9.5]
正如您在此处看到的,此 [index1:index2]
语法选择从索引 index1 - 1
开始并在 index2 - 1
结束的列表项。所以 它从 1
开始计数,而不是 0
。
所以在你的情况下,你只需要做的就是
total = 0.0
# Here creating a new array consisting only of the numbers without the string
filmRatingsWithoutString = filmRatings[0][1:len(filmRatings[0])]
for i in range(len(filmRatingsWithoutString)):
total = total + filmRatingsWithoutString[i]
print(total)
还有一件事。
您可以简单地使用 example_list[1:]
而不是使用 example_list[1:len(example_list)]
继续到最后。
perhaps some kind of filter is the way to go
如果你想像你说的那样用过滤来做到这一点,你可以简单地使用if
语句来检查值是否是一个字符串然后继续正常。
# Only allow strings here
filmRatingsWithoutString = [
item for item in filmRatings[0] if type(item) != str
]
您可以使用列表推导过滤列表并获取所有浮点数
[i for i in filmRatings[0] if type(i) is float]
您可以像这样使用求和函数来添加它们
sum([i for i in filmRatings[0] if type(i) is float])
我有一个二维列表,我添加了几行。每行包含一个字符串,然后是 3 个浮点数。
filmRatings = [ [ "Lion", 9.8, 7.2 , 9.5 ] ]
filmRatings.append( ["Transformers" , 3.7 , 6.8 , 5.2] )
filmRatings.append( ["Pirates of the Caribbean" , 6.1 , 4.9 , 7.3] )
filmRatings.append( ["Moana" , 8.2 , 7.9 , 6.7] )
filmRatings.append( ["War Games" , 7.3 , 8.1 , 7.7] )
print(filmRatings)
total = 0.0
for i in range(len(filmRatings[0])):
total = total + filmRatings[0][i]
print(total)
我只想将第一行 [0] 中的浮点值加在一起,但需要避免 [0][0] 中的字符串值。 本质上我想做的只是想忽略列表的字符串元素并将浮点数加起来,也许某种过滤器是可行的方法,但我不确定因为我知道有几种方法可以正如他们所说的猫。
提前致谢:)
what I want to do is simply want to ignore the string elements of the list
数组切片是这项工作的完美工具。
在你的每个列表中,第 0 个元素是一个字符串
example_list = [ "Lion", 9.8, 7.2 , 9.5 ]
那么你要做的就是对该列表进行切片
print(example_list[1:len(example_list)]) # [9.8, 7.2, 9.5]
正如您在此处看到的,此 [index1:index2]
语法选择从索引 index1 - 1
开始并在 index2 - 1
结束的列表项。所以 它从 1
开始计数,而不是 0
。
所以在你的情况下,你只需要做的就是
total = 0.0
# Here creating a new array consisting only of the numbers without the string
filmRatingsWithoutString = filmRatings[0][1:len(filmRatings[0])]
for i in range(len(filmRatingsWithoutString)):
total = total + filmRatingsWithoutString[i]
print(total)
还有一件事。
您可以简单地使用 example_list[1:]
而不是使用 example_list[1:len(example_list)]
继续到最后。
perhaps some kind of filter is the way to go
如果你想像你说的那样用过滤来做到这一点,你可以简单地使用if
语句来检查值是否是一个字符串然后继续正常。
# Only allow strings here
filmRatingsWithoutString = [
item for item in filmRatings[0] if type(item) != str
]
您可以使用列表推导过滤列表并获取所有浮点数
[i for i in filmRatings[0] if type(i) is float]
您可以像这样使用求和函数来添加它们
sum([i for i in filmRatings[0] if type(i) is float])