1. 程式人生 > 實用技巧 >第五天知識總結

第五天知識總結

例題1:有如下值li=[11,22,33,44,55,66,77,88,99,90],將所有大於66的值儲存至字典的第一個key中
,將小於66的值儲存至字典的第二個key中。即{'k1':大於66的所有值列表,'k2':小於66的所有值列表}
# 方法一
li = [11,22,33,44,55,66,77,88,99,90]
dic = {'k1':[],
       'k2':[]
       }
for i in li:
    if i == 66:continue
    if i < 66:
        dic['k1'].append(i)
    else:
        dic[
'k2'].append(i) print(dic) # 方法二 li = [11,22,33,44,55,66,77,88,99,90] dic = {} li_greater = [] li_less = [] for i in li: if i == 66: continue if i < 66: li_less.append(i) else: li_greater.append(i) dic.setdefault('k1',li_greater) dic.setdefault('k2',li_less) print
(dic)
例題2:輸出商品列表,使用者輸入序號,顯示使用者選中的商品。
商品 li=['手機','電腦','滑鼠墊','鍵盤']
要求:1:頁面顯示 序號+商品名稱,如:
1.手機
2.電腦
。。。
2:使用者輸入num_choose = input('請選擇商品序號'),然後列印商品名稱
3:如果使用者輸入的商品序號有誤,則提示輸入有誤,並重新輸入。
4:使用者輸入Q或q,推出程式
li=['手機','電腦','滑鼠墊','鍵盤']
for i in li:
        print
('{}\t{}'.format(li.index(i)+1,i)) while 1: sum_choose = input('輸入的商品序號/輸入R或者r退出') if sum_choose.isdigit(): sum_choose = int(sum_choose) if sum_choose > 0 and sum_choose <= len(li): print(li[sum_choose-1]) else: print('輸入有誤,並重新輸入') elif sum_choose.upper() == 'Q': break else: print('請輸入數字')
小知識點(瞭解就行):
# 1.一個=是賦值,兩個等號是比較值是否相等;is是比較的意思,比較的是記憶體地址;id是記憶體地址的意思
li1 = [1,2,3]
li2 = li1
print(li1 is li2)
print(id(li1),id(li2))
# 2.當數字的範圍在-5--256之間時,記憶體地址時一樣的
i1 = 3
i2 = 3
print(id(i1),id(i2))
i3 = 295
i4 = 295
print(id(i3),id(i4))          # 在終端去試
# 3.字串:1.不能含有特使字元/
#            2.str*20還是同一個地址,s*21以後都是兩個地址
i5 = 'find'
i6 = 'find'
print(id(i5),id(i6))
i7 = '@fing'
i8 = '@fing'
print(id(i7),id(i8))            # 在終端去試
i9 = 'a'*20
i10 = 'a'*20
print(i9 is i10)
i9 = 'a'*21
i10 = 'a'*21
print(i9 is i10)                 # 在終端試
編碼問題:


1.各個編碼之間的二進位制,是不能互相識別的,會產生亂碼
2.檔案的儲存和傳輸不能是unicode(只能是utf-8,utf-16,gbk,gb2312,ascii等)


python3:
        str在記憶體中是用unicode編碼
          bytes型別
          對於英文:
                str : 表現形式:s='alex'
                      編碼方式: 00100101   unicode
                bytes : 表現形式:s=b'alex'
                        編碼方式:00100101  utf-8,jbk...
          對於中文:
                str : 表現形式:s='中國'
                      編碼方式: 00100101   unicode
                bytes : 表現形式:s=b'\xe4\xb8\xad\xe5\x9b\xbd'
                        編碼方式:00100101  utf-8,jbk...


# encode:編碼,如何將str --> bytes,可以設定編碼方式
s1 = 'alex'
s1_1 = s1.encode('utf-8')
print(s1_1)
s2 = '中國'
s2_2 = s2.encode('utf-8')
print(s2_2)
例題4.購物車作業(持續完善)


li = [
    {'name':'蘋果','price':10},
    {'name':'梨子','price':12},
    {'name':'香蕉','price':15},
    {'name':'橘子','price':18},
    {'name':'葡萄','price':20}
]
shopping_car = {}
print('歡迎光臨本小店!')
for i,k in enumerate(li):
        print('序號:{}\t商品:{}\t價格:{}{}'.format(i,k['name'],k['price'],''))
money = input('請輸入你預計的總價格:')
if money.isdigit():
    while 1:
        choose = input('請輸入商品序號')
        if choose.isdigit() and int(choose) < len(li) and int(choose) >= 0:
            number = input('請輸入商品個數:')
            if number.isdigit():
                if int(money) > li[int(choose)]['price'] * int(number):
                    money = int(money) - li[int(choose)]['price'] * int(number)
                    if li[int(choose)]['name'] in shopping_car:
                        shopping_car[li[int(choose)]['name']] += int(number)
                    else:
                        shopping_car[li[int(choose)]['name']] = int(number)
                    print('你的購物車中商品有{},餘額為{}'.format(shopping_car,money))
                else:
                    print('價格超出預算,請重新選擇')
                    break
            else:
                print('請輸入正確的數字')
        else:
            print('請輸入正確的序號')
else:
    print('請輸入正確的數額')