Python Growth Notes-Basics (iii) Python list tuples, dictionaries, collections

Source: Internet
Author: User
Tags delete key shallow copy python list

The content of this section

    1. list, tuple operations
    2. String manipulation
    3. Dictionary operations
    4. Collection operations
    5. File operations
    6. Character encoding and transcoding

I. operation of lists and tuples

The list is one of the most commonly used data types in the future, and the list allows for the most convenient storage, modification and other operations of the data.

Definition List

names = ["Wang", "HouZi1", "Niuniu", "Zhouyi", "HouZi2", [up]]

You can get the values in the list by the way of the table corners

Print (names)
Print (Names[0]) #获取names中0角标元素的值
Print (names[0],names[1]) #获取names中0/1 The value of the angle element
Print (Names[5])
Print (Names[-1]) # Take the last one, reverse the value
#print (Names[0:3]) #切片 disregard the end of the head,
#print (Names[1:3]) #切片 disregard the end of the head,
#print (Names[:3]) #切片 ignore the end, the first is 0 can be omitted
#print (names[-1:-3]) #切片只能从左往右取, wrong
#print (Names[-6:-1])
#print (names[-6:]) #切片中0和-1 can be omitted
#print (names[:])
#print (names[0:])
#print (names[-1:])
#print (Names[0:-1])
#print (Names[:-1])
#print (names[-6:])
#print (Names[0::2]) #步长
#print (Names[::2]) # Every other cut can omit 0-1 from the head cut to the tail

‘‘‘
#插入操作
Names.append ("Meizi") # Append: in the last insert
Print (names)
Names.insert (1, "Xiaoqian") #在原来第二个位子插入
Print (names)
‘‘‘

#修改
Names[1]= "Qianqian" # Modify the second position of the person
Print (names)

#delete
Names.remove ("Wangsen")
Del Names[0]
Names.pop (0)
Print (Names.index ("Houzi"))
Print (Names[names.index ("Houzi")])
Print (Names.count ("Houzi"))

Introduce a special method extend ()

Watch the little practice.

Num=[1,2]num1=[3,4]num.extend (NUM1) print (num) # Printing results for [1, 2, 3, 4]

It can be seen that the function of this method is to extend the list num1 to num

In the introduction of the lower-depth copy

Shallow copy

Shallow copy copies only one layer, not the deep copy

If the list contains a small list copy of his memory address, changing the value in the small list will change

Take a look at the following exercises

Name1=["A", "B", "C", [1,2]]name2=name1.copy () Name1[-1][1]=1000print (name1,name2)

Through the above knowledge point we can know that the printed name1 and name2 will become 1000 because the list contains a small list copy of his address, so as long as the change of a place, as long as the point to his will become 1000

3. Tuples

Tuples are actually similar to the list, but also to save a group of numbers, it is not once created, it can not be modified, so it is called a read-only list

It has only 2 methods, one is count, the other is index, complete.

# format name= (1,2,3,3) Name.count (3) # Gets the number of 3 in this tuple Name.index (1) # gets 1 The position of this element in the tuple

4. String manipulation methods

Name= ' Houyafan ' Name.capitalize () # The first letter of the string Name.count (' a ') # A in the string of occurrences Name.center (50, '-') # Print 50 characters altogether Not enough-fill in name.endswith (' an ') # to determine whether to return the true or false Name.startswith (' ho ') in an end # to determine if the position of the string is to return the true or false name.find (' a ') # If there are multiple identical, left Start the first name.format_map ({}) # Pass the dictionary with no Egg Name.isalnum () # contains the number or the letter any one will return true otherwise return False Name.isalpha () # As long as all are plain English characters return true Otherwise returns false Name.isdecimal () # To determine whether the number is 10 binary name.isdigit () # To determine if it is an integer ' variable '. Isidentifier () # To determine if a valid variable is a return true Otherwise returns false Name.islower () # Whether the lowercase character is returned true otherwise returns False Name.isnumeric () # Whether it is just a number consisting of ==isdigit no OVA with Name.isspace () # Whether it is a space is returned true otherwise return false NA Me.istitle () # The first letter of all Words name.isupper () # Determines if all uppercase in English is returned true otherwise returns false ' + '. Join ([' 1 ', ' 2 ', ' 3 ') # Add the characters to the middle of the string to add anything to the output 1+2+ 3name.ljust (50, ' * ') # output not enough 50 at the end plus *name.rjust (50, ' * ')) # output is not enough 50 in front plus *name.lower () # Uppercase to lowercase name.upper ()) # lowercase to uppercase Name.lstrip () # Default remove space from the left or enter Name.rstrip ()) # Default does not write from the right to remove the space or enter Name.strip () # Remove the carriage return or the Space Name.replace (' 1 ', ' h ', count=1) # 1 Replace H Count=1 represents left-to-right number first name.rfind (' O ') # from left to right to find the rightmost o ' Corner position name.split (") # string generates the list ' 1+2+3 ' by space.Splitlines () # Auto-Identify newline name.swapcase () # Uppercase in English letters, lowercase with lowercase, uppercase Name.zfill (50)) # 50 characters not enough 0 fill in 

Above is how all the strings are manipulated

Introduce a big thing, if you want to encrypt your own string when you write the program, you can do this by using the following method:

5. Dictionaries

The dictionary is unordered and there is no subscript to find by key
dict[' 1 ']= ' houyafan ' # Modify dict[' 4 ']= ' Yuanyuan ' # without this key on new del dict[' 1 '] # Delete this key and Valuedict.pop (' 2 ') # Specify Delete key is ' 2 ' of 1 pairs of dictionaries Dict.popitem () # Random Delete no eggs with dict.get (' 1 ') # Safe to get value there is a return without a noneprint (' 1 ' in  

The cycle of a dictionary

For i in Dict:    print (i,dict[i]) # loop his key I  corresponding to the dict[i] corresponding value

6. Ternary operators

A = 1b = 2c = 3d = A If a>b else C # has a value equal to  if a is greater than B assigns a to D otherwise C assigns a value to D

The next day, Python, just leave a job.

To achieve the store interface for merchants and customers, merchants can add, delete, modify goods, and synchronize to the customer, customers can purchase goods, the first customer login prompt input deposit, the purchase successful exit, print the purchase of goods, while recording the balance. The second time comes in to show the balance directly. Ask to judge all kinds of abnormal situations.

Python Growth Notes-Basics (iii) Python list tuples, dictionaries, collections

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.