简体   繁体   English

如果列表中有整数 k 和 n,则返回 True 的函数?

[英]Function to return True if integer k and n in list?

Very new to python... If I give a list that has two integers n and k, the function should return Boolean True.对 python 非常陌生...如果我给出一个包含两个整数 n 和 k 的列表,该函数应该返回布尔值 True。 K has to be in the list the number of n times. K 必须在列表中出现 n 次。 For example, if I call matchingValues([1, 2, 1, 4, 1, 1], 1, 4) should return True since 1 is in the list 4 times.例如,如果我调用 matchingValues([1, 2, 1, 4, 1, 1], 1, 4) 应该返回 True,因为 1 在列表中出现了 4 次。 I'm guessing I have to use the .count() option but not sure how to write it... Wish I could give more code but unfortunately I am very lost我猜我必须使用 .count() 选项但不知道如何编写它...希望我能提供更多代码但不幸的是我很迷茫

    def matchingValues(aList,n,k):

Better than a solution involving len() is to use the .count() method for lists.比涉及len()的解决方案更好的是对列表使用.count()方法。 It takes an argument, and returns the number of times that argument appears in the list that it is called on.它接受一个参数,并返回该参数出现在调用它的列表中的次数。

What you want to do is define a variable which counts how many times n is in the list你想要做的是定义一个变量来计算 n 在列表中的次数

found = 0

and then, iterate over your list with a loop然后,循环遍历您的列表

for x in range(0, len(aList)):
    if aList[x] == n:
        found += 1
return(found == k)

This is, however, if you want to avoid using the count function:但是,如果您想避免使用count函数,请执行以下操作:

return(aList.count(n)==k)

Using the manual, hand-written loop will allow you to exit the loop as soon as you know there are k n s in the list, if you want the loop to work like that (however, I have not provided that code. I have only written exactly what count does)使用说明书,手写循环将让你退出循环,只要你知道有k n S IN列表中,如果你希望循环这样的工作(但是,我还没有提供的代码。我有只写了count作用)

If you want there to be AT LEAST k n s in aList , this would work for you如果您希望aList至少有k n s,这对您aList

found = 0
for x in range(0, len(aList)):
    if aList[x] == n:
        found += 1
        if found == k:
            return True
return False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 函数powers(n,k)返回列表[1,n,n ^ 2,n ^ 3,…,n ^ k],其中k是整数 - A function powers(n,k) that returns the list [1,n,n^2,n^3,…,n^k] where k is an integer 如果可以通过在列表中放置加减运算获得目标整数,则应返回true的写函数 - Write function that should return true if the target integer can be obtained by placing addition or subtraction operations in the list 在Python中递归生成n个选择k组合的列表-但返回列表 - Recursively Generating a List of n choose k combinations in Python - BUT return a list 给定一个 integer n,如果 n 在 100 或 200 的 10 以内,则返回 True - Given an integer n, return True if n is within 10 of either 100 or 200 几乎在那里:给定一个 integer n,如果 n 在 100 或 200 的 10 以内,则返回 True - ALMOST THERE: Given an integer n, return True if n is within 10 of either 100 or 200 nCk的函数从python中的n列表中选择了k个元素 - function for nCk chose k elements from list of n in python 如何修复我的代码? 使用递归,返回一个包含所有 k 的列表,使得 3 ≤ k ≤ n 并且 k 可以被 3 或 5 整除,但不能同时被 5 整除? - How can I fix my code? Using recursion, return a list containing all k such that 3 ≤ k ≤ n and k is divisible by 3 or 5 but not both? 生成列表a(n)不是素数+ a(k),k &lt;n的形式 - Generate a list a(n) is not of the form prime + a(k), k < n 编写一个输入正整数n并返回可除以17的n位正整数的数量的函数 - Write a function that input a positive integer n and return the number of n-digit positive integers that divisible by 17 "如何编写一个python函数以在整数中有偶数个0时返回True,否则使用递归返回False?" - How to write a python function to return True when there's even number of 0 in a integer and return False otherwise using recursion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM