I wanna convert this to a regular for loop
for i in sorted(pdict.item开发者_开发技巧s(),key=lambda x:x[1],reverse=True):
if(i[1]<rating):
print("Jersey number: "+str(i[0])+", Rating: "+str(i[1]))
you mean like this?
for i in pdict.items():
if(i[1]<rating):
print("Jersey number: "+str(i[0])+", Rating: "+str(i[1]))
convert this to a regular for loop
Ok, here you go.
for i in pdict.items():
if i[1] < rating:
print("Jersey number: " + str(i[0]) + ", Rating: " + str(i[1]))
But perhaps you wanted to clarify your question?
You can also make i
more explicitly defined by specifying that two values are coming from .items()
: the dictionary key and the dictionary value. The syntax below makes that more clear.
for key, value in pdict.items():
if value < rating:
print(f"Jersey number: {key}, Rating: {value}")
精彩评论