如何知道列表中的哪个元素触发了any()函数?

Lui*_* U. 7 python python-2.7

我正在开发一个Python程序来检测记录列表中的城市名称.我到目前为止开发的代码如下:

aCities = ['MELBOURNE', 'SYDNEY', 'PERTH', 'DUBAI', 'LONDON']

cxTrx = db.cursor()
cxTrx.execute( 'SELECT desc FROM AccountingRecords' )

for row in cxTrx.fetchall() :
    if any( city in row[0] for city in aCities ) :
        #print the name of the city that fired the any() function
    else :
        # no city name found in the accounting record
Run Code Online (Sandbox Code Playgroud)

该代码可以很好地检测在会计记录中找到aCities列表中的城市,但是当any()函数返回True或False时我很难知道哪个城市(墨尔本,悉尼,珀斯,迪拜或伦敦) )触发退出.

我已经尝试过aCities.index和队列但到目前为止没有成功.

fal*_*tru 10

我不认为这是可能的any.您可以使用next默认值:

for row in cxTrx.fetchall() :
    city = next((city for city in aCities if city in row[0]), None)
    if city is not None:
        #print the name of the city that fired the any() function
    else :
        # no city name found in the accounting record
Run Code Online (Sandbox Code Playgroud)