【问题标题】:how do i fix the code without counting the same element again in python?如何修复代码而不在 python 中再次计算相同的元素?
【发布时间】:2021-03-17 07:38:24
【问题描述】:
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                                                                                                                                     
--------------------- 

【问题讨论】:

  • 请以文本形式发布您的代码
  • 请将您的代码作为文本发布并编辑您的问题。
  • @Pygirl 我已经发布了代码
  • 我尝试了集合(列表)中的项目:它有效
  • 将输出作为文本发布,文本截图会适得其反,盲人无法访问

标签: python list python-2.7


【解决方案1】:

试试:

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 函数计算其值。

【讨论】:

  • 非常感谢!! @Pygirl。我被那个代码震惊了很长时间。
  • 我想对代码进行另一次更改。如何按顺序输出最后一个表。 (l1,l2,l3,l4)
  • @user15413198: for item in sorted(set(Li)): 使用sorted
猜你喜欢
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 2015-08-19
相关资源
最近更新 更多