灵虚御风
醉饮千觞不知愁,忘川来生空余恨!

导航

 
"""
整型
浮点型
字符串
列表
字典
集合
元组
布尔值
"""

"""具名元组"""
# 想表示坐标点x为1 y为2的坐标
from collections import namedtuple

point = namedtuple('坐标',['x','y','z']) # 第二个参数既可以传可迭代对象

# point = namedtuple('坐标','x y z') # 也可以传字符串 但是字符串之间以空格隔开

# p = point(1,2,5) # 注意元素的个数必须跟namedtuple第二个参数里面的值数量一致
# print(p)
# print(p.x)
# print(p.y)
# print(p.z)

# card = namedtuple('扑克牌','color number')
# # card1 = namedtuple('扑克牌',['color','number'])
# A = card('♠','A')
# print(A)
# print(A.color)
# print(A.number)


# city = namedtuple('日本','name person size')
# c = city('东京','R老师','L')
# print(c)
# print(c.name)
# print(c.person)
# print(c.size)


"""队列:先进先出"""

import queue
q = queue.Queue() # 生成队列对象

q.put('first')# 往队列中添加值

# q.put('second')
# q.put('third')

# print(q.get()) # 朝队列要值
# print(q.get())
# print(q.get())
# print(q.get()) # 如果队列中的值取完了 程序会在原地等待 直到从队列中拿到值才停止

# deque 双端队列
from collections import deque
d = deque(['a','b','c'])
"""
append
appendleft

pop
popleft
"""

d.append(1)
d.appendleft(2)
"""
队列不应该支持任意位置插值
只能在首尾插值(不能插队)
"""
d.insert(0,'hahah') # 特殊点:双端队列可以根据索引在任意位置插值

# print(d.pop()) # 1
# print(d.popleft()) # hahah


normal_d = dict([('a',1),('b',2),('c',3)])
print(normal_d) # {'a': 1, 'b': 2, 'c': 3}

from collections import OrderedDict
order_d = OrderedDict([('a',1),('b',2),('c',3)])
# print(order_d) # OrderedDict([('a', 1), ('b', 2), ('c', 3)])
order_d1 = OrderedDict()
order_d1['x'] = 1
order_d1['y'] = 2
order_d1['z'] = 3
# print(order_d1) # OrderedDict([('x', 1), ('y', 2), ('z', 3)])



for i,j in order_d1.items():
print(i,j)
# x 1
# y 2
# z 3

# order_d1 = dict()
# order_d1['x'] = 1
# order_d1['y'] = 2
# order_d1['z'] = 3
# print(order_d1)
# for i in order_d1:
# print(i)



from collections import defaultdict

values = [11, 22, 33,44,55,66,77,88,99,90]

my_dict = defaultdict(list) # # 后续该字典中新建的key对应的value默认就是列表
print(my_dict['aaa']) # []
for value in values:
if value > 66:
my_dict['k1'].append(value)
else:
my_dict['k2'].append(value)
print(my_dict)
"""
defaultdict(<class 'list'>, {'aaa': [], 'k2': [11, 22, 33, 44, 55, 66], 'k1': [77, 88, 99, 90]})
"""
# my_dict1 = defaultdict(int)
# print(my_dict1['xxx'])
# print(my_dict1['yyy'])
#
# my_dict2 = defaultdict(bool)
# print(my_dict2['kkk'])
#
# my_dict3 = defaultdict(tuple)
# print(my_dict3['mmm'])


from collections import Counter
s = 'abcdeabcdabcaba'
res = Counter(s)
print(res) # Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})

for i in res:
print(i)
# 先循环当前字符串 将每一个字符串都采用字典新建键值对的范式

# d = {}
# for i in s:
# d[i] = 0
# print(d)

posted on 2022-03-24 17:03  没有如果,只看将来  阅读(13)  评论(0编辑  收藏  举报