Python Learning Note 06-a list of tuple dictionaries

Source: Internet
Author: User

First, List
str1 = ' AHJFHAJ1KNVR '
Print (list (STR1))
Print (Type (STR1))
A = [' A ', ' 1 ', 789]
Print (a)
Print (Type (a))
#显示
# [' A ', ' H ', ' j ', ' F ', ' h ', ' A ', ' j ', ' 1 ', ' k ', ' n ', ' V ', ' R ']
# <type ' str ' >
# [' A ', ' 1 ', 789]
# <type ' list ' >

Ways to view a list
Print (dir (a))

Append Append
A.append (' World ')
Print (a)
#显示 [' A ', ' 1 ', 789, ' world ']

Delete an element at the end of a pop
Print (' = = ' *30)
A.pop ()
Print a
#显示
# ==============================
# [' A ', ' 1 ', 789]

Index qualifying list subscript, Lenovo Str's find
Print (A[0],a[1],a[2],a.index (' a '))
#显示 (' A ', ' 1 ', 789, 0)

Insert Insertion Element
A = [' A ', ' 1 ', 789, ' world ']
A.insert (0, "insert0")
Print a
#显示 [' insert0 ', ' A ', ' 1 ', 789, ' world ']
A.insert (2, "Insert2")
Print a
#显示 [' insert0 ', ' a ', ' insert2 ', ' 1 ', 789, ' world ']

Remove deletes the first matching element
A.append (' 1 ')
Print (a)
A.remove (' 1 ')
Print a
#显示
# [' Insert0 ', ' a ', ' insert2 ', ' 1 ', 789, ' 1 ']
# [' Insert0 ', ' a ', ' Insert2 ', 789, ' 1 ']

Sort sorts
Print (' sort ' *10)
Print (a)
A.sort ()
Print (a)

#显示
# Sortsortsortsortsortsortsortsortsortsort
# [' Insert0 ', ' a ', ' Insert2 ', 789, ' 1 ']
# [789, ' 1 ', ' a ', ' insert0 ', ' Insert2 ']

Reverse reverse order
Print (' reverse ' *10)
Print (a)
A.reverse ()
Print (a)
#显示
# Reverse Reverse Reverse Reverse reverse reverse reverse Reverse Reverse Reverse
# [789, ' 1 ', ' a ', ' insert0 ', ' Insert2 ']
# [' Insert2 ', ' insert0 ', ' A ', ' 1 ', 789]

Slice
#索引从0开始, the right index number takes that number-1
Print ('--' *10 ')
Print (a)
Print (a[:])
Print (a[3:])
Print (A[1:3])
#显示
# --------------------
# [' Insert2 ', ' insert0 ', ' A ', ' 1 ', 789]
# [' Insert2 ', ' insert0 ', ' A ', ' 1 ', 789]
# [' 1 ', 789]
# [' Insert0 ', ' a ']

Print ('% ' *10)
A.append (' last ')
Print (a)
Print (A[1:5:2])
#1开始位置, 5 end position and the slice does not take the end value, 2 is the step step value, each add 2
#显示
# % % % % % % % % % %
# [' Insert2 ', ' insert0 ', ' A ', ' 1 ', 789, ' last ']
# [' Insert0 ', ' 1 ']

Two, the meta-group
A list that is equivalent to an immutable element
str1= ' MNZXOLUIQRNKLCX '
Print (tuple (STR1))
Print (Type (tuple (STR1)))
#显示
# (' m ', ' n ', ' z ', ' x ', ' O ', ' l ', ' u ', ' i ', ' Q ', ' R ', ' N ', ' K ', ' l ', ' C ', ' X ')
# <type ' tuple ' >

A single tuple element is followed by a comma after the element, or the Python interpreter is not recognized as a tuple type
m= (' ABCD ')
Print (Type (m))
#显示 <type ' str ' >

n= (' ABCD ',)
Print (Type (n))
#显示 <type ' tuple ' >

x= (456)
Print (type (x))
Y= (456,)
Print (Type (y))
#显示
# <type ' int ' >
# <type ' tuple ' >

Methods of tuple
Print (dir (x))

Count counts the number of an element
Tul = (' A ', ' C ', ' t ', ' I ', ' C ', ' t ')
Print (_tul.count (c))
#显示2

Index returns the subscript for the first occurrence of an element
Print (Tul.index (' C '))
#显示1
Print (Tul.index (' h '))
#元素不存在时报错
#print (Tul.index (' h '))
#ValueError: Tuple.index (x): X not in tuple

Third, the dictionary
is a variable container model that can store any type of object
Key:value form
The key value is separated by a colon, and the key value pair is delimited by commas, and the entire dictionary is included in curly braces {}


Dictionary Assignment Method 1
k = {' name ': ' Chris ', ' weight ': ' 136 ', ' Age ': 20}
Print (k)
Print (type (k))
#显示
# {' Age ': ' ' name ': ' Chris ', ' weight ': ' 136 '}
# <type ' Dict ' >

Dictionary Assignment Method 2
K1 = dict (a = 1, b = 3, c = 6, F = 9)
Print (K1)
#显示
# {' A ': 1, ' C ': 6, ' B ': 3, ' F ': 9}

Dictionary copy mode 3 (not used)
K3 = Dict ([' Name ', ' Chris '), (' weight ', 120)])
Print (K3)
#显示
{' name ': ' Chris ', ' Weight ': 120}

Common methods of dictionaries
Print (dir (K3))

Clear Erase Content
K1.clear ()
Print (K1)
#显示
# {}

#get方法
Print (K3.get (' name '))
Print (K3.get (' weight '))
Print (K3.get (' job '))
#显示
# chris
# 120
# None

#setdefault方法
#如果有value, the original value is obtained and if it is empty, the assigned value is taken
Print (K3.setdefault (' name '))
Print (K3.setdefault (' weight ', ' 999 '))
Print (K3.setdefault (' job ', ' Engineer '))
#显示
# chris
# 120
# Engineer

#keys方法
Gets all the key values of the dictionary,
Print (K3.keys ())
#扩展学习itemkeys

#values方法
Get all value values for a dictionary
Print (K3.values ())
#扩展学习itemvalues

#iteritems方法
Print (K3.iteritems ())
#显示
# <dictionary-itemiterator Object at 0x0000000002e9bef8>

For E, F in K3.iteritems ():
Print (E, f)
#显示
(' Job ', ' Engineer ')
(' name ', ' Chris ')
(' weight ', 120)

#items方法
Print (K3.items ())
#显示
[(' Job ', ' Engineer '), (' Name ', ' Chris '), (' weight ', 120)]

Recommend the use of iteritems in development, can make better use of system resources, development efficiency is higher
Take one with one, take it with you, do not remove it all at once
Iterkeys,itervalues use similar,

#update方法

#pop方法
K3 = {' job ': ' Engineer ', ' name ': ' Chris ', ' Weight ': 120}
Print (K3)
K3.pop (' name ')
Print (K3)
#显示
# {' Job ': ' Engineer ', ' name ': ' Chris ', ' Weight ': 120}
# {' Job ': ' Engineer ', ' weight ': 120}

K3.pop (' 120 ')
Print (K3)
#显示报错
#pop只能删除key, cannot be used with value

Advanced Operations for Dictionaries
Fromkeys usage
p = [' A ', ' B ', ' C ', ' d ']
m = {}
n = M.fromkeys (P, 123)
n = Dict.fromkeys (P, 123)
Print (n)


#zip函数, overlay two list elements into a dictionary
P1 = [' A ', ' B ', ' C ', ' d ']
P2 = [1, 2, 3, 4]
Dict_test = Dict (Zip (P1, p2))
Print (dict_test)
#显示
# {' A ': 1, ' C ': 3, ' B ': 2, ' d ': 4}

#update to overlay two dictionary elements
Print (K3)
Dict_test.update (K3)
Print (dict_test)
#显示
# {' A ': 1, ' C ': 3, ' B ': 2, ' d ': 4, ' weight ': ' Job ': ' Engineer ', ' name ': ' Chris '}

#对字典进行排序
MM = Dict (A=1, b=10, c=3, d=9)
Print (mm)
Print sorted (Mm.iteritems (), key = Lambda d:d[0], reverse = False)
#显示
# [(' A ', 1), (' B ', ten), (' C ', 3), (' d ', 9)]

This article from "Crazy Little Monk" blog, reproduced please contact the author!

Python Learning Note 06-a list of tuple 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.