泛前端开发

泛前端开发
python
Comments

Python概念转换dictionary、tuple、和 List


1.**Dictionar **–>Object json

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

Dictionary 的 key 是大小写敏感的.

d["server"]

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

d.clear()  #清空

2.List–>String Array



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

==>[‘b’, ‘mpilgrim’]


li[1:-1]

==>[‘b’, ‘mpilgrim’, ‘z’]


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

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

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

li.insert(2, "new")

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

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

# ==> 

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


搜索 list index –>indexOf

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

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


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

li =li+ li

li = li*2

Tuple是不可变的 list

深入 Python :Dive Into Python 中文版



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

Comments