Demo entry 6796417

Py

   

Submitted by xu on May 15, 2019 at 04:01
Language: Python. Code size: 1.5 kB.

import re
import pandas as pd
from collections import defaultdict as de
def dell(d,na):
    for i in na:
        del d[i]
    return d
    
def gps_de(d,n):
    name=[]
    for k,v in d.items():
        if v<n:
            name.append(k)
    return dell(d,name)

def gps_cr(d):
    name=[]
    for k,v in d.items():
        name.append(k)
    name_new=[i+j[-1] for i in name for j in name if i[1:]==j[:-1]]
    return name_new

def gps_crdi(name,dataset):
    r=de(int)
    for i in name:
        b=''
        for j in range(len(i)-1):
            b+=i[j]+'.*'
        b+=i[-1]
        for i in dataset['booknumber'].values:
            i_new=i.replace(',','')
            res=re.search(b,i_new)
            if not(res==None):
                r[b.replace('.*','')]+=1
    return r
    
with open('D:\\undergraduate\\class\\bigdata\\gsp.csv')as f:
    dataset=pd.read_csv(f)
    r_1=[]
    r_1_di=de(int)
    for i in dataset['booknumber'].values:
        for j in set(i.split(',')):
            r_1_di[j[0]]+=1
    di=gps_de(r_1_di,2)
    print('L1:')
    print(di)
    name=[]
    for k,v in di.items():
        name.append(k)
    name_new=[i+j for i in name for j in name]
    r_di=gps_crdi(name_new,dataset)
    di_3=gps_de(r_di,2)
    nuu=1
    while(len(di_3)!=0):
        print('L'+str(nuu+1)+':')
        print(di_3)
        name_n=gps_cr(di_3)
        r_di=gps_crdi(name_n,dataset)
        di_3=gps_de(r_di,2)
        nuu+=1

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).