打印矩阵中具有特定值的行
Print rows with certain values from a matrix
我有一个矩阵,每一行都有一辆自行车。我想打印包含某辆自行车 f.ex“Trek”的所有行。我正在努力寻找正确的代码。下面的代码给出了以下错误消息 AttributeError: 'list' object has no attribute 'values'。有人对如何解决这个问题有任何想法吗?
bikes = [["Giant", "Steel", "58 cm", "1000"], ["Trek", "Aluminium", "56 cm", "6000"],
["Trek", "Carbon", "60 cm", "8000"], ["Specialized", "Carbon", "60 cm", "10000"],
["Trek", "Aluminium", "58 cm", "1500"]]
print(bikes[bikes.values == "Trek"])
试试这个:
print([bike for bike in bikes if bike[0] == "Trek"])
我有一个矩阵,每一行都有一辆自行车。我想打印包含某辆自行车 f.ex“Trek”的所有行。我正在努力寻找正确的代码。下面的代码给出了以下错误消息 AttributeError: 'list' object has no attribute 'values'。有人对如何解决这个问题有任何想法吗?
bikes = [["Giant", "Steel", "58 cm", "1000"], ["Trek", "Aluminium", "56 cm", "6000"],
["Trek", "Carbon", "60 cm", "8000"], ["Specialized", "Carbon", "60 cm", "10000"],
["Trek", "Aluminium", "58 cm", "1500"]]
print(bikes[bikes.values == "Trek"])
试试这个:
print([bike for bike in bikes if bike[0] == "Trek"])