列表是一种有序的可变序列
,可以包含不同类型的元素。列表可以通过方括号 []
来表示,元素之间用逗号分隔
。
注释: 注意列表可变,字符串不可变,只能改变大小写
my_list = [1, 'hello', 3.14, True]
元组是一种有序的不可变序列
,同样可以包含不同类型的元素。元组可以通过圆括号 ()
来表示,元素之间用逗号分隔
。
my_tuple = (1, 'hello', 3.14, True)
因为元组是不可修改的所以只能查询,如果要修改得先转换成列表进行修改,之后在转换成元组
x = (1,5,'i','j')
# x.sort() #报错 AttributeError: 'tuple' object has no attribute 'sort'
print(x[1]) #输出: 5
x[1] = 6 #报错 TypeError: 'tuple' object does not support item assignment
字典是一种键值对
的集合,键和值可以是任意的数据类型。字典可以通过花括号 {}
来表示,每个键值对使用冒号 : 分隔
,键值对之间用逗号分隔
。可做内容修改
a={age:10}
a['age']=18
print(a) #输出 {'age': 18}
字典里边没有顺序 ,列表有从0开始
字典是直接删除重新加入,所以没有顺序
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
a={'age':10,'name':'xiaoming'}
print('name' in a) #输出 True
print('s' not in a) #输出 True
可以通过dict(健)=键值
for 健,键值 in 字典.items()
a = {'age':10,'name':'xiaoming'}
for (k,v) in a.items(): #()可加可不加
print(k,v)
print(k,v) 输出:
age 10
name xiaoming
键
的视图值
的视图my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
items = my_dict.items()
print(items)
输出: dict_items([(‘name’, ‘Alice’), (‘age’, 25), (‘city’, ‘New York’)])
集合是一种无序的
、不重复
的元素的集合。集合可以通过花括号 {} 或 set() 函数来创建
。
my_set = {1, 2, 3, 4, 5}
随机
添加元素(因为无序所以随机)
字符串是一种由字符组成的不可变序列
,可以用单引号或双引号括
起来
my_string = 'Hello, World!'
上篇文章有写,跳转地址python3 0基础学习----基本知识
print('hi~') #输出 hi~
for num in range(5):
print(num) # 输出: 0, 1, 2, 3, 4
for num in range(2, 7):
print(num) # 输出: 2, 3, 4, 5, 6
for num in range(1, 10, 2):
print(num) # 输出: 1, 3, 5, 7, 9
num1 = int("10")
num2 = float("3.14")
text = str(42)
flag = bool(1)
my_list = [3, 1, 5, 2, 4]
max_value = max(my_list)
min_value = min(my_list)
print(max_value) #输出 5
print(min_value) #输出 1
s = abs(-10)
print(s) #输出 10
rounded_num = round(3.14159, 2)
print(rounded_num) #输出3.14
>>>dir() # 获得当前模块的属性列表
['__builtins__', '__doc__', '__name__', '__package__', 'arr', 'myslice']
>>> dir([ ]) # 查看列表的方法
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
some = [1,2,3,4,5,6]
print(3 in some) #输出 True
print(3 not in some) #输出 False
Dear Sir/Madam,
I am writing this email to express my gratitude to you and to discuss
some matters. I hope this email finds you in good health and high
spirits.Firstly, I would like to sincerely thank you for your generosity and
assistance. I have been facing some difficulties in pursuing my career
goals, and your support has been invaluable to me. Your advice and
guidance have helped me gain a better understanding of the challenges
I have faced and have motivated me to continue striving.The purpose of this email is to request a meeting with you in order to
personally express my gratitude. I would like the opportunity to
showcase the progress I have made in my professional development and
to hear your valuable insights. If you are willing, I can arrange the
meeting according to your convenience, and the location and date can
be adjusted according to your preferences.Furthermore, I wanted to inquire if there is anything else I can do
for you. Your generosity may have an impact not only on me personally
but also on other individuals I may be able to assist. Please let me
know if there is anything you need help with, as I would be more than
happy to offer my assistance.Once again, thank you for your support and generosity, and I hold
great expectations for the future. I sincerely look forward to meeting
with you and expressing my gratitude in person. If you have any
questions or require further information regarding the meeting, please
feel free to contact me.With heartfelt appreciation,
[Your Name]
遍历文件每行内容
拆分每行内容为单词列表
遍历当前行列表单词
查找list中是否存在当前单词,存在记录出现个数,不存在新增一条记录
th = open('a.txt')
print('读取文件内容',th)
lst = list()#空列表
for item in th:
itemStr = item.rstrip()# 去除末尾空白符号
pList = itemStr.split()# 以空格作为分隔符分割每行数据,返回一个单词列表 ,例如首行:['Dear', 'Sir/Madam,']
for word in pList:
if len(lst)==0:
lst.append(word)
continue
if len(lst)>0:
if lst.count(word)>0:
continue
else:
lst.append(word)
print('列表长度',len(lst))
lst.sort()
print(lst)
读取文件
声明空字典
遍历文件内容
去掉每行结尾空白符号
切割每行生成单词字典
th = open('a.txt')
dictStr = dict()#空字典
for item in th:
itemStr = item.rstrip()# 去除末尾空白符号
pDict = itemStr.split()# 以空格作为分隔符分割每行数据,返回一个单词列表 ,例如首行:['Dear', 'Sir/Madam,']
for word in pDict:
dictStr[word] = dictStr.get(word,0)+1 #查找。找到获取对应值+1,没找到默认为0+1
# print(dictStr.items())#items方法,返回可迭代对象的(key,value)
print(sorted([(k,v) for k,v in dictStr.items()]))
bigKey = None #最大键
bigValue = None #最大键值
for k,v in dictStr.items():
if bigKey is None or v>bigValue: # is判断是否相等,or或
bigValue = v
bigKey = k
print(bigKey,':',bigValue)
输出结果:to : 17