Python 循环、列表、字典、集合的定义和常用方法

EN
EN
2023-04-01 / 0 评论 / 90 阅读 / 正在检测是否收录...

学习干货

循环


i = 1  # 声明变量
while i <= 5:
    print("while,Hello World")
    i += 1
else: # 也可以不用else
    print("while 循环结束后 i= %d" % i )

print(" ---------------------------- "  )

for k in range(5):
    print("for,hello")
print("for 循环结束后 k= %d" % k )

print(" ---------------------------- "  )


a = 1
while a <= 5:
    if( a == 3):
        print("if, a = 3")
        break
    else:
        print( "else, a = %d" %a)
    a += 1
else: # 如果break了,不会执行else
    print("while 正常结束" )
print("while 循环结束后 a= %d" % a )

列表


name_list = [ "张三","李四","王五"]
print(name_list)

# 直接修改数据
name_list[1] = "王一博"
print(name_list)

# 在指定未知插入数据 】
# insert(下标,元素)     插入数据
# append(元素)          追加数据
name_list.insert(0,"小唐插队")
print(name_list)
name_list.insert(2,"小曾插队")
print(name_list)

name_list.append("小周跟队")
print(name_list)

# del 删除指定数据
del name_list[2]
print(name_list)

name_list.append("小周跟队")
print(name_list)
# remove 删除第一次出现的元素
name_list.remove("小周跟队")
print(name_list)

# pop 删除
name_list.pop() # 删除最后一个数据
print(name_list)


name_list.pop(0)  # 删除指定数据
print(name_list)

# clear 清空数据
# name_list.clear()
# print(name_list)

name_list.append("小页跟队")
name_list.append("小页跟队")
print(name_list)

# len 列表长度
print( "列表长度 = %d" % len(name_list) )

# count 统计元素出现的次数
print(  "小页跟队 出现次数 = %d" %  name_list.count("小页跟队") )

# sort 列表排序
number_list = [ 2,1,4,3,7,5]
print(number_list)

# sort() 升序
number_list.sort()
print(number_list)

# sort(reverse=True) 降序
number_list.sort(reverse=True)
print(number_list)

# reverse 逆序 (列表当前排序反过来)
number_list.reverse()
print(number_list)

# extend 将两个列表合并
test_1 = [ 10,11,12 ]
test_2 = [ 13,14,15 ]
test_1.extend(test_2)
print(test_1)

元组


# `````````````````````````
#   元组
#   和列表类似,不同之处是 元组的 元素不能修改
#   表示多个元素组成的序列
#   在python开发中 有特定的应用场景
#   元组用()定义 info_tuple = ("张三",18,1.75) 。 只有一个数据时,需要在后面添加逗号 info_tuple = ("李四",)
#   索引从0开始
# `````````````````````````

info_tuple = ("张三",18,1.75,18)
print(info_tuple)
print( type(info_tuple) )

# count 统计元素出现的次数
print( "18出现的次数 = %d" %info_tuple.count(18))

# index 元素出现的下表
print( info_tuple.index(1.75))
print( " 元组的循环 :")
for i in info_tuple:
    print(i)

# list 转list类型
print( list(info_tuple))
# tuple 转tuple类型
print( tuple(info_tuple))

字典

# `````````````````````````
#   字典
#   通常用于存储 描述一个物体的相关数据
#   字典是无序的对象集合 ,列表是有序的对象集合
#   字典用{}定义 ,键值对方式 zhangsan = { "name" :"张三","age":18}
# `````````````````````````

zhangsan = { "name" :"张三","age":18}
print(zhangsan)
print( type(zhangsan))

zhangsan['height'] = 1.75
print(zhangsan)
# setdefault 有则不修改,没有则新增
zhangsan.setdefault("height",1.80)
zhangsan.setdefault("address","中国")
zhangsan.setdefault("gender",True)
print(zhangsan)

print( zhangsan.get("name") )
print( zhangsan.keys() )
print( zhangsan.values() )
print( zhangsan.items() )

# del 删除
del zhangsan['address']
print( zhangsan )
# 打印要删除的值
print( zhangsan.pop("age"))
print( zhangsan )

# 随机删除
print( zhangsan.popitem())
print( zhangsan )

zhangsan['name'] = "三哥"
print( zhangsan )

# update 合并字典
zhangsan_data = { "m" :"李四","f":"张无忌"}
zhangsan.update(zhangsan_data)
print( zhangsan )
print("------------------")
# 字典循环
for k,v in zhangsan.items():
    print(k,v)
print("------------------")
# 字典循环
for k in zhangsan:
    print("%s : %s" % (k,zhangsan[k]))

集合

# `````````````````````````
#   集合
#   无序、互异、确定
# `````````````````````````
set1 = {1,2,2,3,4,10}
print(set1) # >>>  {1, 2, 3, 4,10}

set2 = set('hello')
print(set2) # >>>  {'l', 'h', 'e', 'o'}

set3 = { num for num in range(1,20) if num % 3 == 0 or num % 5 == 0}
print(set3) # >>>  {3, 5, 6, 9, 10, 12, 15, 18}

# in 元素是否在集合中
print(10 in set3) # >>> True
print(10 not in set3) # >>> False

# & 交集
print( set1 & set3) # >>> {10, 3}
print( set1.intersection(set3)) # >>> {10, 3}

# 并集
print( set1 | set3) # >>> {1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18}
print( set1.union(set3)) # >>> {1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18}

# 差集
print( set1 - set3) # >>> {1, 2, 4}
print( set1.difference(set3)) # >>> {1, 2, 4}

# 对称差
print( set1 ^ set3) # >>>{1, 2, 4, 5, 6, 9, 12, 15, 18}
print( set1.symmetric_difference(set3)) # >>> {1, 2, 4, 5, 6, 9, 12, 15, 18}
print( (set1 | set3) - (set1 & set3)) # >>> {1, 2, 4, 5, 6, 9, 12, 15, 18}

print("------------------------"  )
set4 = set()
set4.add(31)
set4.add(33)
set4.add("33")
set4.update({34,35,36})
print(set4)

set4.discard("33")
print(set4)


set4.remove(33)
print(set4)

print( set4.pop() )
print(set4)

set4.clear()
print(set4)
print("------------------")
# frozenset 不可变集合,不能添加修改
set5 = frozenset({1,3,5,6})
print(set5)
0

评论 (0)

取消