python - 如何修复代码而不在 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
---------------------
解决方案
尝试:
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
函数计算它们的值。
推荐阅读
- ansible - ansible/json_query 加入列出 2 个键
- javascript - 使用 JSXGraph 时如何删除创建的点?
- python - CUMSUM 加法如下
- python - 从中提取 data-keyword=
- swift - 如何为 SCNPhysicsBody 的某些属性添加观察者?
- r - 如何合并具有相同列和行中一些相同数据的多张excel表
- android - 如何使用 Kotlin 在片段内播放 YouTube 视频
- java - 构造函数中的对象创建链接 wrt 继承
- apache-kafka - 即使 Kafka 连接断开,应用程序也应该继续运行
- javascript - 我想验证用户的伪在他写作时是否良好