我有一个这样的自行车清单:
fmnewlist = [
['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance', 'KM since Last', 'Reason/s'],
['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'No service'],
['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'No service'],
['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'KM'],
['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'No service'],
['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Batt & KM']
]
我想检查每个内部列表中的某些元素,然后在没有循环的情况下打印出这样的字符串。这是我的代码,为什么它不起作用?
lens=len(fmnewlist)
biketocheck=(input("Bike No.:"))
countertwo=1
while countertwo < (lens+1):
a=fmnewlist[countertwo]
if biketocheck in a[0] and a[5]!="No service":
print("Bike serviced.")
elif biketocheck in a[0] and a[5]=="No service":
print("Bicycle not due for servicing")
elif biketocheck not in fmnewlist:
print("No such bicycle")
else:
print(" ")
countertwo+=1
if countertwo==lens:
break
输入T123
时的预期输出:
Bike No.:T123
No such bicycle
实际输出:
Bike No.:T123
No such bicycle
No such bicycle
No such bicycle
No such bicycle
No such bicycle
输入为T102
时的预期输出:
Bicycle not due for servicing
实际输出:
Bike No.:T102
No such bicycle
Bicycle not due for servicing
No such bicycle
No such bicycle
No such bicycle
输入为T103
时的预期输出:
Bike serviced
实际输出:
Bike No.:T103
No such bicycle
No such bicycle
Bike serviced.
No such bicycle
No such bicycle
答案 0 :(得分:2)
为什么不直接创建列表的查找地图并进行快速if .. in
比较,例如:
fmnewlist = [
['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance','KM since Last', 'Reason/s'],
['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'No service'],
['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'No service'],
['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'KM'],
['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'No service'],
['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Batt & KM']
]
bike_iter = iter(fmnewlist) # create an iterator for easier handling
next(bike_iter) # skip the first item (header)
bike_nos = {bike[0]: bike[5] for bike in bike_iter} # create bike_no: reason map
biketocheck = input("Bike No.: ") # get the input
if biketocheck in bike_nos: # check if the bike exists
if bike_nos[biketocheck] == "No service": # if it exists, check for status...
print("Bicycle not due for servicing")
else:
print("Bike serviced.")
else: # bike doesn't exist...
print("No such bicycle")
如果你测试它:
Bike No.: T103 Bike serviced. Bike No.: T102 Bicycle not due for servicing Bike No.: T123 No such bicycle
答案 1 :(得分:2)
我最好猜测该计划应该做什么:
鉴于这些假设,我相信这段代码应该做你想要的:
fmnewlist = [
['Bike No.', 'Purchase Date', 'Batt %', 'Last Maintenance', 'KM since Last', 'Reason/s'],
['T101', '10/4/2016', '55', '10/1/2017', '25.08', 'No service'],
['T102', '1/7/2016', '10', '15/5/2017', '30.94', 'No service'],
['T103', '15/11/2016', '94', '13/6/2017', '83.16', 'KM'],
['T104', '25/4/2017', '58', '10/1/2017', '25.08', 'No service'],
['T105', '24/5/2017', '5', '20/6/2017', '93.8', 'Batt & KM']
]
biketocheck=(input("Bike No.:"))
found = False
for bike in fmnewlist[1:]:
if bike[0] == biketocheck:
found = True
if bike[5] == "No service":
print("Bicycle not due for servicing")
else:
print("Bike serviced.")
break
if not found:
print("No such bicycle")
修改强>
使用dict
代替,这使逻辑变得更容易:
bikes = { bike[0]:bike[1:] for bike in fmnewlist[1:] }
biketocheck=(input("Bike No.:"))
if biketocheck in bikes:
if bikes[biketocheck][4] == "No service":
print("Bicycle not due for servicing")
else:
print("Bike serviced.")
else:
print("No such bicycle")
答案 2 :(得分:0)
使用for
代替while
,删除else
部分。然后,您可以使用else
语句for
:
for bicycle in fmnewlist[1:]: # Don't use the first line
# previous statements except else, it should have a `break` too.
if biketocheck in a[0]:
if a[5]!="No service":
print("Bike serviced.")
# compare is either != or == so there's no need to write the whole elif
else:
print("Bicycle not due for servicing")
break
else:
print("No such bicycle")
else
仅在for
完成且没有中断的情况下使用。所以这意味着没有找到自行车。你正在做的是检查每一行所需的自行车,当然它不存在于那个元素中。
P.S:您也可以将else
与while
一起使用。但是你必须确定条件在一段时间之后返回False
,否则else
子句将不起作用。 for
执行此操作会更容易。