List of DAY4 python data types

Source: Internet
Author: User

A. List

 Lists: Lists are combinations of elements that are arranged in a specific order. A list is the equivalent of a container that can store any Python data type, which is also known as an array in another language.

I. Creating an array
Li = [' Python ', ' C ', ' Java ', ' C + + ']str_list = [' C.]   ---->   [' C ', ' C. ', ' C ']      #谨记: is to turn the string into three instead of "CCC" str_ list = [[' C '] * 3]----> [[' C ', ' C ', ' C ']]   #谨记: Lists the elements in the list are repeated three times
Two. Index, Slice, step

(1) Index

# in the list, the element to which the index is accessed depends on what type it is.

Li = [' python ', ' P ', ' Java ', ' C + + ']print (Li[0],type (li[0)) ' >>> ' Python ', Strprint (Li[1],type (li[1)) >> >100,int

(2) slicing

# The slice of the list gets a list

Li = [' python ', ' P ', ' Java ', ' C + + ']print (Li[0:3]) >>>[' Python ', p, ' Java ']

(3) Slicing + step (can also reverse step)

Li = [' python ', ' P ', ' Java ', ' C + + ']print (li[::-1]) >>>[' C + + ', ' Java ', ' P ', ' Python ']

 

Three. List additions and deletions

 #增

Append (): Append to the end of the list

Insert (Index,value): Inserts the element into the corresponding position

Extend (): Add the elements of the sequence to the list separately

#append (): Append l1 = [' Alex ', ' Wusir ', ' Taibai ']l1.append (' Egon ')   # Append element L1.append ([])  # append list print (L1)

' Insert (Index,value): Insert, insert element into corresponding position '
L1 = [' Alex ', ' Wusir ', ' Taibai ']
L1.insert (1, ' Bao Yuan ')
Print (L1)

"' Extend () '
# L1 = [' Alex ', ' Wusir ', ' Taibai ')
# l1.extend (' abc ')
>>>[' Alex ', ' Wusir ', ' Taibai ', ' A ', ' B ', ' C ']
# l1.extend ([111,222,333])
# print (L1)

# Delete

Pop (index): Know the index value of the element in advance, delete the corresponding element according to the index value, and return the element for later use.

Remove (): Know the name of the element beforehand, and delete it according to the element name.

Clear (): Clears the list of elements, but preserves the list itself

Del statement: 1. Delete elements by index value (unlike pop, elements cannot be returned)

2. You can delete elements by slicing

3. You can delete the entire list at the memory level

"Pop (): Delete by index, and return deleted elements, convenient later use ' ' L1 = [' Alex ', ' Wusir ', ' taibai ']pop_item = L1.pop (0) print (L1) print (Pop_item)" Remove (): Know the element name, delete by name ' L1 = [' Alex ', ' Wusir ', ' Taibai ']l1.remove (' Alex ') print (L1) ' Clear (): Empty the list contents, keep the list itself ' L1 = [ ' Alex ', ' Wusir ', ' Taibai ']l1.clear () print (L1) ' del# 1. You can delete # 2 by index. # 3 can be deleted by slice. The entire list can be deleted at the memory level # ' L1 = [' Alex ', ' Wusir ', ' Taibai ']del L1   # memory level delete del l1[0]   #按照索引删除del L1[0:3]  #按照切片删除

#改

‘‘‘
1. Follow the index to modify
2. Follow the section to modify
‘‘‘
# L1 = [' Alex ', ' Wusir ', ' taibai '] ' Change the value by index ' # l1[2] = ' God   #l1 [2] on the left side of the equals sign the index position ' ' ' ' ' ' # l1[0:2 ' = ' old boy ' # First, the contents of the slice is emptied, and then the subsequent content is iterated into # print (L1) "To modify by the Slice + step: Delete Several elements can only add a few elements"

#查

(1) by index, slice find

(2) For loop query

' # L1 = [' Alex ', ' Wusir ', ' Taibai ']# for    i ' l1:#       print (i)

# Other methods

‘‘‘
Count ()
Len ()
Index ()
Sort (): for sorting
Reverse (): Flip list
‘‘‘
General list sorting for pure numbers L2 = [5,3,6,2,4,7]l2.sort ()   #从小到大排序l2. Sort (reverse=true)   #从大到小排序print (L2) L2 = [5,3,6,2,4,7] L2.reverse () print (L2)
Three. Nesting of lists
L3 = [' Alex ', ' Wusir ', [' Taibai ', ' Ritian '], 20]# 1, find Alex's e element. # 2, turn the Wusir into uppercase. # 3, add an element to this list [' Taibai ', ' Ritian ', ' + '], ' Text Week ' # 4, ' Taibai ' first uppercase # 5, 99 through the number 1 way into 100, and put back in place. "Find Alex's e element." ' # print (l3[0][2]) ' will turn Wusir into uppercase. "' # L3[1]=l3[1].upper () # Print (L3) ' gives this list [' Taibai ', ' Ritian '] append an element, ' Text week ' ' # l3[2].append (' Text week ') # print (L3) ' Will ' Taibai ' initials ' ' # value = L3[2][0].capitalize () # Print (value) ' will change 99 by 1 to 100 and put back in place. ‘‘‘

  

  

List of DAY4 python data 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.