Python Foundation-list

Source: Internet
Author: User
1. List creation

New_list1 = [' TV ', ' Car ', ' Cloth ', ' food ']
New_list2 = List ([' TV ', ' Car ', ' Cloth ', ' food '])
Print (NEW_LIST1)
Print (NEW_LIST2)
Operation Result:
[' TV ', ' Car ', ' Cloth ', ' food ']
[' TV ', ' Car ', ' Cloth ', ' food ']

2, list commonly used methods:

1. Append

New_list1 = [' TV ', ' Car ', ' Cloth ', ' food ']
Print (NEW_LIST1)
New_list1.append (' water ') #追加 ' water '
Print (NEW_LIST1)
Operation Result:
[' TV ', ' Car ', ' Cloth ', ' food ']
[' TV ', ' Car ', ' Cloth ', ' food ', ' water ']

2.count

New_list1 = [' TV ', ' Car ', ' Cloth ', ' food ', ' food ']
Print (New_list1.count (' food ')) #统计 ' food ' number of times in the list = 2

3.extend

New_list1 = [' TV ', ' Car ', ' Cloth ', ' food ', ' food ']
New_list1.extend ([11,22,33, ' Car '])
Print (NEW_LIST1)
Operation Result:
[' TV ', ' car ', ' Cloth ', ' food ', ' food ', one, one, $, ' car ']

4.sort

New_list1 = [' TV ', ' Car ', ' Cloth ', ' food ', ' food ']
Print (NEW_LIST1)
New_list1.sort () #正向排序
Print (NEW_LIST1)
New_list1.sort (reverse=true) #反向排序
Print (NEW_LIST1)
Operation Result:
[' TV ', ' Car ', ' Cloth ', ' food ', ' food ']
[' Car ', ' Cloth ', ' food ', ' food ', ' TV '] #正向排序结果
[' TV ', ' food ', ' food ', ' Cloth ', ' Car '] #反向排序结果

5. Len

New_list1 = [' TV ', ' Car ', ' food ', ' Cloth ', ' food ']
Print (len (new_list1)) = 5 #打印元素的个数

6.remove

New_list1 = [' TV ', ' Car ', ' food ', ' Cloth ', ' food ']

New_list1.remove (' food ')
Print (NEW_LIST1)
Operation Result:
[' TV ', ' Car ', ' Cloth ', ' food '] #remove会删除找到的第一个相同元素

7.pop

New_list1 = [' TV ', ' Car ', ' food ', ' Cloth ', ' food ']
New_list1.pop () #随机删除一个元素
Print (NEW_LIST1)
New_list1.pop (2) #指定删除某个元素
Print (NEW_LIST1)
The result of the operation is:
[' TV ', ' Car ', ' food ', ' Cloth ']
[' TV ', ' Car ', ' Cloth ']#, the 3rd element ' food ' is removed here

8.index

New_list1 = [' TV ', ' Car ', ' food ', ' Cloth ', ' food ']
Print (New_list1.index (' food ')) #返回指定元素的index值, the same element returns the first one found
Operation Result: 2

9.insert

New_list1 = [' TV ', ' Car ', ' food ', ' Cloth ', ' food ']
New_list1.insert (2, ' hahaha ') #向指定的位置插入元素
Print (NEW_LIST1)
The result of the operation is:
[' TV ', ' Car ', ' hahaha ', ' food ', ' Cloth ', ' food ']

  • 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.