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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值