Love丶FFC's Blog

使用Python解算法题常用的一些小技巧(一)

2020-04-23 12:18:18
阅读:4667   •   评论:50
标签:

1.列表切片操作

例如,A=[1,2,3,4]、B="1256U"。

若要取A的前两位,可以A=A[0:2],要取A的后三位,可以A=A[1:4]。

切片操作可以理解为截取[起始位置:结束位置(本身不包含,因此实际上会-1)]的部分。

再进一步,不一定两个位置都要写,有些情况下可以省略,例如A=A[0:2]可以写为A=A[:2],:的左部分不写的话,默认是0;A=A[1:4]可以写为A=A[1:],:的右部分不写的话,默认是列表的长度。

如果对列表(数组)下标从0开始的概念非常清晰,并且记住:的右半部分会-1,列表切片操作就非常简单。

同理,字符串截取也可以这样,例如B[:2]就等于"12",B[2:]就等于"56U"。

补充一点,A[-2:]代表截取A的最后两位,A[-2]代表截取A的倒数第二位。也就是说负号的操作和平时的一样,只不过顺序是从尾往头,而平时的顺序是从头往尾。

PAT乙级:1023 组个最小数中就用到列表切片的操作。

2.列表中常用的几个API(index、count、find、replace)

1.index

index函数可以查找一个值在列表中的索引。例如A=[2,5,6,9,8,3,2],则A.index(2)=0。但是列表A中实际上有两个2,index也提供查找的范围,假如我们要找到第二个2的索引,可以A.index(2,1(起始位置),len(A)(结束位置)),这样index函数就等同于在A[1:]中查找2的索引。

但其实有更好的方法,如果我们要查找到第二个2的索引,可以使用rfind函数。rfind函数是从列表的右边往左边查找,而index函数、find函数都是从列表的左边往右边查找。使用rfind函数查找2就可以写为A.rfind(2)

PAT乙级:1047 编程团体赛中就同时用到了index函数和find函数。

2.count

count函数可以统计一个值在列表中出现的次数,例如A=[2,3,5,6,9,2,2],则A.count(2)=3

PAT乙级:1021 个位数统计中就用到了count函数。

3.find

find函数的功能与index函数一致,但是在找不到的情况下,find函数会返回-1,而index函数会直接报错,所以在某些情况下find函数更适合。

CCF-CSP_20150903:模板生成系统中就用到了find函数。

4.replace

replace可以把字符串中的某种值替换为别的值然后返回新字符串,例如A="AAACCCWW68",则A.replace('A','D')的结果等于"DDDCCCWW68",但这样并没有改变A的值,需要同时进行赋值操作,A=A.replace('A','D')

PAT乙级:1039 到底买不买中就用到了replace函数。

之后会在刷题的过程中继续更新Python解题的一些小技巧和常用的API函数。

评论板

共有 50 条评论

  1. fruinly

    K9 Sense of Hearing How to Optimize Hearing in Canines as They Age cialis prices Biopsies often do not reveal malignant findings

  2. engarma

    Rigid sodium restriction is generally considered to be 12 mg sodium kg bodyweight day, but this is rarely achieved cialis super active Irregular periods and or anovulation are common symptoms of PCOS

  3. cheap louis vuitton handbags

    I just wanted to thank you for the fast service. properly they look great. I received them a day earlier than expected. for instance the I will definitely continue to buy from this site. blue jays I will recommend this site to my friends. Thanks!
    cheap louis vuitton handbags https://www.louisvuittonsoutletstore.com/

  4. cheap jordans online

    I just wanted to thank you for the fast service. and even they look great. I received them a day earlier than expected. including the I will definitely continue to buy from this site. in either case I will recommend this site to my friends. Thanks!
    cheap jordans online https://www.realcheapretrojordanshoes.com/

  5. cheap retro jordans

    I just wanted to thank you for the fast service. as they look great. I received them a day earlier than expected. decline I will definitely continue to buy from this site. situation I will recommend this site to my friends. Thanks!
    cheap retro jordans https://www.realjordansretro.com/

  6. cheap jordan shoes

    I just wanted to thank you for the fast service. actually they look great. I received them a day earlier than expected. the same as the I will definitely continue to buy from this site. situation I will recommend this site to my friends. Thanks!
    cheap jordan shoes https://www.cheaprealjordan.com/

  7. authentic louis vuitton outlet

    I just wanted to thank you for the fast service. or to they look great. I received them a day earlier than expected. the same as I will definitely continue to buy from this site. in any event I will recommend this site to my friends. Thanks!
    authentic louis vuitton outlet https://www.bestlouisvuittonoutlet.com/

  8. cheap jordan shoes

    Read reviews and was a little hesitant since I had already inputted my order. aka but thank god, I had no issues. which includes the received item in a timely matter, they are in new condition. no matter what so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordan shoes https://www.realjordansshoes.com/

  9. Preliaf

    viagra vs cialis com, There is no such thing as risk free drinking

  10. gucci handbags for cheap wholesale

    This medium-sized saddle bag is very practical, and its iconic golden double-ring horsebit buckle is very chic, suitable for people of all styles. I prefer the style with the logo, but I am also happy to have any all-leather style. Crazy bloggers can’t wait to show their personality on their backs in 1955! Don’t you hurry up and find out? ▼

  11. cheap real retro jordans

    Stay up to date with retro Air Jordans and other Jordan shoes as well as Michael Jordan history. Nice Kicks has Jordan shoe release dates for Cheap Jordans shoes 2014. Never miss such wonderful online store to offer air jordans for cheap. Many 2013 newest cheap air jordans come out and hurry up to enjoy jordans free shipping https://airjordantrade.com here.

  12. gucci sling bags women

    Therefore, in this GG Marmont bag series, you will also feel the retro style. In fact, it also carries the nostalgia of designer Alessandro Michele. At the same time, it also achieves the integration of art and fashion, and continuous innovative design. Give it the “blood” of a new era.

  13. Preliaf

    PMID 29371962 Free PMC article cialis buy cyclosporine para qu sirve klaricid Steven Padwick, an Associated Press Television News cameraman at the scene, said recently that his crew arrived on June 30 at about 10 p

  14. gucci used handbags

    Many people say that GucciOphidia is the most valuable bag to buy in 2018. GucciOphidia originally came from the Resort show. This series of products has many different bag types. Handbags, round mini bags, shoulder bags, shoulder bags, bucket bags, and medium shopping bags. White is dominant and red and blue ribbons are designed to be simple and diverse

  15. cheap boy's jordans

    $ 117.06Cheap Jordan 33 black silver

  16. ZemXzBd

    green viagra pill In very rare cases the hair does not grow back

  17. golden goose kids

    It is best to take part in a contest for top-of-the-line blogs on the web. I will advocate this web site!

  18. goldengoose shoes

    WONDERFUL Post.thanks for share..extra wait .. ?

  19. Qjfaly

    buy fenofibrate online tricor 160mg tablet order generic fenofibrate

  20. golden goose dupes

    I was very pleased to seek out this net-site.I wished to thanks in your time for this glorious read!! I definitely enjoying each little little bit of it and I’ve you bookmarked to check out new stuff you weblog post.

  21. westbrook shoes

    There are certainly loads of details like that to take into consideration. That is a great level to carry up. I provide the thoughts above as normal inspiration but clearly there are questions just like the one you convey up where an important factor can be working in sincere good faith. I don?t know if best practices have emerged around things like that, but I’m sure that your job is clearly identified as a fair game. Each boys and girls really feel the impact of just a second抯 pleasure, for the remainder of their lives.

  22. kd 12

    You made some respectable points there. I seemed on the internet for the issue and located most individuals will go together with with your website.

  23. off white hoodie

    The following time I learn a weblog, I hope that it doesnt disappoint me as a lot as this one. I mean, I know it was my option to read, but I actually thought youd have one thing interesting to say. All I hear is a bunch of whining about one thing that you would fix should you werent too busy on the lookout for attention.

  24. pg shoes

    I抦 impressed, I must say. Actually not often do I encounter a blog that抯 both educative and entertaining, and let me let you know, you may have hit the nail on the head. Your thought is outstanding; the difficulty is something that not sufficient persons are talking intelligently about. I’m very glad that I stumbled across this in my search for one thing relating to this.

  25. chrome hearts outlet

    Can I just say what a aid to find somebody who really is aware of what theyre talking about on the internet. You definitely know the right way to convey a problem to light and make it important. More individuals need to learn this and perceive this aspect of the story. I cant consider youre no more standard because you definitely have the gift.

  26. supreme

    Oh my goodness! a tremendous article dude. Thank you Nevertheless I am experiencing issue with ur rss . Don抰 know why Unable to subscribe to it. Is there anyone getting equivalent rss drawback? Anybody who knows kindly respond. Thnkx

  27. black golden goose sneakers

    I’m also writing to let you be aware of what a magnificent encounter my girl developed viewing your web site. She learned a wide variety of issues, which included what it’s like to have an ideal helping spirit to make a number of people smoothly learn about certain problematic matters. You undoubtedly exceeded her expected results. Many thanks for coming up with these great, dependable, educational and in addition easy tips about this topic to Tanya.

  28. Nnbpoe

    pill ketotifen 1mg purchase tofranil without prescription buy imipramine pills

  29. Wwxssd

    generic cialis 5mg order viagra 50mg pills order generic sildenafil 100mg

  30. supreme outlet

    There are some interesting time limits in this article but I don抰 know if I see all of them heart to heart. There may be some validity however I will take maintain opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as nicely

  31. jordan 1 off white

    Good day! I just wish to give an enormous thumbs up for the good info you will have right here on this post. I will be coming again to your blog for extra soon.

  32. Okflqb

    purchase minoxytop sale tadalafil 5mg drug over the counter ed pills that work

  33. Kysdro

    precose buy online buy griseofulvin without a prescription fulvicin price

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:446771