pythonComments

python概念转换dictionary、tuple、和 list

1.**Dictionar **-->Object json

py
d = {"server":"mpilgrim", "database":"master"}

Dictionary 的 key 是大小写敏感的.

py
d["server"]

del d["server"] #删除单个

d.clear()  #清空

2.List-->String Array


py

li = ["a", "b", "mpilgrim", "z", "example"]
li[0] #start
li[-1] #end
li[-3] == li[5 - 3] == li[2]
li[1:3]

==>['b', 'mpilgrim']

py

li[1:-1]

==>['b', 'mpilgrim', 'z']

py

li[:3]  # ==>li[0:3]
li[3:] #==>li[3:len(li)]
li[:] # ==>li[0:len(li)]

py
li.append("new")   #then   remove('new') ==> unchange

//==>['a', 'b', 'mpilgrim', 'z', 'example', 'new']

py
li.insert(2, "new")

//==>['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']

py
li.extend(["two", "elements"])

# ==> 

li.append("two")
li.append("elements")


搜索 list index -->indexOf

py
li.index("example")
# //==>+number
py
li.index("c") #//==>  error

"c" in li  #//==>False

py
li +=['cc','bb']

li =li+ li

li = li*2

Tuple是不可变的 list

深入 Python :Dive Into Python 中文版

原创文章,版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0

Comments