接触colletions

今天打卡leetcode,遇到了一个关于回文数的题目

https://leetcode-cn.com/problems/longest-palindrome/

解这个题的时候,会去统计一个字符的重复个数

比如: 'ababc'

a出现2次,b出现2次,c出现1次

我是这样写的

ss = 'ababc'
dict = {}
for s in ss:
    if s in dict:
        dict[s] += 1
    else:
        dict[s] = 1
dict = {
    'a': 2,
    'b': 2,
    'c': 1
}

遍历整个字符串,用字典保存其每个元素的结果

然后在评论区,看到了collections模块,好吧,香得不得了

这是一个内置模块

要实现上诉的要求,用collections可以这样写

from collections import Counter
ss = 'ababc'
ans = Counter(ss)
Counter({'a': 2, 'b': 2, 'c': 1})

突然觉得自己傻X了,内置模块这么多好玩好使的,都没掌握,O(∩_∩)O哈哈~

Counterdict的一个子类,所以返回的也是一个字典

抱着好奇的心态,去Python文档上看了下,colletions确实强大且方便

关于colletions

https://docs.python.org/zh-cn/3/library/collections.htm

详细的可以看上面的官方文档,下面做了一个小的对比

关于列表,我们要在0号位置插入一个元素

肯定用insert

list.insert(x, val)

使用colletions创建一个deque 类似list的一个容器,可以实现快速在两端添加和弹出

list = collections.deque()
list.appendleft(val)

做了下方比较

list的insert(0)&deque appendleft比较

import collections
import time

list1 = list()
list2 = collections.deque()

s = time.time()
for i in range(100000):
    list1.insert(0, 123)
print(time.time() - s)

s = time.time()
for i in range(100000):
    list2.appendleft(123)
print(time.time() - s)

执行结果:

2.117663621902466
0.007246971130371094

使用insert在下标0处,插入100000次,需要耗时2.117秒

使用collections.deque 的 appendleft 100000次,仅需0.007秒

insert对比

s = time.time()
for i in range(100000):
    list1.insert(0, 123)
print(time.time() - s)

s = time.time()
for i in range(100000):
    list2.insert(0, 123)
print(time.time() - s)

执行结果:

2.1289243698120117
0.010915756225585938

list.append&deque.insert(0)对比

我们使用list.append 来向list列表中添加元素

我们知道append操作,时间复杂度是O(1),insert操作是O(n)

然后和 deque 的 insert(0)对比看看

s = time.time()
for i in range(100000):
    list1.append(i)
print(time.time() - s)

s = time.time()
for i in range(100000):
    list2.insert(0, i)
print(time.time() - s)

执行结果:

0.010788679122924805
0.012774229049682617

时间相差无几

为什么这么快?

其实deque是一个双端队列,头就是尾,尾就是头

Deque队列是由栈或者queue队列生成的(发音是 “deck”,”double-ended queue”的简称)。Deque 支持线程安全,内存高效添加(append)和弹出(pop),从两端都可以,两个方向的大概开销都是 O(1) 复杂度。

已有 9 条评论
  1. Eric Jones Eric Jones

    Hi, my name is Eric and I’m betting you’d like your website clost.net to generate more leads.

    Here’s how:
    Talk With Web Visitor is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. It signals you as soon as they say they’re interested – so that you can talk to that lead while they’re still there at clost.net.

    Talk With Web Visitor – CLICK HERE http://www.talkwithwebvisitor.com for a live demo now.

    And now that you’ve got their phone number, our new SMS Text With Lead feature enables you to start a text (SMS) conversation – answer questions, provide more info, and close a deal that way.

    If they don’t take you up on your offer then, just follow up with text messages for new offers, content links, even just “how you doing?” notes to build a relationship.

    CLICK HERE http://www.talkwithwebvisitor.com to discover what Talk With Web Visitor can do for your business.

    The difference between contacting someone within 5 minutes versus a half-hour means you could be converting up to 100X more leads today!

    Try Talk With Web Visitor and get more leads now.

    Eric
    PS: The studies show 7 out of 10 visitors don’t hang around – you can’t afford to lose them!
    Talk With Web Visitor offers a FREE 14 days trial – and it even includes International Long Distance Calling.
    You have customers waiting to talk with you right now… don’t keep them waiting.
    CLICK HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

  2. Eric Jones Eric Jones

    Good day,

    My name is Eric and unlike a lot of emails you might get, I wanted to instead provide you with a word of encouragement – Congratulations

    What for?

    Part of my job is to check out websites and the work you’ve done with clost.net definitely stands out.

    It’s clear you took building a website seriously and made a real investment of time and resources into making it top quality.

    There is, however, a catch… more accurately, a question…

    So when someone like me happens to find your site – maybe at the top of the search results (nice job BTW) or just through a random link, how do you know?

    More importantly, how do you make a connection with that person?

    Studies show that 7 out of 10 visitors don’t stick around – they’re there one second and then gone with the wind.

    Here’s a way to create INSTANT engagement that you may not have known about…

    Talk With Web Visitor is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. It lets you know INSTANTLY that they’re interested – so that you can talk to that lead while they’re literally checking out clost.net.

    CLICK HERE http://www.talkwithwebvisitor.com to try out a Live Demo with Talk With Web Visitor now to see exactly how it works.

    It could be a game-changer for your business – and it gets even better… once you’ve captured their phone number, with our new SMS Text With Lead feature, you can automatically start a text (SMS) conversation – immediately (and there’s literally a 100X difference between contacting someone within 5 minutes versus 30 minutes.)

    Plus then, even if you don’t close a deal right away, you can connect later on with text messages for new offers, content links, even just follow up notes to build a relationship.

    Everything I’ve just described is simple, easy, and effective.

    CLICK HERE http://www.talkwithwebvisitor.com to discover what Talk With Web Visitor can do for your business.

    You could be converting up to 100X more leads today!

    Eric
    PS: Talk With Web Visitor offers a FREE 14 days trial – and it even includes International Long Distance Calling.
    You have customers waiting to talk with you right now… don’t keep them waiting.
    CLICK HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

  3. Eric Jones Eric Jones

    Hi, Eric here with a quick thought about your website clost.net...

    I’m on the internet a lot and I look at a lot of business websites.

    Like yours, many of them have great content.

    But all too often, they come up short when it comes to engaging and connecting with anyone who visits.

    I get it – it’s hard. Studies show 7 out of 10 people who land on a site, abandon it in moments without leaving even a trace. You got the eyeball, but nothing else.

    Here’s a solution for you…

    Talk With Web Visitor is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. You’ll know immediately they’re interested and you can call them directly to talk with them literally while they’re still on the web looking at your site.

    CLICK HERE http://www.talkwithwebvisitor.com to try out a Live Demo with Talk With Web Visitor now to see exactly how it works.

    It could be huge for your business – and because you’ve got that phone number, with our new SMS Text With Lead feature, you can automatically start a text (SMS) conversation – immediately… and contacting someone in that 5 minute window is 100 times more powerful than reaching out 30 minutes or more later.

    Plus, with text messaging you can follow up later with new offers, content links, even just follow up notes to keep the conversation going.

    Everything I’ve just described is extremely simple to implement, cost-effective, and profitable.

    CLICK HERE http://www.talkwithwebvisitor.com to discover what Talk With Web Visitor can do for your business.

    You could be converting up to 100X more eyeballs into leads today!

    Eric
    PS: Talk With Web Visitor offers a FREE 14 days trial – and it even includes International Long Distance Calling.
    You have customers waiting to talk with you right now… don’t keep them waiting.
    CLICK HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

  4. Eric Jones Eric Jones

    Hey there, I just found your site, quick question…

    My name’s Eric, I found clost.net after doing a quick search – you showed up near the top of the rankings, so whatever you’re doing for SEO, looks like it’s working well.

    So here’s my question – what happens AFTER someone lands on your site? Anything?

    Research tells us at least 70% of the people who find your site, after a quick once-over, they disappear… forever.

    That means that all the work and effort you put into getting them to show up, goes down the tubes.

    Why would you want all that good work – and the great site you’ve built – go to waste?

    Because the odds are they’ll just skip over calling or even grabbing their phone, leaving you high and dry.

    But here’s a thought… what if you could make it super-simple for someone to raise their hand, say, “okay, let’s talk” without requiring them to even pull their cell phone from their pocket?

    You can – thanks to revolutionary new software that can literally make that first call happen NOW.

    Talk With Web Visitor is a software widget that sits on your site, ready and waiting to capture any visitor’s Name, Email address and Phone Number. It lets you know IMMEDIATELY – so that you can talk to that lead while they’re still there at your site.

    You know, strike when the iron’s hot!

    CLICK HERE http://www.talkwithwebvisitor.com to try out a Live Demo with Talk With Web Visitor now to see exactly how it works.

    When targeting leads, you HAVE to act fast – the difference between contacting someone within 5 minutes versus 30 minutes later is huge – like 100 times better!

    That’s why you should check out our new SMS Text With Lead feature as well… once you’ve captured the phone number of the website visitor, you can automatically kick off a text message (SMS) conversation with them.

    Imagine how powerful this could be – even if they don’t take you up on your offer immediately, you can stay in touch with them using text messages to make new offers, provide links to great content, and build your credibility.

    Just this alone could be a game changer to make your website even more effective.

    Strike when the iron’s hot!

    CLICK HERE http://www.talkwithwebvisitor.com to learn more about everything Talk With Web Visitor can do for your business – you’ll be amazed.

    Thanks and keep up the great work!

    Eric
    PS: Talk With Web Visitor offers a FREE 14 days trial – you could be converting up to 100x more leads immediately!
    It even includes International Long Distance Calling.
    Stop wasting money chasing eyeballs that don’t turn into paying customers.
    CLICK HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

  5. Eric Jones Eric Jones

    Hey, my name’s Eric and for just a second, imagine this…

    - Someone does a search and winds up at clost.net.

    - They hang out for a minute to check it out. “I’m interested… but… maybe…”

    - And then they hit the back button and check out the other search results instead.

    - Bottom line – you got an eyeball, but nothing else to show for it.

    - There they go.

    This isn’t really your fault – it happens a LOT – studies show 7 out of 10 visitors to any site disappear without leaving a trace.

    But you CAN fix that.

    Talk With Web Visitor is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. It lets you know right then and there – enabling you to call that lead while they’re literally looking over your site.

    CLICK HERE http://www.talkwithwebvisitor.com to try out a Live Demo with Talk With Web Visitor now to see exactly how it works.

    Time is money when it comes to connecting with leads – the difference between contacting someone within 5 minutes versus 30 minutes later can be huge – like 100 times better!

    Plus, now that you have their phone number, with our new SMS Text With Lead feature you can automatically start a text (SMS) conversation… so even if you don’t close a deal then, you can follow up with text messages for new offers, content links, even just “how you doing?” notes to build a relationship.

    Strong stuff.

    CLICK HERE http://www.talkwithwebvisitor.com to discover what Talk With Web Visitor can do for your business.

    You could be converting up to 100X more leads today!

    Eric
    PS: Talk With Web Visitor offers a FREE 14 days trial – and it even includes International Long Distance Calling.
    You have customers waiting to talk with you right now… don’t keep them waiting.
    CLICK HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

  6. Eric Jones Eric Jones

    Hi, my name is Eric and I’m betting you’d like your website clost.net to generate more leads.

    Here’s how:
    Talk With Web Visitor is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. It signals you as soon as they say they’re interested – so that you can talk to that lead while they’re still there at clost.net.

    Talk With Web Visitor – CLICK HERE http://www.talkwithwebvisitor.com for a live demo now.

    And now that you’ve got their phone number, our new SMS Text With Lead feature enables you to start a text (SMS) conversation – answer questions, provide more info, and close a deal that way.

    If they don’t take you up on your offer then, just follow up with text messages for new offers, content links, even just “how you doing?” notes to build a relationship.

    CLICK HERE http://www.talkwithwebvisitor.com to discover what Talk With Web Visitor can do for your business.

    The difference between contacting someone within 5 minutes versus a half-hour means you could be converting up to 100X more leads today!

    Try Talk With Web Visitor and get more leads now.

    Eric
    PS: The studies show 7 out of 10 visitors don’t hang around – you can’t afford to lose them!
    Talk With Web Visitor offers a FREE 14 days trial – and it even includes International Long Distance Calling.
    You have customers waiting to talk with you right now… don’t keep them waiting.
    CLICK HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

  7. Eric Jones Eric Jones

    Cool website!

    My name’s Eric, and I just found your site - clost.net - while surfing the net. You showed up at the top of the search results, so I checked you out. Looks like what you’re doing is pretty cool.

    But if you don’t mind me asking – after someone like me stumbles across clost.net, what usually happens?

    Is your site generating leads for your business?

    I’m guessing some, but I also bet you’d like more… studies show that 7 out 10 who land on a site wind up leaving without a trace.

    Not good.

    Here’s a thought – what if there was an easy way for every visitor to “raise their hand” to get a phone call from you INSTANTLY… the second they hit your site and said, “call me now.”

    You can –

    Talk With Web Visitor is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. It lets you know IMMEDIATELY – so that you can talk to that lead while they’re literally looking over your site.

    CLICK HERE http://www.talkwithwebvisitor.com to try out a Live Demo with Talk With Web Visitor now to see exactly how it works.

    Time is money when it comes to connecting with leads – the difference between contacting someone within 5 minutes versus 30 minutes later can be huge – like 100 times better!

    That’s why we built out our new SMS Text With Lead feature… because once you’ve captured the visitor’s phone number, you can automatically start a text message (SMS) conversation.

    Think about the possibilities – even if you don’t close a deal then and there, you can follow up with text messages for new offers, content links, even just “how you doing?” notes to build a relationship.

    Wouldn’t that be cool?

    CLICK HERE http://www.talkwithwebvisitor.com to discover what Talk With Web Visitor can do for your business.

    You could be converting up to 100X more leads today!
    Eric

    PS: Talk With Web Visitor offers a FREE 14 days trial – and it even includes International Long Distance Calling.
    You have customers waiting to talk with you right now… don’t keep them waiting.
    CLICK HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

  8. Eric Jones Eric Jones

    Good day,

    My name is Eric and unlike a lot of emails you might get, I wanted to instead provide you with a word of encouragement – Congratulations

    What for?

    Part of my job is to check out websites and the work you’ve done with clost.net definitely stands out.

    It’s clear you took building a website seriously and made a real investment of time and resources into making it top quality.

    There is, however, a catch… more accurately, a question…

    So when someone like me happens to find your site – maybe at the top of the search results (nice job BTW) or just through a random link, how do you know?

    More importantly, how do you make a connection with that person?

    Studies show that 7 out of 10 visitors don’t stick around – they’re there one second and then gone with the wind.

    Here’s a way to create INSTANT engagement that you may not have known about…

    Talk With Web Visitor is a software widget that’s works on your site, ready to capture any visitor’s Name, Email address and Phone Number. It lets you know INSTANTLY that they’re interested – so that you can talk to that lead while they’re literally checking out clost.net.

    CLICK HERE http://www.talkwithwebvisitor.com to try out a Live Demo with Talk With Web Visitor now to see exactly how it works.

    It could be a game-changer for your business – and it gets even better… once you’ve captured their phone number, with our new SMS Text With Lead feature, you can automatically start a text (SMS) conversation – immediately (and there’s literally a 100X difference between contacting someone within 5 minutes versus 30 minutes.)

    Plus then, even if you don’t close a deal right away, you can connect later on with text messages for new offers, content links, even just follow up notes to build a relationship.

    Everything I’ve just described is simple, easy, and effective.

    CLICK HERE http://www.talkwithwebvisitor.com to discover what Talk With Web Visitor can do for your business.

    You could be converting up to 100X more leads today!

    Eric
    PS: Talk With Web Visitor offers a FREE 14 days trial – and it even includes International Long Distance Calling.
    You have customers waiting to talk with you right now… don’t keep them waiting.
    CLICK HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

  9. Eric Jones Eric Jones

    My name’s Eric and I just came across your website - clost.net - in the search results.

    Here’s what that means to me…

    Your SEO’s working.

    You’re getting eyeballs – mine at least.

    Your content’s pretty good, wouldn’t change a thing.

    BUT…

    Eyeballs don’t pay the bills.

    CUSTOMERS do.

    And studies show that 7 out of 10 visitors to a site like clost.net will drop by, take a gander, and then head for the hills without doing anything else.

    It’s like they never were even there.

    You can fix this.

    You can make it super-simple for them to raise their hand, say, “okay, let’s talk” without requiring them to even pull their cell phone from their pocket… thanks to Talk With Web Visitor.

    Talk With Web Visitor is a software widget that sits on your site, ready and waiting to capture any visitor’s Name, Email address and Phone Number. It lets you know immediately – so you can talk to that lead immediately… without delay… BEFORE they head for those hills.

    CLICK HERE http://www.talkwithwebvisitor.com to try out a Live Demo with Talk With Web Visitor now to see exactly how it works.

    Now it’s also true that when reaching out to hot leads, you MUST act fast – the difference between contacting someone within 5 minutes versus 30 minutes later is huge – like 100 times better!

    That’s what makes our new SMS Text With Lead feature so powerful… you’ve got their phone number, so now you can start a text message (SMS) conversation with them… so even if they don’t take you up on your offer right away, you continue to text them new offers, new content, and new reasons to do business with you.

    This could change everything for you and your business.

    CLICK HERE http://www.talkwithwebvisitor.com to learn more about everything Talk With Web Visitor can do and start turing eyeballs into money.

    Eric
    PS: Talk With Web Visitor offers a FREE 14 days trial – you could be converting up to 100x more leads immediately!
    It even includes International Long Distance Calling.
    Paying customers are out there waiting.
    Starting connecting today by CLICKING HERE http://www.talkwithwebvisitor.com to try Talk With Web Visitor now.

    If you'd like to unsubscribe click here http://talkwithwebvisitor.com/unsubscribe.aspx?d=clost.net

添加新评论