List of Python basic types

Source: Internet
Author: User
Tags shallow copy

List common operations and Methods list classes, lists
Li = [1, +, 9, "age", [P], ["+]]]," EDD ", True] objects created through the list class, Li
List class
An object of the list class Li = [11, 22, 33, 22, 44]
1#Conversion2# string Conversion List Li = List ("ASDFASDFASDF"), internal use for loop3s ="POUASKDFAUSPDFIAJSDKFJ"4New_li =list (s)5 Print(New_li)6 7#the list is converted into a string,8# You need to write your for loop one by one processing: both numeric and string 2018-07-239Li = [11,22,33,"123","Alex"]TenR = Str (LI)'[11,22,33, "123", "Alex"]' One Print(R) As ="" -  forIinchLi: -s = s +Str (i) the Print(s) -#directly using the string join method: The elements in the list are only strings -Li = ["123","Alex"] -v ="". Join (LI) + Print(v) -  +  

 1. Append: The original value was last appended
Object. Method (..) Li Object Call append method
1  Li.append (5)2  li.append ("Alex")3   li.append ([1234,2323])4  print(LI)
2 clear: Clear list
12print(LI)

3 copy: Copy, shallow copy
1  v = li.copy ()2  print(v)
4. Count the number of occurrences of an element
1  v = li.count (2)  print(v)

5. Expand the original list, parameters: can iterate objects
1Li = [11, 22, 33, 22, 44]2Li.append ([9898,"incredibly"])3[11, 22, 33, 22, 44, [9898,'incredibly']]4 5Li.extend ([9898,"incredibly"])6 7   forIinch[9898,"incredibly"]:8 li.append (i)9[11, 22, 33, 22, 44, 9898,'incredibly']Ten  OneLi.extend ("incredibly") A  Print(LI)

6. Get the current value index position by value (left priority)
1  Li = [one, one,2,  v= li.index]3  print(v)

7. Inserting an element at the specified index location
1  Li = [one, one, one,2  li.insert (0,99)3  print( Li

8. Delete a value (1. Specify an index; 2. The last one by default) and get the deleted value
1  Li = [one, a,2  v = li.pop ()3  print(LI)4   Print(v)56li = [one, A, a, a,  a]7   v = li.pop (1)8  print(LI)9   Print(v)

9. Delete the specified value from the list, the left priority
1  Li = [one, one, 2, Li.remove]  3  print(LI)

10 Flipping the current list
1  Li = [one, one, one,2  li.reverse ()3  print(LI)

11 Sorting of Lists
1  Li = [11,44, p, p]2  li.sort ()3  li.sort (reverse=True)4   print(LI)56  

List of Python basic types

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.