Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要帮助:
我想编写这样的函数:将列表作为输入并返回特定字符串的计数。
如果函数输入是 x=[a,a,b,c]函数需要返回2(a在这个列表中是两次)。
x=[a,a,b,c]
2
a
>>> def F(seq, string): return seq.count(string) >>> F(['a','a','b','c'], 'a') 2
相当于:
def F(seq, string): n = 0 for x in seq: if x == string: n += 1 return n