在Python 2.7中,如何检查字符串是否包含前面有任何数字的指定字符

在Python 2.7中,如何检查字符串是否包含前面有任何数字的指定字符,python,Python,我有一个关于我的代码的问题(如下)。我试图创建一个函数来检查字符串是否包含一个gram单位。代码循环检查每个字符串前面有一个数字的g。如果它找到这个,它将向后循环字符串的索引,直到索引不是数字。这段代码对我很有用,但我想知道如何改进这段代码,以及是否有更好的方法 lines = ["100gr", "pack of 100 gram", "100 g", "great pack of 15 gram", "1 pack of 8", "3 oz = 85 g", "100",] for lin

我有一个关于我的代码的问题(如下)。我试图创建一个函数来检查字符串是否包含一个gram单位。代码循环检查每个字符串前面有一个数字的g。如果它找到这个,它将向后循环字符串的索引,直到索引不是数字。这段代码对我很有用,但我想知道如何改进这段代码,以及是否有更好的方法

lines = ["100gr", "pack of 100 gram", "100 g", "great pack of 15 gram", "1 pack of 8", "3 oz = 85 g", "100",]

for lineIndex, line in enumerate(lines): # loop through lines
    line = line.lower() # make line lowercase
    line = "".join(line.split()) # delete whitespaces

    check = False

    for charIndex, char in enumerate(line): # loop through every index from string
        if char == "g" and line[charIndex-1].isdigit() == True: #check for a g with in front a digit
            index = charIndex - 1
            myList = []

            #loop until the index in front is not a digit 
            while True:
                if line[index].isdigit() == True: 
                    myList.append(line[index])
                    index -= 1
                else:
                    break

            # make a string from the list and print it
            myList.reverse()
            result = ""
            for letter in myList:   
                result += letter
            result += "g"
            print("Line: %s\nBecomes: %s" % (line, result))

            # make check False
            check = True


    if check is False:
        print("Line: %s\nDoes not contain grams" % (line))
代码结果:


使用regexp怎么样

import re

lines = ["100gr", "pack of 100 gram", "100 g", "great pack of 15 gram", "1 pack of 8", "3 oz = 85 g", "100"]

pattern = re.compile(r"\d+\s*g")

for line in lines:
    search = pattern.search(line)
    if search:
        print search.group()
返回:

100g
100 g
100 g
15 g
85 g
[('100gr', '100g'),
('pack of 100 gram', '100g'),
('100 g', '100g'),
('great pack of 15 gram', '15g'),
('1 pack of 8', 'No gram unit'),
('3 oz = 85 g', '85g'),
('3 oz = 85 g and 200 g', ['85g', '200g']),
('100', 'No gram unit')]

使用正则表达式,搜索至少一个数字,后跟可能的空格,再加上字母“g”,可以大大降低这一点:

结果是:

100g
100 g
100 g
15 g
Not found.
85 g
Not found.
简洁的表达(来自译员):


试试这一行:

map(lambda x: (x, map(lambda x: x+'g', re.findall('(\d+)\s*(?=g|gr|gm|gram)', x))) if re.search('(\d+)\s*(?=g|gr|gm|gram)', x) else (x, 'No gram unit'), lines)
这将返回元组列表-
(,[,…])

返回:

100g
100 g
100 g
15 g
85 g
[('100gr', '100g'),
('pack of 100 gram', '100g'),
('100 g', '100g'),
('great pack of 15 gram', '15g'),
('1 pack of 8', 'No gram unit'),
('3 oz = 85 g', '85g'),
('3 oz = 85 g and 200 g', ['85g', '200g']),
('100', 'No gram unit')]

你能直接把结果写在这里而不是链接到图片上吗?该结果是否来自此代码?您可以通过在末尾添加一个单词边界来进一步改进此结果-目前这将匹配,例如
'200 green beans'
。怎么样
(\d+)\s*g(?:r | ram)?s*\b
不用担心,+1是最灵活的答案。
[('100gr', '100g'),
('pack of 100 gram', '100g'),
('100 g', '100g'),
('great pack of 15 gram', '15g'),
('1 pack of 8', 'No gram unit'),
('3 oz = 85 g', '85g'),
('3 oz = 85 g and 200 g', ['85g', '200g']),
('100', 'No gram unit')]