在列表中搜索以查看某项是否包含单词的一部分

时间:2018-09-20 19:26:56

标签: python

没有看到我的答案,所以我将解释我要做什么。我需要从URL中分解出一些数据,其中一些URL的字数限制在其后是guid。我想做的就是查看单词是否在列表中项目的任何部分。

我想遍历列表项,如果该项包含单词限制,我想获得下一个33个字符

以下是列表的示例:

  

限制=   ['limit = 5e8f0dc0-4074-481d-a12c-5d3a70406ad7&location = d877f1f9-fae3-4f39-82ca-499ec8a7a0d2',   'location = d877f1f9-fae3-4f39-82ca-499ec8a7a0d2',   'sourceLimitAllocationOid = 212a7e22-aafd-40e3-9684-621dae6708af&sourceUnits = 5000&sourceUnitOfMeasureCode = TON&tragetUnitOfMeasureOid = 1bec1009-90d3-de11-9251-001d092afdd0&targetLimitCommodityOid = 7e7a1112-b7c7-4563-9bd4-702   '',   'limit = 5e8f0dc0-4074-481d-a12c-5d3a70406ad7&location = 2e12899b-e84d-4b1a-8b89-faced0771efc',   '','location = 2e12899b-e84d-4b1a-8b89-faced0771efc',   'sourceLimitAllocationOid = bfa24508-a4e3-4138-8d29-7e9b7c1ea559&sourceUnits = 3000000&sourceUnitOfMeasureCode = BU&tragetUnitOfMeasureOid = 1bec1009-90d3-de11-9251-001d092afdd0&targetLimitCommodityOid = 7e7a1112-b7c7-4563-9   ”,“,   'limit = c0ba884c-800b-4174-8374-0bc5515f8282&location = b3d785e6-4d0c-460b-8e2c-1f32732a1a20',   'location = b3d785e6-4d0c-460b-8e2c-1f32732a1a20']

2 个答案:

答案 0 :(得分:2)

尝试一下:

import re
limits = ['limit=5e8f0dc0-4074-481d-a12c-5d3a70406ad7&location=d877f1f9-fae3-4f39-82ca-499ec8a7a0d2', 'location=d877f1f9-fae3-4f39-82ca-499ec8a7a0d2', 'sourceLimitAllocationOid=212a7e22-aafd-40e3-9684-621dae6708af&sourceUnits=5000&sourceUnitOfMeasureCode=TON&tragetUnitOfMeasureOid=1bec1009-90d3-de11-9251-001d092afdd0&targetLimitCommodityOid=7e7a1112-b7c7-4563-9bd4-702e14b139f4', '', 'limit=5e8f0dc0-4074-481d-a12c-5d3a70406ad7&location=2e12899b-e84d-4b1a-8b89-faced0771efc', '', 'location=2e12899b-e84d-4b1a-8b89-faced0771efc', 'sourceLimitAllocationOid=bfa24508-a4e3-4138-8d29-7e9b7c1ea559&sourceUnits=3000000&sourceUnitOfMeasureCode=BU&tragetUnitOfMeasureOid=1bec1009-90d3-de11-9251-001d092afdd0&targetLimitCommodityOid=7e7a1112-b7c7-4563-9bd4-702e14b139f4', '', '', 'limit=c0ba884c-800b-4174-8374-0bc5515f8282&location=b3d785e6-4d0c-460b-8e2c-1f32732a1a20', 'location=b3d785e6-4d0c-460b-8e2c-1f32732a1a20']

for el in limits:
    a = re.search(r'\b(limit)\b', el)
    if a is not None:
        ind = a.start()
        print(el[ind+5:ind+38])

答案 1 :(得分:1)

根据this tutorial,您可以执行以下操作:

offset = len("limit")
limit = ""
for item in limits:
    index = item.find("limit")
    if (index != -1):
        limit = item[ (limit + offset + 1): (limit + offset + 34) ]
        break

除了您要的内容外,还有几件事。首先,我假设其中只有一个带有limit的字符串。如果不是,请删除break语句,并使limit为列表,并将分配更改为append。其次,我假设您不想从字符串中提取=,因此实际上是在开始和结尾处添加一个。另外,如果您要这样做,则可以稍后删除第一个字符。

我希望这会有所帮助。