【发布时间】: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