[英]Extract all the list elements that are greater than 'm' and less than 'n'
[英]Python program to accept a List,and extract all elements whose frequency is greater than K?
Python - 我如何编写一个 python 程序来接受一个列表,并提取所有频率大于 K 的元素? 请指教?
示例 I/O:
输入列表:
[4, 6, 4, 3, 3, 4, 3, 4, 6, 6]
输入 K:
2
需要 Output : [4, 3, 6]
使用列表推导:
a = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6]
k = 2
out = [i for i in set(a) if a.count(i) > k]
在 python 中使用计数器
list1 = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6]
d = Counter(list1)
new_list = list([item for item in d if d[item] > 1])
print(new_list) #output: [4, 6, 3]
inputs = 0
user_list = []
while inputs < 8:
user_input = int(input("Enter a number: "))
user_list.append(user_input)
inputs += 1
input_list = [4, 6, 4, 3, 3, 4, 6, 6]
input_k = 2
def extract(x, y):
product = []
for i in x:
list_element = i
if list_element > y:
product.append(i)
else:
break
product = list(set(product))
return product
print(extract(user_list, input_k))
print(extract(input_list, input_k))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.