Python/traversals/--sort

本文深入探讨了Python编程中的各种技巧和实践,包括打印函数的详细解析、数据类型检查、自定义类的创建、列表操作、多进程管理及锁机制的运用等。通过具体示例,读者可以了解到如何有效利用Python进行数据处理、排序算法实现以及多线程编程。

Python/traversals/

help(print)
Help on built-in function print in module builtins:

print(…)
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

print(“Enter the value of the root node: “, end=””)
Enter the value of the root node:

check = input().strip().lower()
RUFD

check
‘rufd’

isinstance(4,object)
True

def in_order(node):
if not isinstance(node,object):#false,不执行return,直接执行下一句
return
print(“node.data”)

in_order(4)
node.data

def in_order(node):
if isinstance(node,object):#ture,执行return后跳出
return
print(“node.data”)

in_order(4)

data=6
class TreeNode:
def init(self, data):
self.data = data
self.right = None
self.left = None

tree_node = TreeNode(data)
tree_node
<main.TreeNode object at 0x006CF9D0>

tree_node()
Traceback (most recent call last):
File “<pyshell#231>”, line 1, in
tree_node()
TypeError: ‘TreeNode’ object is not callable

tree_node.data
6

tree_node.right
tree_node = TreeNode(3)
tree_node.data
3

check = input().strip().lower()
9

left_data = int(check)
left_data
9

isinstance(left_data,int)
True

a= input().strip().lower()
7

isinstance(a,int)
False

Python/sorts/Odd-Even_transposition_parallel.py

for i in range(10, 0, -1):
print(i)

10
9
8
7
6
5
4
3
2
1

arr = []
for i in range(10, 0, -1):
arr.append(i)

print(*arr)
10 9 8 7 6 5 4 3 2 1

arr
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

print(arr)
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

##Process是啥函数from multiprocessing import Process, Pipe, Lock

Process(target = abs, args = (-1,-3,4))
<Process(Process-5, initial)>

processArray=[]
processArray.append(Process(target = abs, args = (-1,-3,4)))
processArray
[<Process(Process-6, initial)>]

#lock used to ensure that two processes do not access a pipe at the same time

processLock = Lock()
processLock.acquire()
True

processLock.release()

Python/sorts/Odd-Even_transposition_single-threaded.py

list
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

arr=list
for i in range(0, 10):
for i in range(i % 2, 10 - 1, 2):
if arr[i + 1] < arr[i]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
print(*arr)

9 10 7 8 5 6 3 4 1 2
9 7 10 5 8 3 6 1 4 2
7 9 5 10 3 8 1 6 2 4
7 5 9 3 10 1 8 2 6 4
5 7 3 9 1 10 2 8 4 6
5 3 7 1 9 2 10 4 8 6
3 5 1 7 2 9 4 10 6 8
3 1 5 2 7 4 9 6 10 8
1 3 2 5 4 7 6 9 8 10
1 2 3 4 5 6 7 8 9 10

Python/sorts/bogo_sort.py

user_input =input(‘Enter numbers separated by a comma:\n’).strip()
Enter numbers separated by a comma:
-3,9,-5,8

unsorted = [int(item) for item in user_input.split(’,’)]
unsorted
[-3, 9, -5, 8]

while not True:#假的不执行
print(“yes not true”)

while not False:#真的就执行
print(“yes not true”)
break

yes not true

Python/sorts/bubble_sort.py
##break不可以单独用在if中,必须配合循环中使用来终止循环

swapped = False

if not swapped:
print(“break”)

break

if not swapped:
break
SyntaxError: ‘break’ outside loop

for i in range(3):
swapped = False
if not swapped: break

Python/sorts/bucket_sort.py

unsorted
[-1.0, 4.0, -8.0, 7.0]

my_list=unsorted
min_value, max_value = (min(my_list), max(my_list))
min_value, max_value
(-8.0, 7.0)

bucket_count
4.0

[[] for _ in range(int(bucket_count))]
[[], [], [], []]

buckets
[[-8.0, 9.0], [-1.0, 2.0], [4.0, 5.0], [7.0, 7.0]]

sorted([buckets[i][j] for i in range(2)
for j in range(2)])
[-8.0, -1.0, 2.0, 9.0]

print([buckets[i][j] for i in range(2)
for j in range(2)])
[-8.0, 9.0, -1.0, 2.0]

print([buckets[i][j] for i in range(4)
for j in range(2)])
[-8.0, 9.0, -1.0, 2.0, 4.0, 5.0, 7.0, 7.0]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值