Python data types-lists, tuples, dictionaries

Source: Internet
Author: User
Tags iterable

* List Generation

* Meta-Group

* Dictionary

The following experiments are performed by the 2.7 release

One, List generation

1. Syntax:

Range (Starti stop step) 2.7 version

List (range (Starti stop Step)) 3.5 version

Generate a list of numbers from 1 to 10;

Print (List.pop ()); A=list (range (1,10));p rint (a) # # # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2. Generate the list by looping:

Syntax: [x for X in range (Starti,stop)]

The first "X": From the beginning

The second "X": is to the number of the first few

print ([x for X in range (1,11)]); ######[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

3. Using a list derivation to generate an even number greater than 20 within 100

print [x for x in range (0,101) if x% 2==0 and x>20]

[Expr for Iter_var in iterable if COND_EXPR]

Add judgment, only satisfies the condition content, only then puts the iterable in the ITER.

b = Range (1,11)

Print B #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

c = Range (1,11,2)

Print C #[1, 3, 5, 7, 9]



Two, the meta-group

1. What is a tuple: Also a container type, the elements of a tuple cannot be modified, and tuples use parentheses to wrap the data

2. How to create: add elements in parentheses, separated by commas

tup1= (' Kate ', ' Lisa ', 1997,2000), tup2= (1,2,3,4,5) tup3= ' A ', ' B ', ' C ', ' d ' Print (tup1);p rint (tup2);p rint (TUP3); # # (' Kate ', ' Lisa ', 1997, (1, 2, 3, 4, 5) (' A ', ' B ', ' C ', ' d ')

3. How to create an empty tuple

Tup= ()

When you include only one element in a tuple, you need to add a comma after the element

Tuples are similar to strings, and subscripts start at 0 and can be combined

4. Access to tuples:

Tuples can use subscript to ask the value

tup1= (' Kate ', ' Lisa ', 1997,2000);p rint (Tup1[0:2]); # # (' Kate ', ' Lisa ')

5. Modifying tuples

element values in tuples are not allowed to be modified, but we can concatenate combinations of tuples

eg

tup1= (' abc ', ' Def '); tup2= (' Lisi ', ' Kate '); Tup3=tup1+tup2;print (TUP3); # # (' abc ', ' Def ', ' Lisi ', ' Kate ')


6. Delete a tuple

The elements in the tuple are not allowed to be deleted, and you can delete the entire tuple with the DEL statement

del tuple;//The execution of the statement will be an error


7. Tuple operators

Combine, copy, and create a new tuple after an operation

A.len (): Displays the length of the list

B. Connection +

C. Replication

Print ((a) * # (1, 2, 3, 1, 2, 3)

D. Whether the element exists (the return value is a bool value)

Print (3 in ()); # #True

E. Iteration (looping through tuple elements)

Grammar:

For x in tuples:

Print (x)

eg

list= (' Lisi ', ' Kate ', ' Zhangsan ', ' 1900 '); for x in List:print (x); # #lisikatezhangsan1900

8. Meta-group interception

A= (' How ', ' is ', ' you ')

A[1]

A[-2]

A[1:]//are You

9. No closing separator

x,y=1,2

Print (x, y)//2.7 version

##

(1, 2)

10. Meta-set built-in functions

A.len (t): View list length

B.max (t): Maximum value in list

C.min (t): Minimum value in list

D.tuple (lists) convert a list to a tuple

Third, the dictionary

A dictionary is also known as an associative array (hash list), which consists of keys and values, paired objects.

Object = Data + method

1. Dictionary Features:

①. Unordered (element access via key)

②. Variable: Change the value by key

eg

x={' ename ': ' Lisi ', ' SX ': +, ' yw ': +, ' yy ': 60}x[' ename ']= ' Kate ' Print (x); ###{' yy ': $, ' ename ': ' Kate ', ' sx ': ' yw ': 90}

③. Heterogeneous: Supports multiple data types

④. Nesting: dictionaries, lists, tuples can appear in the values section of a key-value pair

x={' ename ': ' Lisi ', ' SX ': ' x ', ' yw ': ' yy ':(2,3 '}x[' ename ']= ' Kate ' print (×); ###{' yy ': (2, 3), ' ename ': ' Kate ', ' SX ': 100 , ' yw ': 90}

If the same key appears two times at a time, the latter value is remembered


2. Dictionary definitions:

Method One: {key1:value1,key2:value2 ... }key1 must be wrapped in quotation marks

Note: Keys in the dictionary must be unique, and values may not be unique

Method Two: Dict () built-in function, specify the health value

X=dict (ename= ' fbb ', sx=95,yw=65)

The value in the dictionary: x[' ename ']

X=dict (ename= ' fbb ', sx=95,yw=65);p rint (x[' ename '); # # #fbb

3. Modify the values in the dictionary:

Syntax: X[key]=value

Add new element: X[newkey]=newvalue

X={' A ': 1, ' B ': 2, ' C ': 3};x[' C ']=5;print (x); ###{' A ': 1, ' C ': 5, ' B ': 2}

4. Delete dictionary elements (x dictionary)

1 removing elements from a dictionary

Del x[' key ']

2. Clear all the entries in the dictionary

X.clear ()

3. Delete the entire dictionary

Del x (x is the dictionary name)

5. Variable unpacking: items () Convert dictionary to key, value as a list of tuples

Syntax: X.items ()

eg

X={' A ': 1, ' B ': 2, ' C ': 3};p rint (X.items ()); ###[(' A ', 1), (' C ', 3), (' B ', 2)]

Fetch key only: keys ()

X.keys ()

X={' A ': 1, ' B ': 2, ' C ': 3};p rint (X.keys ()); ###[' A ', ' C ', ' B ']

Value only: VALUES ()

X.values ()

X={' A ': 1, ' B ': 2, ' C ': 3};p rint (X.values ()); ###[1, 3, 2]

Has_key (): Determines whether a key exists, the return knot is a bool value

X.has_key (' key ')

eg

X={' A ': 1, ' B ': 2, ' C ': 3};p rint (X.has_key (' a ')); # # #True

Len (): Gets the number of key-value pairs in the dictionary

Len (x)

eg

X={' A ': 1, ' B ': 2, ' C ': 3};p rint (len (x)); # # #3

Update (): Merges the dictionary on the original dictionary, overwriting the same key

Syntax: X1.update (x2);

X1={' A ': 1, ' B ': 2, ' C ': 3};x2={' a ': 3, ' d ': 4, ' E ': 5};x1.update (x2);p rint (x1); ####{' A ': 3, ' C ': 3, ' B ': 2, ' E ': 5, ' d ': 4}




This article from the "Breeze with You" blog, declined reprint!

Python data types-lists, tuples, dictionaries

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.