简体   繁体   English

Python 程序接受一个List,并提取所有频率大于K的元素?

[英]Python program to accept a List,and extract all elements whose frequency is greater than K?

Python - How can I write a python program to accept a List, and extract all elements whose frequency is greater than K? Python - 我如何编写一个 python 程序来接受一个列表,并提取所有频率大于 K 的元素? Please advise?请指教?

Sample I/O:示例 I/O:

Input List:输入列表:

[4, 6, 4, 3, 3, 4, 3, 4, 6, 6]

Input K:输入 K:

2

Required Output : [4, 3, 6]需要 Output : [4, 3, 6]

Use a list comprehension:使用列表推导:

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]

Use counter in python在 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.

相关问题 提取所有大于 'm' 且小于 'n' 的列表元素 - Extract all the list elements that are greater than 'm' and less than 'n' 使用Python代码的薪水高于经理的员工列表? - List of employee whose salary is greater than manager using Python code? 返回 Python 中列表中每个子列表的大于 k 的数字索引 - Return index of numbers greater than k for each of sublists in list in Python python-检查list1中的所有元素是否大于list2中相同索引的元素 - python-Check if all elements in list1 are greater than eliments of the same index in list2 如何获得频率大于的单词列表? - How to get a list of words with frequency greater than? Python 列表累积总和大于列表中元素的 rest - Python list cumulative sum greater than rest of elements in list 查找列表中大于当前元素的所有元素 - Finding all elements greater than current element in a list Python:在numpy数组中向左和向右移动大于0的所有元素 - Python: moving all elements greater than 0 to left and right in numpy array "替换大于某个值的 Python NumPy 数组的所有元素" - Replace all elements of Python NumPy Array that are greater than some value 如何使用 Python 过滤 Pandas 数据帧中所有或部分行值大于 0 的列? - How to filter columns whose all or some rows values are greater than 0 in Pandas data-frame using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM