python筆記_2

--------------------------------2
'''
    字符串操作
    ccc = "我愛中國愛我"
    ccc[0:2]:截取
    ccc.replace("old","new",count):替換
    print("ai" in ccc):判斷是否包含
    print(ccc.endswith("中",2,4)):判斷[2,4)中是否以"中"結尾
    print(ccc.startswith("中",2,4)):判斷[2,4)中是否以"中"開頭
-------
   字符串拼接demo:
    n=10
    n1="10"
    print(str(n)+n1)
    print(n.__str__()+n1)
-------
    統計字符串中每一個字符出現次數demo
    實現思路:建立一個mycount空字典,遍歷字符串,通過get(key)的方法得到對應的value
    如果該值不存在,將對應的key,value=1 放入到mycount空字典中,
    如果該值存在,將對應的key,value=value+1 放入到mycount空字典中,

    mystr= input("請輸入字符串:")
    mycount={}
    for s in mystr :
        n=mycount.get(s)
        if  n is None :
           mycount.__setitem__(s,1)
        else:
            mycount.__setitem__(s,n+1)
    print(mycount)
    for key in mycount:
        #print("%s出現了%d次"%(key, mycount.get(key)))       用c   格式化字符串
        print("{0}出現了{1}次".format(key,mycount.get(key)))#format格式化字符串、
-------
    python是函數式編程,所有的行爲都有對應的函數
    for(;;){}
    for(:){} foreach
    for 迭代變量 in 迭代器 :
    範圍 range
    反轉 reversed

    ():元組  tuple             --》  java中: 數組
    []:列表  list                            list
    []:集合  set                             set      無序,數據不可重複
    {}:字典  dict                            map      鍵值 鍵不可重複

    listDemo:
    mylist=['abc','123']
    mylist.insert(1,'oo')       : 添加指定元素到列表指定位置
    mylist.append('ppp')        :添加一個到末尾
    mylist+=['a','b']           :與另外一個列表拼接
    mylist.remove('a')          :移除列表中指定元素(通過元素名稱)
    mylist.pop(0)               :移除列表中指定元素(通過元素下標)
    mylist.clear()              : 移除所有
    del mylist[2]               :移除列表中指定元素(通過元素下標)

    setDemo:
    myset={"b","b"}
    print(len(myset))
    print(myset)
    myset.add("a")
    myset.add("c")
    print(myset)
    for a in myset :
        print(a)

    dictDemo:
    myzidian={"cn":"china","cn":"chinese","en":"english","us":"a....."}
    print(len(myzidian))
    print(myzidian)
    myzidian.__setitem__("us1","americ....")
    print(myzidian)
    print(myzidian.get("en"))
    myzidian.pop("us1")
    print(myzidian)

    序列:sequence  可迭代的數據
    元組不可被改變,無論順序還是內容
--------
    n=10
    print(n*2)  2倍
    print(n**2) 2次方

--------

    有關隨機數的操作
        import  random
        n =   int(random.random()*10)    #1-10  隨機數 1個 可重複
        print(n)
        print(random.choice(range(1,10)))#1-10  隨機數 1個 可重複
        nums=random.sample(range(1,34),6)#1-34  隨機數 6個 不重複
        print(nums)
--------
    有關排序的操作
    nums=random.sample(range(1,34),6) #nums:集合
    print(sorted(nums))#不改變元數據,改變顯結果
    nums.sort()        #改變元數據
    print(nums)

    冒泡排序換手demo:
    arr=[1,2,3,4,9,5,8]

    for i in range(0,len(arr)-1):
        for j in range(0,len(arr)-i-1):
            if arr[j]<arr[j+1]:
                arr[j],arr[j+1]=arr[j+1],arr[j]
    print(arr)
--------
    推導:lambdas 只有一行
    myword=['Hi1','Hello1','word','python','Height','wall','Hight']
    print([a+"*"+b  for a in myword if a.endswith("1") for b in myword if not b.startswith("H")])
--------

'''
發佈了36 篇原創文章 · 獲贊 14 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章