网页提取固定位置的数据
python 基本数据结构
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all"
- list在内存空间开辟连续的内存空间
-4. 通过添加元素,是index对齐
- python slice使用" [ ]",matlab使用“( )”
- list 元素用“,”隔开。matlab 数组可以用“ ”隔开
- 7.学习查看API
- del 函数删除list中的元素
- remove('string') #删除的另一种方式
- contains
weekdays.remove("Friday") # 删除之前要确保存在
'Friday' in weekday
python 基本数据结构(三)
weekdays.append('Saturday','Sunday') # 不能同时添加两个元素,在最后插入
weekdays.insert(0,"MondayAAA")# 在第0个元素之前插入
weekdays.index('Friday') # 找到元素,并且返回index
last_day=weekdays.pop() # 取最后一个元素
print("last_day=",last_day,"\nweekdays=",weekdays) # 拼接
# 计时函数
def timer(f):
def _f(*args):
t0=clock()
f(*args)
return clock()-t0
return _f
timer(delete_elem)(x,-1) # 获取运行时间,3e-5s
print(len(x))
timer(delete_elem)(x,0) # 0.1s
print(len(x))
# 删除前边的元素,需要把其他元素向前整合。
# 在中间插入和在开始插入一个元素效率较低
timer(insert_elem)(x,0,9) # 0.56
print(len(x))
timer(insert_elem)(x,-1,9)# 6.7e-6
print(len(x))
# sort: sorted()保存原变量,sort(),改变原变量
nums.sort(reverse=True) # 反向排序
sorted('jsdfiwemgajsdf2124') # 可以进行排序
'jsdfiwemgajsdf2124'.sort() # str没有sort属性,只有sorted
obj_list=["string",1,True,3.14]
obj_list.sort() # 不同类型元素之间无法进行比较
x=sorted('sdfsgsdh3swg') # 数据类型是list
y=''.join(sorted('ABCDZYX')) # 数据类型是str
x
type(x)
y
type(y)
# assign vs copy
a=[1,2,3]
b=a
print("a=",a,"\nb=",b)
a[0]=2
print("a=",a,"\nb=",b) # a和b 通过assign,指向同一个地址
# copy,不改变copy量
a=[1,2,3]
b=a
c=a.copy()
d=a[:]
e=list(a)
""" a= [2, 2, 3]
b= [2, 2, 3]
c= [1, 2, 3]
d= [1, 2, 3]
e= [1, 2, 3]"""
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity="all" # 显示左右变量
s1[0]='x' # TypeError: 'str' object does not support item assignment
# str 不支持分配,可不可以修改,string is immutable
name,gender,age=tom_tuple # tuple,可以进行同时多变量赋值
# dictionary——key value pare
# A *dictionary* is similar to a list. But the order of items doesn't matter and they aren't selected by their offset(index). Instead, you specify a unique *key* to associate with each value. The key is often a string, but it can be any of Python's immutabletypes.
# ““:””,
pizza={
"size":"medium",
"type":"pepperoni",
"crust":"Thick",
"qty":1,
"deliver":True
}
pizza # 无序,通过key访问
pizza['type'] # 通过key,访问value
#pizza['topping'] # 不存在,调用——keyError
print(pizza.get('topping'))
pizza.keys() # 所有的key
pizza.values()
pizza.items() # 所有的项目
#dict_items([('size', 'medium'), ('type', 'pepperoni'), ('crust', 'Thick'), ('qty', 1), ('deliver', True)])
pizza['topping']=['cheese','mushroom'] # 添加新的key,struct 的添加 field
# > Tip:Key are unique in dictionaries.
del pizza['topping'] # 删除指定的key& value pair
pizza.clear() # 清空dict
# set——A set is like a dictionary with its values thrown away. Leaving only the keys
empty_set=set() # 定义空set,唯一定义空set的方式
# {}指代的类型是dict
even_set={2,4,6,6,8,10} # 创建set
clock() # 不加()认为是变量
-4. hash方法,key——hash——int(hash code)——取模——index
python 基本数据结构(七)
num_set& even_set # 两个中同时出现
num_set ^ even_set # 异或+相同的时候是零,不同的时候取并集
num_set- even_set # 非,属于第一个集合,但不属于第二个集合
num_set| even_set # 集合取并集
# convert to list
list('abdsdf')# string
list((1,2,3,4,5))# tuple
list({'name':'Ed','employer':'Oracle'}) # dict.key
list({'name':'Ed','employer':'Oracle'}.values()) # dict.value
list({5,6,7,8}) # 集合
set('sdfgerh') # string 2 set
dict(['ab','cd','ef']) list to dict
s1='asdfgg'
s2='erwqte'
list(zip(s1,s2)) # zip 打包
dict(zip(s1,s2))
# zip
d=list(zip(s1,s2)) # zip function
s3,s4=zip(*d) # unzip function
type(s3) # unzip 之后的数据为tuple
print(list(s3))
print(list(s4))
?zip 查看 API
numberlist=[1,2,3]
strlist=['one','two','three']
result=zip(numberlist,strlist)
type(result) # zip 也是一种data type
# mutable & immutable
s='hello'
print("{} id is {}".format(s,id(s)))
s='Yello'
print("{} id is {}".format(s,id(s)))
s=s+'w' # 昂贵的操作
print("{} id is {}".format(s,id(s))) # 把之前的结果覆盖了。
# dict 的key 只能是 immutable的,basic type基本是immutable。
# dict,list,set are mutable
mutable & immutable
mutable and immutable
# list mutable, update immediately
try_result=[] # cold refresh
def try_func(arg,result):
print("inside before result is labeled {}, value {}".format(id(result),result))
result.append(arg) # 修改了list(mutable)
print("inside after result is labeled {}, value {}".format(id(result),result))
print("inside try_result is labeled {}, value {}".format(id(try_result),try_result))
print("inside before result is labeled {}, value {}".format(id(try_result),try_result))
try_func('a',try_result)
print("inside after result is labeled {}, value {}".format(id(try_result),try_result))
# inside before result is labeled 2105835206536, value []
# inside before result is labeled 2105835206536, value []
# inside after result is labeled 2105835206536, value ['a']
# inside try_result is labeled 2105835206536, value ['a']
# inside after result is labeled 2105835206536, value ['a']
# copy function
try_result=[] # cold refresh
def try_func(arg,result):
print("inside before result is labeled {}, value {}".format(id(result),result))
result.append(arg) # 修改了list(mutable)
print("inside after result is labeled {}, value {}".format(id(result),result))
print("inside try_result is labeled {}, value {}".format(id(try_result),try_result))
# pass by value,不太明白
# imutable 的数据,系统会自动产生一个copy,来接受这个量。
# mutable的数据系统直接传递引用(指针)
# Note :Defualt argument values are calculate when the function is define. not when it is run. A common error with new (and sometimes not-so-new) Python programmers is to use mutable data type such as list or dictionary as defualt argument.
网友评论