Search strings in list containing specific letters in random order

后端 未结 5 1232
刺人心
刺人心 2020-12-05 22:23

I am writing a code in Python 2.7 in which I have defined a list of strings. I then want to search this list\'s elements for a set of letters. These letters must be in rando

5条回答
  •  臣服心动
    2020-12-05 23:05

    It would be easy using set():

    wordlist = ['mississippi','miss','lake','que']
    
    letters = set('aqk')
    
    for word in wordlist:
        if letters & set(word):
            print word
    

    Output:

    lake
    que
    

    Note: The & operator does an intersection between the two sets.

提交回复
热议问题