简体   繁体   English

Python正则表达式搜索所有匹配的字符串

[英]Python regex search for all matching strings

I have an issue that might be simpler than I'm making it. 我有一个问题可能比我做的要简单。

   # /usr/bin/env python
   import csv
   import re
   import math

   gt_user = raw_input("Enter person you want to look for")
   for list, nums in info:
     if gt_usr == list:
        re.search(r'/.$/', get_usr)
        total += numbs

I need to find all strings matching user input names ex: User inputs EXAMPLE but in the csv data set there are EXAMPLE-2455 I need to look for all of them and and combine them together into the same data set since they are from the same place. 我需要找到所有与用户输入名称匹配的字符串,例如:用户输入Example,但是在csv数据集中有EXAMPLE-2455,我需要查找所有这些字符串,并将它们组合成相同的数据集,因为它们来自同一地点。 ex: customer and customer-2334 are the same person. 例如:customer和customer-2334是同一个人。

I'm assuming you've omitted the parsing of the CSV into info , which represents a list of rows, where each row contains a pair of items: a name and some number you want to total. 我假设您已经省略了将CSV解析成infoinfo代表一个行列表,其中每一行包含一对项目:一个名称和一些您想要总计的数字。 It sounds like you want something like the following: 听起来您想要以下内容:

total = 0
gt_user = raw_input("Enter person you want to look for")
for name, num in info:
    if gt_user in name:
        total += num

Or more simply: 或更简单地说:

gt_user = raw_input("Enter person you want to look for")
total = sum(num for name, num in info if gt_user in name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM