简体   繁体   中英

Difference between [(a and b) in c] and [a in c and b in c]

Hi all I am trying to solve a problem described in the image here . My attempt of solving it is to render ah to 1-8 then to check for every piece of chess if diagonal tiles one line below are filled with another chesspiece, referred to as "pawns". I am having no problem with the code except with the usage of and in if-clauses. What is the difference between 1)if (a and b) in c and 2)if a in c and b in c? The latter is giving me the desired results but I am curious why 1 isn't working and how it is interpreted by the machine.

Also, is it possible to do an exclusive or in if-clauses in python? I have to consider both the cases of 1 pawn and 2 pawns available and thus is considering using and and or, but I realized that or is not exclusive in python. (Yes,after the and part is run, cases of 2 pawns available will be eliminated and thus an ordinary or is okay, but I am just curious about the way to implement it) In addition, I would be really grateful if you can proffer some comments on the elegancy, idiomaticness and efficiency (processing speed) of my code. I know it is kind of crude and there are so many ways to make it more succinct, but to begin with, I am not even aware of the tools (syntactic sugar and such) available and I will really appreciate it if you can advise me on how to pick up the tools systematically (such as how to give order to ah (a is to the left of b, etc.) without converting them to numbers, which have built-in order)

My code is as follows, thank you.

def safe_pawns(pawns: set) -> int:
    pawns=list(pawns)
    verifier=0
    pawns_num=[]
    for element in pawns:
        element_list=list(element)
        letter={"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8}
        element_list[0]=letter[element_list[0]]
        element_list[0]=str(element_list[0])
        element_num=element_list[0]+element_list[1]
        pawns_num.append(element_num)
        print ()
    print (pawns_num)
    for i in range(len(pawns)):
        first_item=pawns[i][0]
        second_item=pawns[i][1]
        first_item_value=int(letter[first_item])
        pawns_num_check=str(first_item_value)+second_item
        print (pawns_num_check)
        second_item_checker=str(int(second_item)-1)
        first_item_checker1=str(first_item_value-1)
        first_item_checker2=str(first_item_value+1)
        converted_pawn1=first_item_checker1+second_item_checker
        converted_pawn2=first_item_checker2+second_item_checker
        if (converted_pawn1 and converted_pawn2) in pawns_num:   #########
            verifier+=1
            print (pawns[i],converted_pawn1,converted_pawn2)
        if (converted_pawn1) in pawns_num and (converted_pawn2) in pawns_num:
            print (pawns[i],converted_pawn1,converted_pawn2)**
    return verifier
safe_pawns({"b4", "d4", "f4", "c3", "e3", "g5", "d2"})

(a and b in c) means check if a is truthy and if b in c is truthy.

You can use

a in c and b in c

or

all(x in c for x in (a, b))

which for your case would be something like

if all(x in pawns_num for x in (converted_pawn1, converted_pawn2)):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM