【问题标题】:How to check if a string contains an other string如何检查一个字符串是否包含另一个字符串
【发布时间】:2021-05-06 20:59:12
【问题描述】:

这是我的文件:

#This is TEST-data
2020-09-07T00:00:03.230+02:00,ID-10,3,Lon,Man,Lon,1,1,1
2020-09-07T00:00:03.230+02:00,ID-10,3,Lon,Lon,Man,1,1
2020-09-07T00:00:03.230+02:00,ID-20,2,Lon,Lon,1,1
2020-09-07T00:00:03.230+02:00,ID-20,2,Lon,Lon,1
2020-09-07T00:00:03.230+02:00,ID-30,3,Mad,Sev,Sev,1,1,1
2020-09-07T00:00:03.230+02:00,ID-30,GGG,Mad,Sev,Mad1
2020-09-07T00:00:03.230+02:00,ID-40,GGG,Mad,Bar,1,1,1,1
2020-09-07T00:00:03.230+02:00
2020-09-07T00:00:03.230+02:00

当我运行下面的代码时,我得到一个空返回。那可能是因为我的代码似乎不知道 Man 在曼彻斯特,而 Sev 在塞维利亚。 我认为问题出在condition_1

path = r'c:\data\ELK\Desktop\test_data_countries.txt'

cities_to_filter = ['Sevilla', 'Manchester']

def filter_row(row):
    if len(row) > 2 and row[2].isdigit():
            amount_of_cities = int(row[2])        
            cities_to_check = row[3:3+amount_of_cities]    
            condition_1 =  any(city in cities_to_check for city in cities_to_filter)  
                    
            return condition_1

with open (path, 'r') as output_file:
    reader = csv.reader(output_file, delimiter = ',')
    next(reader)
    for row in reader:
        if filter_row(row):
            print(row)

这是我的预期输出:

2020-09-07T00:00:03.230+02:00,ID-10,3,Lon,Man,Lon,1,1,1
2020-09-07T00:00:03.230+02:00,ID-10,3,Lon,Lon,Man,1,1
2020-09-07T00:00:03.230+02:00,ID-30,3,Mad,Sev,Sev,1,1,1

【问题讨论】:

  • 您的代码正在检查“Manchester”是否在“Man”中,而应该是相反的。
  • 它总是会是名称中的前 3 个字符,例如 Man 代表曼彻斯特,还是有可能出现 Mhr 或 Mcr 代表曼彻斯特。
  • @SandeepKumar 可能会有所不同。并不总是前 3 个。
  • @TangerCity 那么我可能会做什么,因为这些是以逗号分隔的值将它们加载到熊猫数据框中并检查不同的可能性,并可能保留一个字典(一种地图 Man -> Manchester)。可能还有其他方法可以提高效率。

标签: python list if-statement filter


【解决方案1】:

你可以用 .split() 函数分割每一行,这个函数根据你给的参数分割字符串,如果你不给参数,它会将字符串分隔为空格。然后它将返回一个列表,因此您应该将其分配给一个列表。然后控制是“man”在列表中还是“sev”在列表中。

for line in file:   
   myList=line.split(",")
   if "man" in myList or "sev" in myList:
       #blabla

【讨论】:

  • @Barmar 好点。看起来我脑子里放了个屁:)
【解决方案2】:

问题是您试图将字符串ManManchester 匹配。

您可以使用以下内容仅匹配前三个字符:

import csv
path = 'Pdata.txt'

cities_to_filter = ['Sevilla', 'Manchester']

def filter_row(row):
    if len(row) > 2 and row[2].isdigit():
            amount_of_cities = int(row[2])        
            cities_to_check = row[3:3+amount_of_cities]
            #print(cities_to_check)
            condition_1 =  any(city[:3] in cities_to_check for city in cities_to_filter)  
            
            return condition_1

with open (path, 'r') as output_file:
    reader = csv.reader(output_file, delimiter = ',')
    next(reader)
    for row in reader:
        if filter_row(row):
            print(row)

要匹配任何字符,而不仅仅是前三个,您可以使用列表推导,同时使用两个列表来查找任何匹配的行。

因此,如果您将cities_to_check 设置为["Man", "vil"],则匹配将同时包含['Sevilla', 'Manchester'],然后您可以使用len(matching) != 0 作为返回条件来获得所需的结果。

cities_to_filter = ['Sevilla', 'Manchester']    
cities_to_check = ["Man", "vil"]
matching = [city2 for city2 in cities_to_filter if any(city1 in city2 for city1 in cities_to_check)]

【讨论】:

  • 如果它不总是前 3 个但它可能是任何东西怎么办?
  • 我已更新代码以处理任何位置的子字符串匹配。
猜你喜欢
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
  • 2014-04-14
相关资源
最近更新 更多