python之链表,format,字符串,tuple,文件读取,set,dict,json文件读取,csv文件读取,函数,sorted函数,print

来源:互联网 发布:公安部大数据平台 编辑:程序博客网 时间:2024/05/22 01:33
  • 链表应用
    myList=[2,3,4,5]#链表构建
    for v in myList:#for循环使用
    print(v)
    len(myList)#长度
    myList.append(8)#添加一个元素
    myList*10#10个myList合一起
    myList1+myList2#两个链表合一起
    myList.extend(myList1)#添加一个链表
    myList.count(a)#a出现的次数
    myList.index(a)#第一次a出现的位置
    a in myList#a是否在链表里,返回布尔值
    myList.insert(index,a)#在index位置插入a
    myList.remove(a)#从链表中删除第一次出现的a
    myList.pop(index)#删除并返回第index位置的值,若index为空,返回最后一个值
    z=[2,3,5,5]
    a,b,*c=z#a=2,b=3,c对应余下的
    max(myList)#链表里的最大值
    min(myList)#链表里的最小值
    sum(myList)#链表里的求和
    myList.reverse()#链表反转
  • format的用法
    for a_new_variable in range(0,10):
    print(f"value is {a_new_variable }")#第一种用法
    print("value is {},times is {}".format(a_new_variable,a_new_variable*2))#第二种用法
  • 字符串的应用
    -s="hello world"
    s[::2]#s["start","end+1","step"]
  • 元组tuple
    atuple=(2,3,4,5)#建立一个tuple
    atuple+=(3,4,6)#相加
    type(())#类型为tuple
    type((9))#类型为int
    type((9,))#类型为tuple
    (b,a)=(a,b)#两个tuple互换
    4,#输出为(4,)
  • 文件读取
    filename="NASA_access_log_Jul95"
    file=open(filename,encoding='latin-1')#如果读取不到,可以换编码utf-8或者其它
    line=file.readline()#读取第一行
    lines=file.readlines()#读取所有行
    firstline.split()#把一行分割成一个链表,若分隔符为空,则用空格分隔
    data=[line.split() for line in lines if len(line.split())==10]#简单写法
  • 集合set
    set([0,1,1,1,2,2,2])#返回{0,1,2},找到不重复的集合,里面是一个链表
    s=set()#新建一个set
    s.add(a)#新添一个元素
  • pandas用法
    import pandas as pd
    df=pd.DataFrame(data_clean)#data_clean是个链表,里面的每一个元素也是链表
  • 字典dict
    shirt={"color":"red","size":"large","price":13.8}#新建一个字典
    shirt['stock']=10#添加一个字段
    #新建字典第一种
    count={}
    for line in data_clean:
    method=line[2]
    if method in count:
    count[method]+=1
    else
    count[method]=1
    #新建字典第二种
    count2={}
    for line in data_clean:
    method=line[2]
    count2[method]=count2.get(methond,0)+1
    #新建字典第二种
    sites_per_hosting={}
    for ws in ws_data:
    hosting=ws['hosting']
    site=ws['_id']
    sites=sites_per_hosting.get(hosting,[])#不能向上边一样一句话,因为链表能相加
    sites.append(site)
    sites_per_hosting[hosting]=sites
    sites_per_hosting
    #新建字典第三种
    from collections import Counter
    count3=Counter()
    for line in data_clean:
    count3[line[2]]+=1
    #循环输出字典的键
    for k in sites_per_hosting:
    print(k,sites_per_hosting[k])
    #循环输出字典的值
    for v in sites_per_hosting.values():
    print(v)
    #循环输出字典的键和值
    for k,v in sites_per_hosting.items():
    print(k,"-->",v)
  • json数据文件的读入
    #第一种方法
    import json
    ws_file=open('websites.json')
    ws_data=[]
    for line in ws_file:
    d=json.loads(line)#d是一个字典
    ws_data.append(d)

    #第二种方法
    ws_data=[json.loads(l) for l in open('websites.json')]
  • csv数据文件的读入
    import pandas as pd
    customs=pd.read_csv(‘data2’)
    customs[“avg”]=customs[“total”]/customs[“num_orders”]
    customs.sort_values(by=’avg’)#通过某个属性的值排序
  • 函数
    #定义一个函数
    def msg():
    print()
    #带参数的函数
    def g(x,y=5):
    return x*10+y
  • sorted函数
    fruits=['banana','pineaplle','strawberry','apple','peach']
    print(sorted(fruits))#按字母表排序
    print(sorted(fruits,reverse=True))#反过来排序
    sorted(fruits,key=len)#按长度排序
    def hosting_key(d):
    return d['hosting']
    sorted(ws_data,key=hosting_key) #按某个值排序
    def complex(d):
    return d['hosting'],d['pv']
    sorted(ws_data,key=complex)#按两个值先后排序
  • 输出print
    print(1,2,3,4,sep="--",end="+")
    print(10,11)#输出1--2--3--4+10 11
原创粉丝点击