开发者

I wanna convert this sorted loop to a regular loop

开发者 https://www.devze.com 2022-12-07 21:34 出处:网络
I wanna convert this to a regular for loop for i in sorted(pdict.item开发者_开发技巧s(),key=lambda x:x[1],reverse=True):

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}")
0

精彩评论

暂无评论...
验证码 换一张
取 消