Python技巧100题(六)

在这里插入图片描述

1.用for循环实现把字符串编程Unicode码位的列表

st = '!@#$%^&*'
codes = []
for s in st:
    codes.append(ord(s))
print(codes)
[33, 64, 35, 36, 37, 94, 38, 42]    

2.用列表推导式实现把字符串变成Unicode码位的列表

st = '!@#$%^&*'
codes = [ord(s) for s in st]
priont(codes)
[33, 64, 35, 36, 37, 94, 38, 42]

3.打印出两个列表的笛卡尔积

colors = ['blacks', 'white']
sizes = ['s', 'M', 'L']
for tshirt in ('%s %s'%(c, s) for c in colors for s in sizes):
    print(tshirt)    
blacks s
blacks M
blacks L
white s
white M
white L    

import itertools
print(list(itertools.product(['blacks', 'white'], ['S', 'M', 'L'])))
[('blacks', 'S'), ('blacks', 'M'), ('blacks', 'L'), ('white', 'S'), ('white', 'M'), ('white', 'L')]

4.可迭代对象拆包时,怎么赋值给占位符

player_infos = [('kobe', '24'), ('james', '23'), ('Iverson', '3')]
for player_names, _ in player_infos:
    print(player_names)
kobe
james
Iverson    

5.Python3中用什么方法接收不确定值或参数

a, b, *c = range(8)
print(a, b, c)
0 1 [2, 3, 4, 5, 6, 7]

a, *b, c, d = range(5)
print(a,b,c,d)
0 [1, 2] 3 4

*a, b, c, d = range(5)
print(a,b,c,d)
[0, 1] 2 3 4

6.用切片将对象倒序

s = 'basketball'
print(s[::-1])
llabteksab

7.怎么查看列表的ID

l = [1, 2, 3]
print(id(l))
17159400

8.可变序列用*=(就地乘法)后,会创建新的序列吗

l = [1, 2, 3]
print(id(l))
28562664
l *= 2
print(l)
[1, 2, 3, 1, 2, 3]
print(id(l))
28562664

9.不可变序列用*=(就地乘法)后,会创建新的序列吗

t = (1, 2, 3)
print(id(t))
54827560
t *= 2
print(t)
(1, 2, 3, 1, 2, 3)
print(id(t))
54752040

10.关于+=的一道谜题

t = (1, 2, [30, 40])
t[2] += [50, 60]
print(t)
(1, 2, [30, 40, 50, 60])

人生漫漫其修远兮,网安无止境。
一同前行,加油!

猜你喜欢

转载自blog.csdn.net/qq_45924653/article/details/108145054