Python中一些语法的使用

元组传参

1
2
3
4
def add(*args):
    print(args, type(args))

add(2, 3)
1
(2, 3) <class 'tuple'>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def add(*numbers):
    total = 0
    for num in numbers:
        total += num
    return total

print(add(2, 3))
print(add(2, 3, 5))
print(add(2, 3, 5, 7))
print(add(2, 3, 5, 7, 9))
1
2
3
4
5
10
17
26

字典传参

1
2
3
4
5
def total_fruits(**kwargs):
    print(kwargs, type(kwargs))


total_fruits(banana=5, mango=7, apple=8)
1
{'banana': 5, 'mango': 7, 'apple': 8} <class 'dict'>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def total_fruits(**fruits):
    total = 0
    for amount in fruits.values():
        total += amount
    return total


print(total_fruits(banana=5, mango=7, apple=8))
print(total_fruits(banana=5, mango=7, apple=8, oranges=10))
print(total_fruits(banana=5, mango=7))
1
2
3
20
30
12

和return返回不同,yeild返回的是一个可迭代对象,可以通过next(yeild返回object)来取下一个元素

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def test_yield():
    for i in range(10):
        yield i


print(type(test_yield()))

_iter = test_yield()
print(next(_iter))
print(next(_iter))
print(next(_iter))
1
2
3
4
<class 'generator'>
0
1
2
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def test_yield():
    for i in [3, 5, 6, 1, 3, 516, 6321, 1]:
        yield i


_iter = test_yield()
num = 0
try:
    while True:
        num += 1
        print(next(_iter))
except Exception as ex:
    pass

print("列表的元素个数%d" % num)
1
2
3
4
5
6
7
8
9
3
5
6
1
3
516
6321
1
列表的元素个数9

==问,为什么要这么干,直接len()不就好了吗?答,除了获取长度,还可以进行其他操作,而不用先获取列表长度进行。==

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
>>> # 返回一个迭代器
>>> var = map(str, [3, 1, 2])
>>> print(list(var))
>>> print(list(var))  # 迭代器走完就没有值了

>>> begin = time.time()  # 记录转换耗时
>>> # 返回一个列表(推荐使用,速度更快,因未出现显式循环)
>>> var_s = list(map(str, [3, 1, 2]))
>>> print(var_s, "转换耗时: ", time.time()-begin)

>>> begin = time.time()  # 记录转换耗时
>>> # 返回一个列表
>>> var_s = [str(i) for i in [3, 1, 2]]
>>> print(var_s, "转换耗时: ", time.time()-begin)

['3', '1', '2']
[]
['3', '1', '2'] 转换耗时:  1.430511474609375e-06
['3', '1', '2'] 转换耗时:  1.6689300537109375e-06

python的执行中,等号右边先执行,如下代码中,python先创建出数组然后再让a指向该数组,如果简单用等号将a 赋值给b,则b也指向该数组,修改b的同时,原来的a也会随之改变,如果不想要a 发生改变,需使用b = a.copy()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> # 第一种赋值方式
>>> a = [3, 1, 2]
>>> b = a
>>> b[0] = 0
>>> print(a, b)
>>> # 第二种赋值方式
>>> a = [3, 1, 2]
>>> b = a.copy()  # 复制法
>>> b[0] = 0
>>> print(a, b)
out:
[0, 1, 2] [0, 1, 2]
[3, 1, 2] [0, 1, 2]

单纯输出列表效果不好看,可以做成表格形式。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
>>> # 列表显示为表格
>>> from prettytable import PrettyTable
>>> fruit = [['apple', 'red', '100g'], ['banana', 'yellow', '80g']]
>>> print("直接打印列表\n", fruit)
>>> x = PrettyTable()  # 创建表格实例
>>> x.field_names = ['fruit', 'color', 'weight']  # 定义表头
>>> x.add_rows(fruit)
>>> print("打印成表格\n", x)
out:
直接打印列表
[['apple', 'red', '100g'], ['banana', 'yellow', '80g']]
打印成表格
 +--------+--------+--------+
| fruit  | color  | weight |
+--------+--------+--------+
| apple  |  red   |  100g  |
| banana | yellow |  80g   |
+--------+--------+--------+
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> a = [[1, 2],
     [3, 4],
     [5, 6]]
>>> print(*a)
>>> print(zip(*a))
>>> print(list(zip(*a)))
out:
[1, 2] [3, 4] [5, 6]
<zip object at 0x7fb6cd7e7f00>
[(1, 3, 5), (2, 4, 6)]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
>>> a = [3, 2, None, "", 0]
>>> # 剔除假值(python中,None、0、"" 都为假”)
>>> print(list(filter(bool, a)))

>>> # 剔除 3
>>> print(list(filter(lambda i: i != 3, a)))

# 筛选出奇数
b = [1, 2, 3, 4, 5]
print(list(filter(lambda i: i % 2, b)))

out:
[3, 2]
[2, None, '', 0]
[1, 3, 5]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> import sys
>>> a = [3, 2, None, "", 0]
>>> b = 9
>>> c = "python"
>>> print("变量a占用的内存空间为{}字节,\n变量b占用的内存空间为{}字节,\n变量c占用内存空间为{}字        节。".format(
>>>     sys.getsizeof(a),
>>>     sys.getsizeof(b),
>>>     sys.getsizeof(c),
>>> ))

out:
变量a占用的内存空间为112字节
变量b占用的内存空间为28字节
变量c占用内存空间为55字节
1
2
3
>>> print(f"{5/2:.2f}")
out:
2.50
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import time
s = time.time()
np.sum(distance)
print(f"np求和耗时 {time.time() - s}")
sum = 0
total = distance.shape[0]
s = time.time()
for i in range(total):
    sum += distance[i]
print(f"循环求和耗时 {time.time() - s}")

np求和耗时 0.00003552436828613281e-05
循环求和耗时 0.0009284019470214844
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
distance = np.random.random((7532, 11314))
import time
s = time.time()
sort_idx = np.argsort(distance, axis=-1)  # 独立将每一行元素从小到达排列(即相似性从大到小)的下标 (7532, 11314)
print(f"np排序耗时{time.time() - s}")
s = time.time()
sort_idx = np.array(torch.argsort(torch.tensor(distance).cuda(), dim=-1).detach().cpu().numpy())
print(f"tensor排序耗时{time.time() - s}")
np排序耗时3.7687795162200928
tensor排序耗时3.2619576454162598