Python Basic Skills Comprehensive Training question 2

Source: Internet
Author: User

1, to determine whether each letter in a string is in another string, you can use the properties of the collection to solve, if the elements of the collection, if the update (update) is not added to the collection, then the length of the collection is the same as the original, if added, the length of the collection will increase

' Ghost ' ' Hello, can you help me install ghost Windows XP system  '>>> b_set = Set (b)>>> b_set.update (List (a))print len (b_set) = = Len (set (b)) True'abcostg'>>> b_set.update (List (a)) print len (b_set) = = Len (set (b)) False

2, what if it's more than one character?

#!/usr/bin/python#Coding:utf-8#str_list = [' abc ', ' Ghost ', ' hello ']Str_list = ['ABC','Ghost','Hellox']target_str="Abcdefghijklopqrst"Target_str_set=Set (TARGET_STR) forValinchStr_list:target_str_set.update (val)PrintLen (target_str_set) = = Len (set (TARGET_STR))

3, statistics most occurrences of the character

[Email protected]:~/python/tmp$ python str3.py[('F', 7), ('s', 5), ('a', 4), ('J', 4), ('k', 3), ('h', 2), ('3', 2), ('1', 2), ('2', 2), ('D', 1), ('L', 1), ('4', 1), (';', 1)][email protected]:~/python/tmp$ Cat str3.py#!/usr/bin/python#Coding:utf-8Str='askfjkjasf1234fasdfasfsh;lkjfhjf123'L= ([(Key, Str.count (key)) forKeyinchset (str)]) L.sort (Key=LambdaITEM:ITEM[1], reverse =True)PrintL[email protected]:~/python/tmp$

Here is a lambda expression, key specifies which key to sort by, item is the formal parameter, represents the current tuple, item[1], that is, the 2nd item in the tuple, here is the number of strings, reverse = True, from high to low sort.

4, count the occurrences of the This module, being, is, than, three words

[Email protected]:~/python/tmp$!ppython statics.py [(' be', 3), (' is', 10), ('than', 8)][email protected]:~/python/tmp$ Cat statics.py#!/usr/bin/python#Coding:utf-8ImportOsthis_str= Os.popen ("Python-m This"). Read () This_str= This_str.replace ('\ n',"') L= This_str.split (' ' )Print[(x, L.count (x)) forXinch[' be',' is','than']][email protected]:~/python/tmp$

Os.popen ("Python-m this"). Read the command line python-m the execution result of this module into a string

5, using the displacement operator, conversion between B, KB, MB conversion relationship

[Email protected]:~/software$ ls-l sogoupinyin_2.2.0. 0102_amd64.deb-rw-rw-r--1 Ghostwu ghostwu 22852956 February 2 14:36 sogoupinyin_2.2.0. 0102_amd64.deb[email protected]:~/software$ LS-LH sogoupinyin_2.2.0. 0102_amd64.deb-rw-rw-r--1 Ghostwu ghostwu 22M February 2 14:36 sogoupinyin_2.2.0. 0102_amd64.deb[email protected]:~/software$ Pythonpython2.7.12 (Default, Dec 4 2017, 14:50:18) [GCC5.4.0 20160609] on Linux2type" Help","Copyright","credits" or "License"  forMore information.>>> size = 22852956>>>Print "%s KB"% (Size >> 10 )22317KB>>>Print "%s MB"% (Size >> 20 )21stMB>>>

6, concatenate the values in the list into a string

>>> a = [10, 20, 30, 1, 2, 3]>>> s =Str (a)>>>s'[1, 2, 3]'>>>type (s)<type'Str'>>>> s[1:-1]'Ten, 1, 2, 3'>>> S.replace (', ',"', s[1:-1]) Traceback (most recent): File"<stdin>", Line 1,inch<module>Typeerror:an integer isRequired>>> S[1:-1].replace (', ',"' )'102030123'>>>

Python Basic Skills Comprehensive Training question 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.