python note 06 编码方式

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,90,99]
dic = {}
l_high = [] #大于66的所有值列表
l_low = []  #小于66的所有值列表
for i in li:
    if i == 66:continue
    if i > 66:
        l_high.append(i)
    else:
        l_low.append(i)
dic.setdefault('k1',l_high)
dic.setdefault('k2',l_low)
print(dic)
#输出{'k2': [11, 22, 33, 44, 55], 'k1': [77, 88, 90, 99]}

2、

输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机", "电脑", '鼠标垫', '游艇']
要求:1:页面显示 序号 + 商品名称,如:
1 手机
2 电脑

2: 用户输入选择的商品序号,然后打印商品名称
3:如果用户输入的商品序号有误,则提示输入有误,并重新输入。
4:用户输入Q或者q,退出程序。

flag = True
while flag:
    li = ["手机", "电脑", "鼠标垫", "游艇"]
    for i in li:
        print('{}\t\t{}'.format(li.index(i)+1,i))
    num_of_chioce = input('请输入选择的商品序号/输入Q或者q退出程序:')
    if num_of_chioce.isdigit():
        num_of_chioce = int(num_of_chioce)
        if num_of_chioce > 0 and num_of_chioce <= len(li):
             print(li[num_of_chioce-1])
        else:print('请输入有效数字')
    elif num_of_chioce.upper() == 'Q':break
    else:print('请输入数字')

3、

#数字,字符串 小数据池
#数字的范围 -5 -- 256
#字符串:1,不能有特殊字符
#       2,s*20 还是同一个地址,s*21以后都是两个地址

s = 'alex'
s1 = b'alex'
print(s,type(s))
print(s1,type(s1))
#输出alex <class 'str'>
#    b'alex' <class 'bytes'>

编码方式(了解)

str 在内存中是用unicode编码。


对于英文:
str :表现形式:s = 'alex'
编码方式: 010101010 unicode
bytes :表现形式:s = b'alex'
编码方式: 000101010 utf-8 gbk。。。。

对于中文:
str :表现形式:s = '中国'
编码方式: 010101010 unicode
bytes :表现形式:s = b'x\e91\e91\e01\e21\e31\e32'
编码方式: 000101010 utf-8 gbk。。。。

s1 = 'alex'
# encode 编码,如何将str --> bytes, ()
s11 = s1.encode('utf-8')
s11 = s1.encode('gbk')
print(s11)
#输出b'alex'
s2 = '中国'
s22 = s2.encode('utf-8')
print(s22)
#输出b'\xe4\xb8\xad\xe5\x9b\xbd'
s22 = s2.encode('gbk')
print(s22)
#输出b'\xd6\xd0\xb9\xfa'

猜你喜欢

转载自www.cnblogs.com/alifetimelove/p/10522837.html