Python 数据基本类型



列表python

序列是Python中最基本的数据结构。序列中的每一个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。数据库

Python有6个序列的内置类型,但最多见的是列表和元组。api

序列均可以进行的操做包括索引,切片,加,乘,检查成员。数据结构

此外,Python已经内置肯定序列的长度以及肯定最大和最小的元素的方法。app

列表是最经常使用的Python数据类型,它能够做为一个方括号内的逗号分隔值出现。ide

列表的数据项不须要具备相同的类型函数

如:[11,22,33]、['yefei', 'wusiqi']url

list1 = ['Google', 'Runoob', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

访问列表中的值spa

使用下标索引来访问列表中的值,一样你也可使用方括号的形式截取字符,以下所示:code

list1 = ['Google', 'Runoob', 1997, 2000];

list2 = [1, 2, 3, 4, 5, 6, 7 ];

print ("list1[0]: ", list1[0])

print ("list2[1:5]: ", list2[1:5])

以上实例输出结果:

list1[0]:  Googlelist2[1:5]:  [2, 3, 4, 5]

更新列表

list = ['Google', 'Runoob', 1997, 2000]
print ("第三个元素为 : ", list[2])
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])

以上实例输出结果:

第三个元素为 :  1997更新后的第三个元素为 :  2001

删除列表

del()

list = ['Google', 'Runoob', 1997, 2000]
print (list)
del list[2]
print ("删除第三个元素 : ", list)
del list      #删除列表 整个元素

remove删除指定方法

remove()
删除制定的列表元素
a = [1,2,3]
a.remove(2)
print (a)
返回值
[1,3]


列表支持拼接操做:

list1 = [1,2,3]
list2 = [4,5,6]
list = list1 + list2
print (list)

输出的值为
[1,2,3,4,5,6]


嵌套列表

a = [1,2,3,4,5]
n = [6,7,8]
x = [a,n]
[[1,2,3,4,5],[6,7,8]]

Python列表函数&方法

len(list) 列表元素个数
max(list) 返回列表元素最大值
min(list) 返回列表元素最小值
list(seq) 将元祖转换为列表
list.append(obj) 在列表末尾添加新的对象
list.count(obj) 统计某个元素在列表中出现的次数
list.extend(seq) 在列表末尾一次性追加另外一个序列中的多个值(用新列表扩展原来的)
list.index(obj) 从列表中找出某个值第一个匹配项的索引位置
list.insert(index,obj)
将对象插入列表
list.pop(obj=list[-1]) 移除列表中的一个元素(默认最后一个元素,而且返回该值)

list.reverse() 反向列表中的元素
list.sort() 对原列表进行排序
list.clear() 清空列表
list.copy() 复制列表


字典

如:{'name': 'wupeiqi', 'age': 18} 、{'host': '2.2.2.2', 'port': 80]}

ps:循环时,默认循环key

dic = {'k1':'v1','k2':'v2'} 
dic = dict(k1='v1',k2='v2')

这两个dic 是相等的


set集合

set是一个无序且不重复的元素集合

练习:寻找差别

# 数据库中原有
old_dict = {
    "#1":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 },
    "#2":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 },
    "#3":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 }
}
  
# cmdb 新汇报的数据
new_dict = {
    "#1":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 800 },
    "#3":{ 'hostname':c1, 'cpu_count': 2, 'mem_capicity': 80 },
    "#4":{ 'hostname':c2, 'cpu_count': 2, 'mem_capicity': 80 }
}

1  原来没有 -》 新加入

2, 原来有 -》 更新

3, 新无,原来有 -》 原来删除

三个列表:

      要更新的数据

 要删除

 要添加

old = set(old_dict.keys())
new = set(new_dict.keys())

#更新数据   交集
update_set = old.intersection(new)
print(update_set)

#删除老的数据  添加新的数据   差集
delete_set = old.symmetric_difference(update_set)
print(delete_set)
add_set = new.symmetric_difference(update_set)
print(add_set)


#差集
#def difference() 循环old里面的元素 若是update_set有的就删除
#delete_set = old.difference(update_set)
#add_set = new.difference(update_set)

#差集
#delete_set = old.difference(new)

#add_set = new.difference(old)


计数器(counter)

Counter是对字典类型的补充,用于追踪值的出现次数。

ps:具有字典的全部功能 + 本身的功能

c = Counter('abcdeabcdabcaba')
print c
输出:Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
import  collections

#打印出元素的个数
obj = collections.Counter('adsdssdfsfsdsdfsdfsd')
print(obj)

#查看前4位元素
ret = obj.most_common(4)
print(ret)

#循环key值
for k in obj.elements(): 
    print(k)

#循环key值 和 键值
for k,v in obj.items():
    print(k,v)
 
#update方法    
obj = collections.Counter(['11','12','12','14'])
print(obj)
#添加
#obj.update(['eric','11','11'])
#print(obj)
#删除
obj.subtract(['eric','11','11'])
print(obj)
返回值
Counter({'12': 2, '14': 1, '11': 1})
Counter({'12': 2, '14': 1, 'eric': -1, '11': -1})


有序字典(orderedDict )

orderdDict是对字典类型的补充,他记住了字典元素添加的顺序

dic = collections.OrderedDict()
#dic = dict()   若是是基本的字典类型  输出就是无序的
dic['k1'] = 'v1'
dic['k2'] = 'v2'
dic['k3'] = 'v3'
print(dic)

#默认值
dic['k4'] = None   这两个相同   dic.setdefault('k4')

#popitem 删除最后一个key
dic.popitem()
print(dic)

#move_to_end 把指定的key 最后输出
dic.move_to_end('k1')
print(dic)

#pop 指定删除 而后删除的信息打印出来
ret = dic.pop('k2')
print(dic)
print(ret)

#update更新  添加元素到原先的字典当中 而后相同的key会自动更新
dic.update({'k1':'v111','k10':'v10'})
print(dic)

默认字典

defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。

有以下值集合 [ 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 90. ..],将全部大于  66  的值保存至字典的第一个key中,将小于  66  的值保存至第二个key的值中。
即: { 'k1' : 大于 66  'k2' : 小于 66 }
values = [11,22,33,44,55,66,77,88,99]
my_dict = collections.defaultdict(list)
for value in values:
    if value > 66:
        my_dict['k1'].append(value)
    else:
        my_dict['k2'].append(value)


print(my_dict)

返回值
defaultdict(<class 'list'>, {'k2': [11, 22, 33, 44, 55, 66], 'k1': [77, 88, 99]})


可命名元祖

根据nametuple能够建立一个包含tuple全部功能以及其余功能的类型。

练习

MytupleClass = collections.namedtuple('Mytuple',['x', 'y', 'z'])

obj = MytupleClass(11,22,33)

print(obj.x)
print(obj.y)
print(obj.z)

之后写坐标能够用到

相关文章
相关标签/搜索