如何使用python计算字典中的元素?

2024-05-20 23:02:20 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的格言:

{'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移动属性!你知道吗

需要帮忙吗


Tags: commapmoveextractattributevariablemethodclass