python中imag⽤法_花了⼀晚上时间,终于把Python的基本⽤
法归纳好了!
⼀、内置函数
1. complex([real[,imag]])
返回⼀个复数,实部 + 虚部*1j,或者把字符串或者数字转成复数形式。
参数可以是复数表达式,也可以是字符串。当参数是字符串的时候,数字与操作符之间不能有空格。即comple('1 + 2j')是错误的。
print(complex(1, 2))
print(complex(1 + 2j))
print(complex('1+2j'))
# 输出 1+2j
print(complex(1))
# 输出 1+0j
满⾜:实部 + 虚部*1j 的数被称为复数。
a = 1 + 3j
# 求实部
al)
# 求虚部
print(a.imag)
# 求共轭
jugate())
2. chr(i) 与 ord(i)
chr(i) 是将当前整数 i 转成对应的 ascii 字符,可以是⼗进制,也可以是⼗六进制,其中0 <= i <= 0x10ffff (1114111)。其对应的逆操作为 ord(i),i 为 ascii 字符。
下⾯的函数演⽰如何求⼀个可迭代对象的 ascil字符 或者其对应的数值。注意函数 ordplus ,参数 x 中的每⼀个元素必须是单个字符,如果是列表,形式如下:[‘P’ , ‘y’, ‘t’ , ‘h’, ‘o’ , ‘n’]。
def chrplus(x):
chr_string = ''
for elem in x:
chr_string += chr(elem)
return chr_string
def ordplus(x):
ord_list = []
for elem in x:
return ord_list
x = 'Python⾼效编程'
temp = ordplus(x)
print(temp)
# 输出:[112, 121, 116, 104, 111, 110,
# 39640, 25928, 32534,31243]
init = chrplus(temp)
print(init)
# 输出:Python⾼效编程
返回 enumerate 对象。迭代对象必须是序列,迭代器,或者其他⽀持迭代的对象。enmerate() 函数返回的是迭代器,同样是可迭代对象。每次迭代的元素,都包含元素在序列⾥的序号(strat 默认值为 0) 和元素对应值。因此,我们可以⽤ for 循环获取返回值。
等价于:
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1
for i, elem in enumerate(['P', 'y', 't', 'h', 'o', 'n']):
print(i, elem)
4. abs(x)
返回数的绝对值。参数可以是整数或者浮点数。如果参数是复数,返回复数的模。Python 中虚数⽤数值加上字符 j 的形式表⽰。要注意 j 前⾯的数值不能省略,⽐如 1j。
下⾯是我写的简易版的 abs 函数:
from math import sqrt
def naive_abs(x):
# isinstance 判断参数x是否为整数或浮点数
if isinstance(x, int) or isinstance(x, float):
if x < 0:
x = - x
# 判断参数x是否为复数
elif isinstance(x, complex):
# x.real 复数的实部
# x.imag 复数的虚部
imag = x.imag
# 求复数的模
x = sqrt(real ** 2 + imag ** 2)
else :
return '请输⼊ int float complex'
return x
print(abs(3+4j))
print(naive_abs(3+4j))
# 输出 5.0
print(abs(-6))
print(naive_abs(-6))
# 输出 6
⼆、算法与数据结构
1. ⼆分查
要想使⽤⼆分搜索,⾸先要确保迭代序列是有序的。对于⽆序序列,我们⾸先要进⾏排序操作。
每次循环缩⼩⼀半搜索范围,时间复杂度为 O(logn)。每次循环,⽐较选取的中间数与需要查的数字,如果待查数⼩于中间数,就减少右界⾄中间数的前⼀个数;如果待查数⼤于中间数,就增加左界到中间数后⼀个数;如果待查数等于中间数,返回中间数的下标,该下标即为待查数在序列中的位置。当左界⼤于右界时,循环结束,说明序列中并没有待查数。
def binary_search(item, find):
# 有序可迭代对象
left, right = 0, len(item) - 1
mid = left + (right - left) // 2
while left <= right:
if item[mid] == find:
return mid
elif item[mid] > find:
right = mid - 1
else:
left = mid + 1
mid = left + (right - left) // 2
return None
seq = [1, 4, 7, 9, 13, 17, 18, 21, 34, 45, 65]
binary_search(seq, 13)
# 输出:4
⾸先要打乱序列顺序 ,以防算法陷⼊最坏时间复杂度。快速排序使⽤“分⽽治之”的⽅法。对于⼀串序
列,⾸先从中选取⼀个数,凡是⼩于这个数的值就被放在左边⼀摞,凡是⼤于这个数的值就被放在右边⼀摞。然后,继续对左右两摞进⾏快速排序。直到进⾏快速排序的序列长度⼩于 2 (即序列中只有⼀个值或者空值)。
# quicksort
index复数import random
def quicksort(seq):
if len(seq) < 2:
return seq
else:
base = seq[0]
left = [elem for elem in seq[1:] if elem < base]
right = [elem for elem in seq[1:] if elem > base]
return quicksort(left) + [base] + quicksort(right)
seq = [9, 8, 7, 6, 5, 4, 3]
random.shuffle(seq)
# seq:[6, 4, 9, 3, 8, 5, 7]
print(quicksort(seq))
# 输出:[3, 4, 5, 6, 7, 8, 9]
3. 冒泡排序
冒泡排序(顺序形式),从左向右,两两⽐较,如果左边元素⼤于右边,就交换两个元素的位置。其中,每⼀轮排序,序列中最⼤的元素浮动到最右⾯。也就是说,每⼀轮排序,⾄少确保有⼀个元素在正确的位置。这样接下来的循环,就不需要考虑已经排好序的元素了,每次内层循环次数都会减⼀。其中,如果有⼀轮循环之后,次序并没有交换,这时我们就可以停⽌循环,得到我们想要的有序序列了。
def bouble_sort(sequence):
seq = sequence[:]
length = len(seq) - 1
i = j = 0
flag = 1
while i < length:
j = 0
while j < length - i:
if seq[j] > seq[j + 1]:
seq[j], seq[j + 1] = seq[j + 1], seq[j]
flag = 0
j += 1
if flag:
break
i += 1
return seq
4. 选择排序
选择排序,每次选择当前序列的最⼩值,将其与当前序列的第⼀个元素交换位置,每迭代⼀次,当前序列长度减⼀。迭代结束,即可得到有序序列。
def find_minimal_index(seq):
min_elem = seq[0]
count = 0
min_elem_index = count
for elem in seq[1:]:
count += 1
if elem < min_elem:
elem, min_elem = min_elem, elem
min_elem_index = count
return min_elem_index
def select_sort(sequence):
# 选择排序
seq = sequence[:]
length = len(seq)
for i in range(length):
index = find_minimal_index(seq[i:])
seq[index + i], seq[i] = seq[i], seq[index + i]
return seq
5. 去重序列重复元素
⾸先新建⼀个集合 set,对于序列中的元素,如果已经在集合中了,我们就不返回这个值。如果不在集合中,就向集合添加这个元素,并返回这个值。key 是函数名,通过修改 key,我们可以改变重复元素的判断依据。⽐如对于下⾯这个序列:a = [{'a': 6, 'b': 4}, {'a': 6, 'b': 3}, {'a': 6, 'b': 4},{'a': 8, 'b': 12}]list(dedupe(a, lambda x: x['a']))这⾥我们把 dedupe 设置为,基于关键字 ‘a’ 对应值去除重复元素,也就是说集合中添加的元素为关键字 ‘a’ 对应值。输出为:[{'a': 6, 'b': 4}, {'a': 8, 'b': 12}]list(dedupe(a, lambda x: (x['a'],x['b'])))这⾥,集合添加的是关键字’a’和’b’对应值的元组。
输出为: [{'a': 6, 'b': 4}, {'a': 6, 'b': 3}, {'a': 8, 'b': 12}]
# Python⾼效编程
def dedupe(sequence, key):
# 依序去除重复元素
seen = set()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。