3 回答
TA贡献1993条经验 获得超5个赞
您的条件只是写错了 - 它正在选择第二个元素为的子列表
>= user_input + 0.6(计算结果为>= 4.0,但您希望第二个元素为between 3.4 and 4.0。所以我相信您需要做的就是像这样更改它:
for i in range(0, len(mylists)):
if user_input <= mylists[i][1] <= user_input + 0.6:
new_list.append(mylists[i])
希望对您有所帮助,编码愉快!
TA贡献1805条经验 获得超9个赞
您是否收到错误或只是意外的输出?
也许你可以试试:
new_list = list(filter(lambda x: user_input<= x[1] <= user_input + 0.6 , mylists))
TA贡献1797条经验 获得超6个赞
你可以使用这个:
import re
mylists = [[['CS105', 'ENG101', 'MATH101', 'GER'], 3.4207362518089726, 0.2808766238976195], [['CS105', 'ENG101', 'GER', 'GER'], 2.9687393162393163, 0.3408964829117446]]
user_input = 3.4
st = str(user_input) #transform input from user to string
denom = '1'+len(st.split('.')[-1])*'0' #get how much decimals st have and create denominator to the decimal part
decimal_part = 1- int(st.split('.')[-1])/int(denom) #create decimal numbers to reach upper bound
new_list = []
for i in range(0, len(mylists)):
if user_input <= mylists[i][1] <= user_input + decimal_part:
new_list.append(mylists[i])
另一种方法是:
import numpy as np
mylists = [[['CS105', 'ENG101', 'MATH101', 'GER'], 3.4207362518089726, 0.2808766238976195], [['CS105', 'ENG101', 'GER', 'GER'], 2.9687393162393163, 0.3408964829117446]]
user_input = 3.4
new_list = []
for i in range(0, len(mylists)):
if user_input <= mylists[i][1] <= np.ceil(user_input):
new_list.append(mylists[i])
添加回答
举报