切片
mystr='helloworld'
mystr=mystr[1:3]
# print(mystr)
format
mystr2 = 'welcome, dear {name}'
mystr2 = mystr2.format(name = "haha")
# print(mystr2)
join
mystr3 = ['luo','bo','da','za']
mystr3 = '-'.join(mystr3)
# print(mystr3)
replace
mystr4 = 'haha-lala'
mystr4 = mystr4.replace('haha','good')
# print(mystr4)
split
mystr5 = 'lala,haha'
mystr5 = mystr5.split('h')
# print(mystr5)
append 和 extend
list1 = [1,2]
list2 = [3,4]
list3 = [5,6]
list1.append(list2)
list3.extend(list2)
# print(list1)
# print(list3)
删除元素
del:根据下标进行删除
pop:删除最后一个元素
remove:根据元素的值进行删除
list4 = ['a', 'b', 'c', 'd']
del(list4[0])
# print(list4)
# list4.pop()
# print(list4)
# list4.remove('c')
# print(list4)
排序
mylist5 = [1, 5, 2, 3, 4]
mylist5.sort()
print(mylist5)
mylist5.reverse()
print(mylist5)
清空字典
dict1 = {'key1':1,'key2':2}
# dict1.clear()
# print(dict1)
使用 pop 方法来指定删除字典中的某一项
# d1 = dict1.pop('key1')
# print(d1)
# print(dict1)
遍历字典
mykey = [key for key in dict1]
# print(mykey)
myval = [val for val in dict1.values()]
# print(myval)
key_val = [(k,v) for k,v in dict1.items()]
# print(key_val)
fromkeys
用于创建一个新字典,以序列中元素做字典的键,value 为字典所有键对应的初始值
keys = ['aa','bb','cc','dd']
dict2 = dict.fromkeys(keys,0)
# print(dict2)
== 是比较操作符,只是判断对象的值(value)是否一致。
is 判断的是对象之间的身份(内存地址)是否一致。
*arg会把位置参数转化为tuple **kwarg会把关键字参数转化为dict
sum(range(1,101))
import time
import datetime
# print(datetime.datetime.now())
import copy
浅拷贝
l1 = [1, 2, 3, [1, 2]]
l2 = copy.copy(l1)
l2.append('a')
l2[3].append('a')
# print(l1,l2)
深拷贝
l3 = copy.deepcopy(l1)
l3.append('a')
l3[3].append('a')
# print(l1,l3)
def num():
return [lambda x:i*x for i in range(4)]
print([m(1) for m in num()])
[3, 3, 3, 3]
for i in range(1,10):
for j in range(1,i+1):
print("%s*%s=%s" %(i,j,i*j),end="")
print()
mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list(filter(lambda x: x%2 == 1, mylist))
mylist = [1,2,3,4,5,6,7,8,9]
li1 = list(map(lambda x: x*2,mylist))
# print(li1)
from functools import reduce
li3 = reduce(lambda x,y: x+y, range(101))
# print(li3)
a,b = 1,2
h = a-b if a>b else a+b
# print(h)
print(random.random())
print(random.randint(1, 100))
from collections import Counter
mystr = 'dhasdgaskbkgagdadd;hadgajkdhklhafa'
# print(Counter(mystr))
# print([x for x in range(10) if x%2 == 1])
def binary_search(data,item):
n = len(data)
first = 0
last = n-1
while first <= last:
mid = (first+last)//2
if data[mid] == item:
return mid+1
elif data[mid] > item:
last = mid-1
else:
first = mid+1
return '不在列表中'
l1 = [1,2,5,66,80,100]
# print(binary_search(l1,880))
import json
dict1 = {'a':1,'b':2,'c':3}
myjson = json.dumps(dict1)
# print(myjson)
mydict = json.loads(myjson)
# print(mydict)
# read: 读取整个文件
# readline: 读取下一行,使用生成器方法
# readlines: 读取整个文件到一个迭代器供我们遍历
import random
l1 = [1,2,3,4]
random.shuffle(l1)
# print(l1)
str1 = 'liuleqian NB'
# print(str1[::-1])
# 主要分为实例方法、类方法和静态方法
# 实例方法:
# 定义:第一个参数必须是实例对象,该参数名一般约定为“self”,通过它来传递实例的属性和方法(也可以传类的属性和方法);
# 调用:只能由实例对象调用。
# 类方法:
# 定义:使用装饰器@classmethod。第一个参数必须是当前类对象,该参数名一般约定为“cls”,通过它来传递类的属性和方法(不能传实例的属性和方法);
# 调用:实例对象和类对象都可以调用。
# 静态方法:
# 定义:使用装饰器@staticmethod。参数随意,没有“self”和“cls”参数,但是方法体中不能使用类或实例的任何属性和方法;
# 调用:实例对象和类对象都可以调用。
str1 = " han "
# print(str1.strip())
str1 = "ha haha ha"
# print(str1.replace(" ",""))
print("".join(str1.split(" ")))