您的位置:首页 > 编程语言 > Python开发

Python解一道题的N种做法(2)

2016-02-29 12:28 1026 查看
Description:

We need a method in the List Class that may count specific digits from a given list of integers. This marked digits will be given in a second list. The method .count_spec_digits()/.countSpecDigits() will accept two arguments,
a list of an uncertain amount of integers integers_lists/integersLists (and of an uncertain amount of digits, too) and a second list, digits_list/digitsList that has the specific digits to count which length cannot be be longer than 10 (It's obvious, we've
got ten digits). The method will output a list of tuples, each tuple having two elements, the first one will be a digit to count, and second one, its corresponding total frequency in all the integers of the first list.  This list of tuples should be ordered
with the same order that the digits have in digitsList

Let's see some cases:

l = List()

integers_list = [1, 1, 2 ,3 ,1 ,2 ,3 ,4]
digits_list = [1, 3]
l.count_spec_digits(integers_list, digits_list) == [(1, 3), (3, 2)]

integers_list = [-18, -31, 81, -19, 111, -888]
digits_list = [1, 8, 4]
l.count_spec_digits(integers_list, digits_list) == [(1, 7), (8, 5), (4, 0)]

integers_list = [-77, -65, 56, -79, 6666, 222]
digits_list = [1, 8, 4]
l.count_spec_digits(integers_list, digits_list) == [(1, 0), (8, 0), (4, 0)]======================================================================
首先我的思路是利用join()方法将列表integers_list转换成字符串,利用之前学到的str.count方法来实现计数:

class List(object):
def count_spec_digits(self, integers_list, digits_list):
str_integers_list = [str(abs(item)) for item in integers_list]
str_digits_list = [str(item) for item in digits_list]
string = ''.join(str_integers_list)
return [(a,b) for (a,b) in zip(digits_list,map(lambda x:str.count(string,x),str_digits_list))]其实转换的时候不用取绝对值abs,这样反而耗时。
=======================================================================

看一下别人的做法:

(1)利用collections模块的Counter类计数

from collections import Counter

class List(object):
@staticmethod
def count_spec_digits(integers_list, digits_list):
counts = Counter(''.join(str(abs(a)) for a in integers_list))
return [(b, counts[str(b)]) for b in digits_list]他使用了装饰器将方法写成静态的,而且最后的return语句写的非常简洁明了,反观我写的就比较复杂,所以效率就比较低了。
-----------------------------------------------------------------------------------------------------------------

(2)与我的想法类似,但是写法更精炼简洁,转换成字符串的时候用到了生成器表达式,这个效率应该更高。

class List(object):
def count_spec_digits(self, integers_list, digits_list):
s = "".join(str(i) for i in integers_list)
return [(dig, s.count(str(dig))) for dig in digits_list]------------------------------------------------------------------------------------------------------------------
(3)one line python

class List(object):
def count_spec_digits(self, ar, digits):
return [(d, sum(str(n).count(str(d)) for n in ar)) for d in digits]------------------------------------------------------------------------------------------------------------------
(4)最传统的做法,效率肯定比较低吧

class List(object):
def count_spec_digits(self, integers_list, digits_list):
result = []
for a in range(len(digits_list)):
digits_list[a] = str(digits_list[a])
for a in range(len(integers_list)):
integers_list[a] = str(integers_list[a])
for a in digits_list:
count = 0
for b in integers_list:
count += b.count(a)
result.append((int(a),count))
return result
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: