Python basic data results and usage (str,list,tuple,doct,set) Notes

Source: Internet
Author: User
Tags delete key

    

One, the string (str) is important ...

How to create

    1. STR1 = "ABC"
    2. str2 = str ()

Common operations

  1. S.replace (' A ', ' B ') replaces the A in s with the B
  2. Str.capitalize () capitalizes the first letter in Str
  3. Str.upper () Uppercase
  4. Str.lower () lowercase
  5. S.swapcase () Uppercase and lowercase
  6. Str.startswith (' xx ') to determine whether to start with XX
  7. Str.endswith (' G ') to determine whether the end of G
  8. Str.isnumeric () determines whether a number
  9. List = Str.split ('. ') Use the list. Divided into lists, but must be in list.
  10. '. '. Join (list) with. Connect List to
  11. S.strip () Remove space
  12. Sstr1.index (SSTR2) or Sstr1.find (SSTR2) find
  13. Len (SSTR1) string length
  14. Split () split
  15. A[0:3] Slice, which refers to the middle element of >=0<3 in a
  16. SSTR1 = Sstr1[::-1] Reverses the SSTR1 string and assigns the value to SSTR1

< Span class= "string" > append: Escape character in string:

    • < Span class= "string" >\\     slash
      \ '      single quotes
      \ "     Double quotes
      \a     system horn
      \b      BACKSPACE
      \n     line break
      \t      horizontal tab
      \v     portrait tab
      \r      return character
      \f     page break
      \o      octal number represents the character   
      \x     hexadecimal number represents the character &NBSP;&NBSP;
      \000 Terminator, ignoring all characters after \000

Second, lists (list ordered, mutable) are important .....

How to create

  1. listA = [‘a‘, ‘b‘, ‘c‘, 1, 2]
  2. list(obj)     
  3. #把对象转换成列表,obj可以是元组,字典,字符串等
  4. Note: The list can support multiple layers of nesting.
  5. Note: If the string needs to contain double quotes, do not forget to escape, "\" "\"

Common operations  

    1. List.append () Append member, member data
    2. List.pop () Delete Members, delete member I, note: can be individually given to a variable to be deleted
    3. List.count (x) calculates the number of occurrences of the parameter X in the list
    4. List.remove () Remove members from list, delete members directly I
    5. List.extend (l) Append another list to the list L
    6. List.reverse () Reverses the order of the members in the list
    7. List.index (x) Gets the position of the parameter X in the list
    8. List.sort () sort the members in the list
    9. List.insert () inserting data into the list insert (A, B) inserts data into the list
    10. The List.isinstance () function can be used to determine whether a particular identifier contains a particular type of data

Third, Ganso (tuple, ordered, non-modifiable) need to add common operations

How to create

  1. tuple1 =()
  2. tuple1 = tuple({1,2,3,4,5,‘6‘})
  3. tuple1 = (1, 2, ‘3‘, 4, ‘5‘)
  4. Note: You cannot add or modify elements in a tuple after you define a tuple, but you can modify the elements of the following (except Ganso) for grandchildren, add
  5. Tuple1[-1] refers to the calculation of the last beginning of the Yongzu

Four, the dictionary (Dictionary, unordered, modifiable) is important ....

How to create

  1. Ict1 = {' name ' : ' Liuzhichao ', ' age ' : , ' sex ' : ' Male '}
  2. < Span class= "PLN" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" >ict1 = dict ()

< Span class= "PLN" >< span class= "str" >< Span class= "PLN" >< Span class= "pun" > common actions

    1. Dic.clear () Empty dictionary
    2. Dic.keys () Get a list of keys
    3. Dic.values () Get a list of values
    4. Dic.copy () Copy Dictionary
    5. Dic.pop (k) Delete key k
    6. Dic.get (k) Gets the value of the key K
    7. Dic.update () Update member, if member does not exist, equivalent to join
    8. Dic.items () Gets a list of keys and values

Four, set (unordered, cannot repeat)

How to create

    1. set1 = {1, 2, 3, 4, 5}
    2. Set2 = set ()

Common operations

  1. set2.add(10) #添加新元素 10,要保证set2中没有10 否则就添加一个10  或添加一个obj
  2. set3 = frozenset(list1)      set3.add(10)  固定集合不能添加元素
  3. s.issubset(t) #如果s是t的子集,返回True,否则返回Falses.
  4. issuperset(t) #如果s是t的超集,返回True,否则返回Falses.
  5. union(t) #返回一个新集合, 该集合是s和t的并集
  6. s.intersection(t) #返回一个新集合, 该集合是s和t的交集
  7. s.difference(t) #返回一个新集合, 该集合是s的成员, 但不是t的成员, 即返回s不同于t的元素
  8. s.copy() #返回一个s的浅拷贝, 效率比工厂要好     #不明白
  9. s.update(t) #用t中的元素 修改s,即s现在包含s或t的成员
  10. s.difference_update(t) #s存在,t不存在,更新t
  11. s.remove(obj) #从集合s中删除对象obj,如果obj不是集合s中的元素(obj not in s),将引发keyError错误
  12. s.discard(obj) #如果obj是集合s中的元素,从集合s中删除对象obj
  13. s.pop() #删除集合s中得任意一个对象,并返回它
  14. s.clear() #删除集合s中的所有元素
  15. The intersection () method returns a new collection that contains all the elements that appear at the same time in the two collection.
  16. The Union () method returns a new collection containing elements that are not the same in two collections.
  17. The Symmetric_difference () method returns a new collection that contains all elements that appear only in one of the collections.

Python basic data results and usage (str,list,tuple,doct,set) Notes

Related Article

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.