这是我的字典:
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Attribute']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
键是类的名称,值是重构类型,在这个示例中,我只有一个类,它包含1个Move属性和7个Move方法。
我使用此代码来计算每个类中的重构类型的数量
import csv
import pandas as pd
df=pd.read_csv('output2.csv', sep=',')
df1 = df[['RefactoringType','RefactoringDetail']]
df1['Detail']=df.RefactoringDetail.str.extract(r'[C|c]lass\s*([^ ]*)')
del df1['RefactoringDetail']
#this part generate the map
my_map={}
for i in range(df1.shape[0]):
my_map[df1['Detail'][i]] = []
my_map[df1['Detail'][i]].append(df1['RefactoringType'][i])
print(my_map)
#define all refactoring operations
all_refactorings=['Extract Superclass','Move Attribute','Extract Method','Inline Method','Rename Method','Move Method','Rename Package','Move Attribute','Pull Up Method','Pull Up Attribute','Push Down Method','Push Down Attribute','Move Class','Rename Class','Move And Rename Class','Extract And Move Method','Move Source Folder','Change Package','Extract Variable','Rename Attribute','Move and Rename Attribute','Replace Variable with Attribute','Replace Attribute ','Merge Variable','Merge Parameter','Merge Attribute','split Variable','split Parameter','Split Attribute','Extract Interface']
newdf = pd.DataFrame(columns= ['Class'] + all_refactorings )
for class_name in my_map :
new_row = {ref:0 for ref in all_refactorings}
new_row['Class'] = class_name
for ref_type in my_map[class_name] :
new_row[ref_type] += 1
newdf=newdf.append(new_row, ignore_index=True)
newdf
它为我回报:
Class Extract Superclass Move Attribute Extract Method Inline Method Rename Method Move Method Rename Package Move Attribute Pull Up Method Pull Up Attribute Push Down Method Push Down Attribute Move Class Rename Class Move And Rename Class Extract And Move Method Move Source Folder Change Package Extract Variable Rename Attribute Move and Rename Attribute Replace Variable with Attribute Replace Attribute Merge Variable Merge Parameter Merge Attribute split Variable split Parameter Split Attribute Extract Interface
com.sunlightlabs.android.congress.fragments.Le... 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
我不知道错误在哪里它应该给我7个移动方法和1个移动属性!
请帮助