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
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
相关文章推荐
- Python中三引号的区别
- python Debug 单步调试
- IPython使用问题记录
- theano 0.8与pydot、graphziv的冲突解决
- python 环境搭建
- python类中实现dict的功能
- Python执行系统命令的方法 os.system(),os.popen(),commands
- python爬虫代码
- python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期
- 1-Python 安装 Matplotlib
- 2+python解析RSS(feedparser)
- 打造自己的Python编码环境
- Python中用feedparser解析RSS
- Python 的生成二维码生成库 -- qrcode
- python爬虫抓网页的总结
- Python爬虫(一):环境配置
- python入门经典(二)
- Python2.x与Python3.x的区别
- Learning Python(10)--input与raw_input
- python学习笔记六之面向对象相关下(基础篇)