Python through comparison (3): dictionary class, string

Source: Internet
Author: User

Dictionary
Create a dictionary
AB = {"key1": "A", "key2": "B", "key3": "C "}, this statement creates a dictionary and adds three dictionary items to the dictionary. Note that the key here can only be a constant class, that is, the value of the content cannot be changed when a dictionary is created, for example, if you use a list object as the key, an error is returned.

Add or modify a dictionary
AB ["key4"] = "D"

Copy dictionary objects
AB. Copy ()
Are you sure you want to perform a deep copy or a latent copy of this function? That is, transfer the application or pass the value?
We can make a test.
>>> Dict = {1: 'A', 2: 'B '}
>>> Dict2 = dict. Copy ()
>>> Dict2
{1: 'A', 2: 'B '}
>>> Dict
{1: 'A', 2: 'B '}
>>> Dict [2] = 'C'
>>> Dict
{1: 'A', 2: 'C '}
>>> Dict2
{1: 'A', 2: 'B '}
It can be seen that the deep copy is different from the. NET copy, and the. NET copy is the transfer value by default.

Sequence Interface

Sequence is the interface of the above collection (list, tuple, dictionary) class, defining the functions of the indexer and slice. For the indexer. netProgramFor example, in list and tuple, we can use the list [Index] method to access the values in the list. This is very similar.. Net indexer. Slice is a part of the returned list.
For example
>>> A = [1, 2, 3, 4, 5]
>>> A [1: 3]
[2, 3]
Through a [], we return the sublist from the second element to the fourth element in list A. What if we want to return all the data in a list? Through a [:], we can return all data of A. Note that this is actually a deep copy instead of a reference copy.
Dict1 = dict2 [:]

Python does not provide the interface,. net and Java programmers, it may be easier to understand the sequence expression as an interface, and in terms of code implementation, Python. in the. NET version, the list object is inherited from an interface called isequence.

Public class tuple: isequence, icollection, ienumerable, ienumerable <Object>, ivalueequality, ilist <Object>, icodeformattable, iparametersequence

Isequence defines the indexing and slice Methods

For Python interface resources, see

Http://blog.csdn.net/pjeby/archive/2007/03/18/1532666.aspx

String type
String Initialization Method

STR = "My name is ZY"
You can also use single quotes
STR = 'My name is ZY'
You can also use three quotation marks.
STR = ''' sent to and sent to non '''

Other methods can be seen through help (string). I will not repeat it.
Here are some special usage instructions.
String. startswith (STR): indicates whether the string starts with a certain string. If yes, true is returned.
String. Find (STR): searches for a string in the string and returns the true result.
If 'str' in name: This is another way of writing the above query statement.
String connection, similar.. Net string join method, Python strings can also be connected to an array to form a long string, but note the delimiter here ,. net, we use the static method of string, String. join (string separator, string [] list). The first parameter sets the delimiter after the connection and the last parameter sets the array to be connected. However, since Python does not have a static method, so he is a little different. Let's look at the following statement:
Delimiter = '_*_'
Mylist = ['Brazil ', 'Russia', 'India ', 'China']
Print delimiter. Join (mylist)

The connector becomes the subject of the sentence. Do you notice it?

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.