Python 3.x 如何搜索单词中的特定字符串
我有:Python 3.x 如何搜索单词中的特定字符串,python-3.x,Python 3.x,我有: 包含一定数量代码的列表 (这是整个列表的一部分) 2.这根绳子 string1 = "A1Contributo pubblico1559.020• 559.020,00A2.2Cofinanziamentoprivato in denaro122.500• 22.500,00A2.4Entrate generate dalprogetto00• 0,00B2.20aLocali: locazioni eutenze00• 0,00B2.20bImmobili:ammortamenti00
string1 = "A1Contributo pubblico1559.020• 559.020,00A2.2Cofinanziamentoprivato in denaro122.500• 22.500,00A2.4Entrate generate dalprogetto00• 0,00B2.20aLocali: locazioni eutenze00• 0,00B2.20bImmobili:ammortamenti00• 0,00B2.20cImmobili:manutenzioneordinaria00• 0,00B2.21Attrezzature: noleggi eleasing00• 0,00B2.22Attrezzature:manutenzioni ordinarie00• 0,00B2.23Attrezzature:ammortamenti00• 0,00B2.1Docenza (dipendenti ecollaboratori)00• 0,00B2.14Viaggi di studio deipartecipanti00• 0,00B2.18Materiali diconsumo/materialididattici00• 0,00E1.1UCS ora formazione5.94085• 504.900,00E1.2UCS allievo120403,5• 48.420,00E1.3Costi acofinanziamentoprivato150150• 22.500,00E1.4UCS ora/utente(individuale)15038• 5.700,00"
我想验证list1中的元素是否包含在string1中,以及它们在字符串中的位置
我的最终目标是从字符串中提取每个代码的相对数量,例如代码“A1”->559.020,00,代码“A2.2”->22.500,00等等
起初,我尝试了一个简单的方法:
for code in list1:
stringPosition = re.search(code, string1)
但是在这个解决方案中,像B2.2、B2.20这样的代码有一个问题,因为我找到了相同的位置
所以我试图理解如何在字符串中搜索精确的代码。我看到以下帖子:
我尝试应用建议的解决方案(例如使用r'\w'+(代码)+r'\w'),但没有成功
我的第一次尝试:
for code in list1:
stringPosition = re.search(code, string1)
if stringPosition != None:
print(code, stringPosition)
list2.append(stringPosition)
提前感谢您的建议您可以使用正确的正则表达式:
import re
MAGIC_REGEX = "([A-Z]\d+(?:.\d+[a-z]?)?)[^\•]+\• ([\d\.,]+)"
matches = re.findall(MAGIC_REGEX, string1)
print(matches)
filtered = list(filter(lambda x: x[0] in list1, matches))
print(filtered)
number_filtered = list(map(lambda x: (x[0], float(x[1].replace(".", "").replace(",", "."))), filtered))
print(number_filtered)
因为我不确定你到底想要什么,所以我明确划分了三个步骤:
- 在匹配正则表达式之后,你得到了你想要的东西 (2.4、0、00’),(2.4、0、00’),(2.4、0、00’,('2.4、0、00’,('B2.20a、0、00’,('B2.20b、0、00’,('B2.20b、0、00’,('1.1”、“559.1”、“559.9.0.0 0.0 0.0 0 0 0 0 0万万万万万万万万万万万万万万万万万万万万万万、0,,,,,,,,(([[[[[3.3.3.3.3.3.3.3.3.2.2.2.2.2.2.2.2.3”、“22”、“22”、“22”、“22”、“22”、“22”、“22”、“22”、“22”、“22”、“22.2.2.2.2.2.2.2.2.2.2.2.2.2”、“22”、“22”、“22”、“22”、“22[1,00')]
- 过滤是指仅获取列表中的代码(注意,差异是B2.21-23和所有E代码,因为它们不在列表中) 过滤=[('A1','559.020,00'),('A2.2','22.500,00'),('A2.4','0,00'),('B2.20a','0,00'),('B2.20b','0,00'),('B2.20c','0,00'),('B2.1','0,00'),('B2.14','0,00'),('B2.18','0,00')]
- 如果将值转换为float,则需要删除第一个点并将逗号更改为点 过滤后的数字=[('A1',559020.0),('A2.2',22500.0),('A2.4',0.0),('B2.20a',0.0),('B2.20b',0.0),('B2.20c',0.0),('B2.1',0.0),('B2.14',0.0),('B2.18',0.0)]
string1.find(item)
也是你的选择吗?你为什么需要这个职位?你可能只需要获得具有值的代码对吗?@palvarez因为我是python新手,所以我刚刚尝试找到一个解决方案,但你的答案对我来说是完美的,我去研究你的解决方案:-)
import re
MAGIC_REGEX = "([A-Z]\d+(?:.\d+[a-z]?)?)[^\•]+\• ([\d\.,]+)"
matches = re.findall(MAGIC_REGEX, string1)
print(matches)
filtered = list(filter(lambda x: x[0] in list1, matches))
print(filtered)
number_filtered = list(map(lambda x: (x[0], float(x[1].replace(".", "").replace(",", "."))), filtered))
print(number_filtered)