1、关于Python中的复数,下列说法错误的是(C)
A、表是复数的语法是real + image j
B、实部和虚部都是浮点数
C、虚部必须后缀j,且必须小写
D、方法conjugate返回复数的共轭复数
分析:
A,Python中复数表达形式:real + image j/J;
B,Python实部和虚部均浮点类型;
C,虚部后缀为j或J;
D,方法conjugate返回复数的共轭复数,如1+2j调用此方法后变为1-2j;
2、What gets printed?( 4 )
nums=([1,1,2,3,3,3,4])
print(len(nums))
set 类型的特性是会移除集合中重复的元素,因此变量 nums 实际上等于:set中的数据不能重复,会自动去除重复的值
nums = {1, 2, 3, 4}
3、以下程序输出为:None 18
info = {'name':'班长', 'id':100, 'sex':'f', 'address':'北京'}
age = info.get('age')
print(age)
age=info.get('age',18)
print(age)
dict.get(key, value=None)
当value的值存在时返回其本身,当key的值不存在时返回None(即默认参数)。
5、已知a = [1, 2, 3]和b = [1, 2, 4],那么id(a[1])==id(b[1])的执行结果 ( TRUE )
print(id(a[1]) ==id (b[1])) True
print((a[1]) is (b[1])) True
1、is 比较两个对象的 id 值是否相等,是否指向同一个内存地址;
2、== 比较的是两个对象的内容是否相等,值是否相等
在python3.6中对于小整数对象有一个小整数对象池,范围不止在[-5,257)之间。我试了百万以上的数地址都是相同的。
id(object)是python的一个函数用于返回object的内存地址。但值得注意的是,python 为了提高内存利用效率会对一些简单的对象(如数值较小的int型对象,字符串等)采用重用对象内存的办法。
6、以上函数输出结果为: 一个 shape = (10,5) 的 one-hot 矩阵
import numpy as np
a = np.repeat(np.arange(5).reshape([1,-1]),10,axis = 0)+10.0
b = np.random.randint(5, size= a.shape)
c = np.argmin(a*b, axis=1)
b = np.zeros(a.shape)
b[np.arange(b.shape[0]), c] = 1
print b
>>> a = np.repeat(np.arange(5).reshape([1,-1]),10)
>>> a
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4])
>>> a = np.repeat(np.arange(5).reshape([1,-1]),10,axis=0)
>>> a
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
a = np.repeat(np.arange(5).reshape([1,-1]),10,axis = 0)+10.0
>>> a
array([[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.],
[ 10., 11., 12., 13., 14.]])