Python数据类型str、list、tuple、dict、set

字符串
  • 字符串常见的功能
name = 'fe_cow'

print name.capitalize()  # 首字母大写 :Fe_cow

print name.count('_')    # 统计字符串出现在这个字符中的个数: 1

print name.center(10, '*') # 打印10个字符, 不够用"*" 补齐 :**fe_cow**  (如果第二个参数为空的话, 用空格补齐)

print name.endswith("w")  # 判断字符串是否 "w" 结尾 : True(返回的是布尔值)

print name+'666'.strip('666')  # strip 去掉后面的字符串数字'666': fe_cow

print name.split('_')  # 以"+"为分隔符生成新的列表,默认不的话以'空格':['fe', 'cow']

print name.replace('f', 'F')  # 将字符串'f'替换'F': Fe_cow

print "250".isdigit()  # 判断字符是否为整数 :True

print '+'.join(['1', '2', '3'])  # join将后面的内容添加到前面的字符串中, 以"+" 分割:1+2+3
列表
  • 列表常见功能
name = ['Au_cow', 'Ag_cow', 'Cu_cow', 'Fe_cow']

# 切片
print name[1]   # 获取列表索引为1:Ag_cow
print name[1:3]  # 切片:索引1-2 ['Ag_cow', 'Cu_cow'] 返回的是列表
print name[-1]  # 代表从后往前取值:Fe_cow
print name[:2]  # 取0 1 那就是:['Au_cow', 'Ag_cow'] 返回的是列表

# 添加
name.append('Ai_cow')  
print name  # 列表添加:['Au_cow', 'Ag_cow', 'Cu_cow', 'Fe_cow', 'Ai_cow']

# 插入 0代表插入的位置
name.insert(0, 'Ai_cow')  
print name  # ['Au_cow', 'Ag_cow', 'Cu_cow', 'Ai_cow']

# remove删除指定元素
name.remove('Ai_cow')  #['Au_cow', 'Ag_cow', 'Cu_cow', 'Fe_cow']
print name

# pop删除最后一个元素
name.pop()  
print name  # ['Au_cow', 'Ag_cow', 'Cu_cow', 'Fe_cow']

# del [] 删除指定的元素
del name[4]  
print name  # ['Au_cow', 'Ag_cow', 'Cu_cow', 'Ai_cow']
  • 列表功能扩展
# 扩展
name_one = ['Au_cow', 'Ag_cow']
name_two = ['Fe_cow', 'Ai_cow']

# 添加 extend
name_one.extend(name_two)
print name_one  # ['Au_cow', 'Ag_cow', 'Fe_cow', 'Ai_cow']

# 统计 count
print name_one.count('Au_cow')  # 1

# 排序 sort
name_two.sort()
print name_two  # ['Ai_cow', 'Fe_cow']

# 排序 reverse
name_one.reverse()
print name_one  # ['Ag_cow', 'Au_cow']

# 获取下标 index
print name_one.index('Au_cow')  # 0

# 同时获取下标和值  enumerate
for index, item in enumerate(name_one):
    print index, item  # 0 Au_cow; 1 Ag_cow
元组
  • 元组常见功能
# 创建元组
name = ('Au_cow', 'Ag_cow', 'Cu_cow', 'Fe_cow')

# 常见功能
print name.count('Au_cow')  # 1
print name.index('Ag_cow')  # 1 
字典
  • 字典常用功能
# 创建
name = {'name': 'fe_cow', 'age': 18, 'gender': 'man'}

# 增加
name['height'] = 180.00
print name  #{'gender': 'man', 'age': 18, 'name': 'fe_cow', 'height': 180.0}

# 修改
name['height'] = 175.00
print name  # {'gender': 'man', 'age': 18, 'name': 'fe_cow', 'height': 175.0}

# 删除
name.pop('height')
print name  # {'gender': 'man', 'age': 18, 'name': 'fe_cow'}

# 查找value
print name.get('name')  # fe_cow

# 循环 字典.items()
for key, value in name.items():
    print key, value   # gender man ; age 18;name fe_cow

#  查找出key
for key in name.keys():
    print key  # gender; age ; name

# 查找出value
for value in name.values():
    print value  # man; 18 ; fe_cow
集合
  • 集合常用功能
# 创建集合
name = set(['Au_cow', 'Ag_cow', 'Cu_cow'])
print name  # set(['Au_cow', 'Ag_cow', 'Cu_cow'])

# 添加(可以看出打印出来的顺序是无序的)
name.add('Ai_cow')
print name  # set(['Au_cow', 'Ag_cow', 'Ai_cow', 'Cu_cow'])

# 添加多个集合 update
name.update(['Fe_cow', 'men~'])
print name  # set(['men~', 'Au_cow', 'Fe_cow', 'Cu_cow', 'Ag_cow', 'Ai_cow'])

# 删除 remove
name.remove('men~')
print name  # set(['Au_cow', 'Fe_cow', 'Cu_cow', 'Ag_cow', 'Ai_cow'])

# 随机删除pop()
name.pop()
print name  # set(['Fe_cow', 'Cu_cow', 'Ag_cow', 'Ai_cow'])
  • 集合功能拓展
name_one = set(['Au_cow', 'Ag_cow', 'Cu_cow'])
name_two = set(['Cu_cow', 'Fe_cow', 'Ai_cow'])

# 并集 union
print name_one.union(name_two)  # set(['Au_cow', 'Fe_cow', 'Ag_cow', 'Ai_cow'])

# 差集 difference
print name_one.difference(name_two)  # set(['Au_cow', 'Ag_cow'])

# 交集  intersection
print name_one.intersection(name_two)  # set(['Cu_cow'])

猜你喜欢

转载自blog.csdn.net/Fe_cow/article/details/80643488