[Python note 2] list, string, dictionary (list?string?map?)

Source: Internet
Author: User
Tags shallow copy

1. List

1) List initialization in Python

     list=[0,1,2,3,4,5,6,7,8,9]

Variable name =[,,,,], of course the initial list can be empty

PS: See [], familiar with C + + students can not help but think of the familiar array, then python in the list and what is the difference between the group?

In Python we can still use subscripts to manipulate lists, just like manipulating arrays. However, if Python only provides me with such a simple (Keng) single (die) operation, how can it be shown that Python's profile is powerful?

2) sharding

sharding refers to the use of an index to specify the scope of an access list to access multiple elements, rather than just one element.

List=[0,1,2,3,4,5,6,7,8,9]print list[1:5]print list[8:20]print list[-5:]print list[:-5]print list[:]print list[1:5:2 ]print list[::-2]list[1:5]=[11,12,13,14]print list[:]

With a few tests, you probably understand the use of shards. Where shards have three parameters, and the third parameter is the step default is 1

3) List method

1, append (x) add element x after the list

2, COUNT (x) counts the number of elements x

3, Extend (x) Add a new list after the list (return to source list)

4. Index (x) calculates the indexes of element x

5. Insert (Index,value) inserts the element value at index

6. Pop (index) deletes the element at index and returns, deleting the last element by default

7, remove (x) Delete element x, no return value

8. Reverse () reverse list

9. Sort ()

2. String

1) string formatting

Name =raw_input (' please input your name: ') print ' Hello%s! '% ( Name) number=1.123print '%10.5f '% (number) print '%-10.5f '% (number)


2) String method1. Find (x) finds the position of x in a longer string and returns the leftmost index2, lower (x) convert x to lowercase3, replace (x, y) replaces all x in the string with y4, Split (x) use X to cut the string, return a list5. Join (x) use X to connect all the elements in a list6, Strip () remove the left and right sides of the string space3. Dictionaries       1) Initialization of the dictionary         
<span style= "FONT-SIZE:14PX;" >        dict={' first ': 1, ' second ': 2, ' third ':3}</span>
The variable name ={key1:value1,key2:value2 ...}2) Dictionary method1. Clear () Clears all elements of the dictionary2, copy () replication (shallow copy)3, Fromkeys (x) based on the elements of the list x to build a dictionary, the key value is the list of each element, value is None4. Get (key) to find value based on key5. Hash_key (x) find if the dictionary contains a key value x6. Update (x) updating another dictionary with one dictionary7, Popitem () delete the last element and return8, Pop (key) delete the key value corresponding to the element9. Items () Converts the dictionary to a list and returns10, Iteritems () Convert dictionary to list, return iterator11. Key () converts the key in the dictionary to a list and returns12, Iterkey () converts a key in the dictionary to a list, returns an iterator13. VALUES () converts the value in the dictionary to a list and returns14, Itervalues () converts values in a dictionary to a list, returns an iterator               There are some methods that are common to strings, lists, and dictionaries, and are not listed separately.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Python note 2] list, string, dictionary (list?string?map?)

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.