首页 > 解决方案 > 如何修复代码而不在 python 中再次计算相同的元素?

问题描述

n = int(input("Enter the number of transaction\n"))
#ask for items in all ‘n’ transactions provided by user.
List = []
List2 = []
for i in range(0, n):
    print("Enter the item at Transaction: ", i+1 )
    item = input().split(",")
    List.append(item)
#print the transaction
print("---------------------");    
print(" no | transaction");    
print("---------------------");
for i in range(0, n):
    print(i+1," ",List[i])
print("---------------------"); 
#finding frequency
def countList(List, x): 
    return sum(x in item for item in List) 
#Displays the frequency of each element present in array in order
print("---------------------");    
print(" Items  | Frequency");    
print("---------------------");  
#list should be in order  
for item in List:
    for x in item:
        print("    " + str(x) + "    |    " + str(countList(List,x))); 
    print("---------------------");  

这是代码。我不希望代码再次重复元素的频率计数。你能帮我解决这个问题吗?

这是输出:

Enter the number of transaction                                                                                                                      
5                                                                                                                                                    
Enter the item at Transaction:  1                                                                                                                    
l1,l2                                                                                                                                                
Enter the item at Transaction:  2                                                                                                                    
l1,l2,l3                                                                                                                                             
Enter the item at Transaction:  3                                                                                                                    
l1,l3                                                                                                                                                
Enter the item at Transaction:  4                                                                                                                    
l2,l4                                                                                                                                                
Enter the item at Transaction:  5                                                                                                                    
l4        
---------------------                                                                                                                                
 no | transaction                                                                                                                                    
---------------------                                                                                                                                
1   ['l1', 'l2']                                                                                                                                     
2   ['l1', 'l2', 'l3']                                                                                                                               
3   ['l1', 'l3']                                                                                                                                     
4   ['l2', 'l4']  
5   ['l4']                                                                                                                                           
---------------------                                                                                                                                
---------------------                                                                                                                                
 Items  | Frequency                                                                                                                                  
---------------------                                                                                                                                
    l1    |    3                                                                                                                                     
    l2    |    3                                                                                                                                     
    l1    |    3                                                                                                                                     
    l2    |    3                                                                                                                                     
    l3    |    2                                                                                                                                     
    l1    |    3                                                                                                                                     
    l3    |    2                                                                                                                                     
    l2    |    3                                                                                                                                     
    l4    |    2                                                                                                                                     
    l4    |    2                                                                                                                                     
--------------------- 

标签: pythonlistpython-2.7

解决方案


尝试:

n = int(input("Enter the number of transaction\n"))
#ask for items in all ‘n’ transactions provided by user.
List = []
List2 = []
for i in range(0, n):
    print("Enter the item at Transaction: ", i+1 )
    item = input().split(",")
    List.append(item)
#print the transaction
print("---------------------");    
print(" no | transaction");    
print("---------------------");
for i in range(0, n):
    print(i+1," ",List[i])
print("---------------------"); 
#finding frequency
def countList(List, x): 
    return sum(x in item for item in List) 
#Displays the frequency of each element present in array   
print("---------------------");    
print(" Items  | Frequency");    
print("---------------------");  

####### Here all the changes have been made #######

Li = ([item for sublist in List for item in sublist])
for item in set(Li):
        print("    " + str(item) + "    |    " + str(Li.count(item))); 
print("---------------------");

Enter the number of transaction
3
Enter the item at Transaction:  1
l1,l2
Enter the item at Transaction:  2
l3,l1,l4
Enter the item at Transaction:  3
l2,l3
---------------------
 no | transaction
---------------------
1   ['l1', 'l2']
2   ['l3', 'l1', 'l4']
3   ['l2', 'l3']
---------------------
---------------------
 Items  | Frequency
---------------------
    l4    |    1
    l2    |    2
    l3    |    2
    l1    |    2
---------------------

解释

我已经展平了List,然后只从中取出唯一的项目来使用count函数计算它们的值。


推荐阅读