Python - match a word in a string with a list of strings

后端 未结 6 2151
北恋
北恋 2021-01-25 22:20

I\'m new to python and I was wondering how string comparison is done

Let\'s say I have a list of strings containing state names like

states = [\"New York         


        
6条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-25 22:32

    states = ["New York", "California", "Nebraska", "Idaho"]
    postal_addr = "1234 1st E St San Jose California 95112"
    
    result = None
    for state in states:
        if state in postal_addr:
            result = state
    
    print(result)
    

    Unfortunately, this will also match words that contain a state name such as Idahoba.

提交回复
热议问题