Python Automated test Aries-WEEK3 Dictionary

Source: Internet
Author: User

#和列表一样, it's just immutable variables.
#定义元组时, if the tuple has only one element, add a comma after the element, or it will be a string
Lis = [] #列表这样定义
TP = (' 127.0.0.1 ', ' 3306 ')
#lis [0]= ' 3306 ' #不能这样写 because this list is empty, subscript 0 does not exist, use append or insert
Print (LIS)
Print (TP)

TP = (' 127.0.0.1 ', ' 3306 ') #元组
names = [Lingyul, ' Panyang '] #列表
name = ' Lingyul,cyc '
New_lis = tuple (LIS)
Print (Type (new_lis))

#强制类型转换的int float str list tuple




#字符串, List, dictionary
#字典是无序的
#定义一个字典
info = {
' Name ': ' Lingyul ',
' Age ': 18,
' Sex ': ' Woman ',
' ID ': ' 1 ',

}
#查询
Print (info[' name '])
Print (Info.get (' id '))
Print (Info.get (' addr ')) #get方法取值找不到时会返回none # # # # # # # #运行结果为none
Print (Info.get (' addr ', ' Beijing ')) #如果get取不到值, return the value that follows #运行结果为beijing
#这两个的区别是: Use [] to get a value if the value does not return an error message
#get方法取值找不到时会返回none

#字典的取值就这两种方法

#增
info[' addr '] = ' Beijing '
Print (Info.get (' addr '))

Info.setdefault (' phone ', 110110)
Print (info)

#修改
info[' id ']=7 #这种方法既能新增也能修改, in case there is an ID that deserves to be modified, in the absence of this worthy case will be added
Print (info)

#删除
del info[' addr ']
Print (info)

#字典是无序的, so you must specify what to delete when you delete with pop
Print (Info.pop (' phone '))
Print (info)

Info.popitem () #随即删除一个元素

Info.clear () #清空字典

#????????????????????????????????????????????????????????????????????????????
New_infos = [
{
' YANGWB ':
{
' ID ': 1,
' Sex ': ' Mans ',
' Phone ': 250,
' Addr ': ' huoying '

},
' Yangwn ':
{
' ID ': 2,
' Sex ': ' Mans '
}

}

]

Print (New_infos[0].get (' YANGWB '). Get (' addr '))




all={
' Car ':
{
' Color ': [' red ', ' yellow ', ' black '],
' Moeny ': 111111,
' Pailiang ': ' 2.5L ',
' Name ': ' BMW '


},
' Car1 ':
{


},
' Car2 ':
{



}

}

Print (All.keys ()) #获取字典的所有key
Print (All.values ()) #获取所有的value
Print (' Items is: ', All.items ()) #同时获取字典的key和values, circular use

Info2 = {
' Name ': ' Lingyul ',
' Age ': 18,
' Sex ': ' Woman ',
}
For k,v in Info2.items ():
Print ('%s is%s: '% (k,v)) # The purpose of the items method is to take both key and value to the loop
#如果数据比较大时, it will be very slow, it is first to take the dictionary out into a string, the value of the string is taken

#这种效率比较高
For K in Info2:
Print (' k is: ', K)
Print (Info2[k])


#把两个字典合并在一起, if you have the same key, update the value
Info.update (Info2)

#python2中判断key是否存在, not in Python3.
Info.has_key (' name ')
Print (' name ' in info) #python3中直接用in判断是否存在某个key值


Python Automated test Aries-WEEK3 Dictionary

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.