在python中的元组列表中查找元素

时间:2014-12-17 11:08:34

标签: python

我有以下元组列表。

[('rel', 'dns-prefetch'), ('href', 'http://g-ecx.images-amazon.com')]
[('rel', 'dns-prefetch'), ('href', 'http://z-ecx.images-amazon.com')]   
[('rel', 'dns-prefetch'), ('href', 'http://ecx.images-amazon.com')]
[('rel', 'dns-prefetch'), ('href', 'http://completion.amazon.com')]
[('rel', 'dns-prefetch'), ('href', 'http://fls-na.amazon.com')]
[('rel', 'stylesheet'), ('href', 'http://z-ecx.images-  amazon.com/images/G/01/AUIClients/NavAuiAssets-bc93e610a616dd196eda0cc0238d74dd3f830199.min._V2_.css')]
[('rel', 'stylesheet'), ('href', 'http://z-ecx.images-amazon.com/images/G/01/AUIClients/AmazonUI-c0d0f2a115191b947c48fd97b453a95d32cbadc0.rendering_engine-not-trident.weblab-AUI_CSS_REDUCTION_28708-T1.weblab-AUI_HIGH_CONTRAST_40800-T1.min._V2_.css')]
[('rel', 'stylesheet'), ('href', 'http://z-ecx.images-amazon.com/images/G/01/AUIClients/AmazonGatewayAuiAssets-0320163872407ddbb58566fd72a492fca3b17ed4.min._V2_.css')]
[('rel', 'canonical'), ('href', 'http://www.amazon.com/')]

我需要在那些中找到一个元素。我在做。

 for attr in attrs:
    #Need a code here to find an element without iterating over attar.

有没有办法在Python中做到这一点。

1 个答案:

答案 0 :(得分:0)

您可以使用in来测试会员资格:

>>> for attr in attrs:
...   if ('href', 'http://fls-na.amazon.com') in attr:
...     print attr
...
[('rel', 'dns-prefetch'), ('href', 'http://fls-na.amazon.com')]