Python在字符串列表中找到最常见的模式

时间:2015-11-19 02:58:03

标签: python regex string pattern-matching

我有一大堆API调用存储为字符串,这些调用已被删除所有常用语法(' htttp://',' .com',&#39 ;。'等等。)

我想返回最常见图案的字典,其长度大于> 3,其中键是找到的模式,值是每个模式的出现次数。我试过这个:

calls = ['admobapioauthcert', 'admobapinewsession', 'admobendusercampaign']

>>> from itertools import takewhile, izip
>>> ''.join(c[0] for c in takewhile(lambda x: all(x[0] == y for y in x), izip(*calls)))

返回:

'admob'

我希望它返回:

{'obap': 2, 'dmob': 3, 'admo': 3, 'admobap': 2, 'bap': 2, 'dmobap': 2, 'admobapi': 2, 'moba': 2, 'bapi': 2, 'dmo': 3, 'obapi': 2, 'mobapi': 2, 'admob': 3, 'api': 2, 'dmobapi': 2, 'dmoba': 2, 'mobap': 2, 'mob': 3, 'adm': 3, 'admoba': 2, 'oba': 2}

- 我当前的方法只适用于识别前缀,但是我需要它来操作所有字符,无论它在字符串中的位置如何,我想再次将每个模式的出现次数存储为dict值。 (我已尝试过其他方法来实现这一目标,但它们非常难看)。

2 个答案:

答案 0 :(得分:2)

这就是你想要的。它在分割点后给出了常见的字符串模式。

calls = ['admob.api.oauthcert', 'admob.api.newsession', 'admob.endusercampaign']
from collections import Counter
Counter(reduce(lambda x,y: x+y,map (lambda x : x.split("."),calls))).most_common(2)

O / P:[('admob', 3), ('api', 2)]

filter(lambda x: x[1]>1 ,Counter(reduce(lambda x,y: x+y,map (lambda x : x.split("."),calls))).most_common())

更新: 我不知道这是否适合你:

calls = ['admobapioauthcert', 'admobapinewsession', 'admobendusercamp']
filter(lambda x : x[1]>1 and len(x[0])>2,Counter(reduce(lambda x,y:x + y,reduce(lambda x,y: x+y, map(lambda z :map(lambda x : map(lambda g: z[g:x+1],range(len(z[:x+1]))),range(len(z))),calls)))).most_common())

O / P:

[('admo', 3), ('admob', 3), ('adm', 3), ('mob', 3), ('dmob', 3), ('dmo', 3), ('bapi', 2), ('dmobapi', 2), ('dmoba', 2), ('api', 2), ('obapi', 2), ('admobap', 2), ('admoba', 2), ('mobap', 2), ('dmobap', 2), ('bap', 2), ('mobapi', 2), ('moba', 2), ('obap', 2), ('oba', 2), ('admobapi', \
2)]

答案 1 :(得分:1)

使用UIView,然后按点分割使用dict comprehension -

 #import "MainViewController.h"


@interface MainViewController (){

    UIView * MainView;
    UIView * SubView;
}

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    MainView = [[UIView alloc] init];
    MainView.backgroundColor = [UIColor orangeColor];
    MainView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:MainView];

    [self AppyingAutolayoutsAtPortraint];
}

-(void)AppyingAutolayoutsAtPortraint{

    NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(MainView);

    //Appliying Autolayouts for MainView

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[MainView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[MainView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

}

-(void)AppyingAutolayoutsLandAtScpae{

    NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(MainView);

    //Appliying Autolayouts for MainView

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-0-[MainView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-0-[MainView]-0-|"]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:HeaderDictionary]];

}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
        NSLog(@"portrait");

        [self.view removeConstraints:self.view.constraints];
        [self AppyingAutolayoutsAtPortraint];
        [self.view setNeedsDisplay];
    }

    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        NSLog(@"LandscapeRight");

        [self.view removeConstraints:self.view.constraints];
        [self AppyingAutolayoutsLandAtScpae];
        [self.view setNeedsDisplay];
    }

    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        NSLog(@"LandscapeLeft");

        [self.view removeConstraints:self.view.constraints];
        [self AppyingAutolayoutsLandAtScpae];
        [self.view setNeedsDisplay];
    }
}

@end

或者

Collections.Counter

或者

>>>from collections import Counter
>>>calls = ['admob.api.oauthcert', 'admob.api.newsession', 'admob.endusercampaign']
>>>l = '.'.join(calls).split(".")
>>>d = Counter(l)
>>>{k:v for k,v in d.most_common(3) }
>>>{'admob': 3, 'api': 2}
>>>{k:v for k,v in d.most_common(4) }
>>>{'admob': 3, 'api': 2, 'newsession': 1, 'oauthcert': 1}