pythonスタック、キュー操作、文字列フォーマット出力、文字列共通関数サンプルコード
39548 ワード
"""
Created by cheng star at 2018/3/25 13:40
@email : [email protected]
"""
import heapq
import queue
import random
import collections
from collections import deque
import sys
import timeit
import string
"""
python
"""
def test_heap() :
"""
:return:
"""
print("run function >>>>>> {0}".format(sys._getframe().f_code.co_name))
print("####################### heappush ########################")
data = [x for x in range(10)] #
data_choice = random.choice(data) # random
print(data_choice)
random.shuffle(data) # random.shuffle(data)
print(data)
"""
:
:
"""
heap = [] #
for x in data :
heapq.heappush(heap , x) #
print(heap)
heapq.heappush(heap , 0.5)
print(heap)
data_pop = heapq.heappop(heap) # ,
print(data_pop)
print(heap)
data_pop = heapq.heappop(heap)
print(data_pop)
print(heap)
"""
, heapify
"""
print("####################### heapify ########################")
data_heap = [x for x in range(10)]
random.shuffle(data_heap) #
heapq.heapify(data_heap) # None
print(data_heap)
data_replace = heapq.heapreplace(data_heap , 5) # , ,
print(data_replace)
print(data_heap)
heap_nlargest = heapq.nlargest(3 , data_heap) # n
print(heap_nlargest)
heap_nsmallest = heapq.nsmallest(3 , data_heap) # n
print(heap_nsmallest)
def test_queue() :
"""
:return:
"""
print("run function >>>>>> {0}".format(sys._getframe().f_code.co_name))
print("####################### FIFO #####################")
q = queue.Queue() # FIFO
q.put(0) #
q.put(2)
q.put(1)
print(q.queue)
q_get = q.get() #
print(q_get)
print(q.queue)
print("####################### LIFO ( ) #####################")
q = queue.LifoQueue() # LIFO ( -> Stack )
q.put(0)
q.put(2)
q.put(1)
print(q.queue)
q_get = q.get()
print(q_get)
print(q.queue)
print("####################### PriorityQueue #####################")
q = queue.PriorityQueue(5) # ,
q.put(3)
q.put(2)
q.put(4)
q.put(1)
q.put(5)
print(q.queue)
q_get= q.get()
print(q_get)
print(q.queue)
print("####################### collections.deque #####################")
dq = deque() #
dq.append("zhao") #
dq.append("qian")
dq.append("sun")
dq.append("li")
dq.append("cheng")
dq.appendleft("wang") #
dq.appendleft("jiji")
print(dq)
pop_left = dq.popleft() #
print(pop_left)
print(dq)
pop_right = dq.pop() #
print(pop_right)
print(dq)
def test_zip() :
"""
zip
:return:
"""
print("run function >>>>>> {0}".format(sys._getframe().f_code.co_name))
l_1 = [x for x in range(10)]
l_2= [x for x in range(8)]
l_zip = zip(l_1 , l_2) # ,
for(k, v) in l_zip :
print("k = {0} , v = {1}".format(k , v)) #
# print(l_zip)
def test_str_format() :
"""
:return:
"""
num = 123
print("%+d" %num) #
print("%o" %num) #
print("%x" %num) #
print("%e" %num) #
print("######################## format #######################")
# {index}
print("The number {0:,} in hex is : {0:#x} , the number {1} in oct is {1:#o}".format(5555 , 55))
print("The number {1} in hex is : {1:#x} , the number {0} in oct is {0:#o}".format(5555 , 55))
# {keyword}
print("my nameis {name} , my age is {age} , my number is {number}".format(name = "star" , number = "10222953" , age = 26))
position = [1 , 2 , 3]
print("X : {0[0]} , Y : {0[1]} , Z : {0[2]}".format(position))
weather = [("Monday" , "rain") ,
("Tuesday" , "sunny") ,
("Wednesday" , "sunny") ,
("Thursday" , "rain") ,
("Frifay" , "cloud")]
formatter = "Weather of {0[0]} is {0[1]}".format
"""
help(map) => map(func , *iterables)
"""
for item in map(formatter , weather) :
print(item)
def test_str() :
"""
:return:
"""
s = "apple,peach,banana,peach,pear"
print("######################## #######################")
print(s.find("peach")) # ,
print(s.find("peach" , 7)) # index ,
print(s.find("peach" , 7 , 20)) # ,
print(s.rfind("peach")) # ( )
print(s.index("bana")) #
print(s.rindex("bana")) # ( ) ,
print(s.count("p")) #
print(s.count("apple"))
print(s.count("ppp"))
print("########################### ########################")
s_split = s.split(",") # , split() , ( 、 , )
print(s_split)
s_rsplit = s.rsplit(",") # ,
print(s_rsplit)
s_split_max = s.split("," , 2) #
print(s_split_max)
s_partition = s.partition("banana") # , ,
print(s_partition)
s_rpartition = s.rpartition("banana") # , ,
print(s_rpartition)
s_partition_none = s.partition("none") # ,
print(s_partition_none)
print("########################### ########################")
s = "today is a NICE day !!!"
print(s.lower()) #
print(s.upper()) #
print(s.capitalize()) # ,
print(s.title()) #
print(s.swapcase()) #
print("########################### ########################")
print(s.replace("today" , "tomorrow")) # , str1 str2
s = "today is a nice day !!!
tomorrow is rain !!!"
print(s.replace("
" , "")) #
print("########################### ########################")
s = "python is a great programming language . I like it !!!"
print(s)
table = ''.maketrans("abcdef123" , "uvwxyz@#$") # str1 str2
s_trans = s.translate(table) #
print(s_trans)
s = "678"
s_trans = s.translate(table) # ,
print(s_trans)
print("########################### ########################")
s = " python "
print("start{0}end".format(s.strip())) #
print("start{0}end".format(s.lstrip())) #
print("start{0}end".format(s.rstrip())) #
print("########################### startswith/endswith ########################")
s = "today is a nice day !!!"
print(s.startswith("today")) #
print(s.startswith("is" , 6)) # index
print(s.startswith("today" , 0 , 3)) #
print(s.endswith("!!!")) # XXX
s1 = "abc"
s2 = "bcd"
s3 = "efg"
print(s1.endswith(("abc" , "bcd" , "efg")))
print(s2.endswith(("abc" , "bcd" , "efg")))
print(s3.endswith(("abc" , "bcd" , "efg")))
print("########################### 、 、 、 ########################")
s_num = "123abc"
s_alp = "abc"
s_digit = "123"
s_space = " "
s_upper = "ABCDE"
s_lower = "abcd"
print(s_num.isalnum()) #
print(s_alp.isalpha()) #
print(s_digit.isdigit()) #
print(s_space.isspace()) #
print(s_upper.isupper()) #
print(s_lower.islower()) #
print("########################### ( ) ########################")
s = "Hello , Python"
print(s.center(20)) # ,
print(s.center(20 , "=")) # , ,
print(s.ljust(20 , "=")) # , ,
print(s.rjust(20 , "=")) # , ,
print("########################### ########################")
print(string.digits) #
print(string.ascii_letters) #
print(string.punctuation) #
print(string.ascii_lowercase) #
print(string.ascii_uppercase) #
print(string.printable) #
def generate_random_string() :
"""
:return:
"""
#
x = "".join([string.digits , string.ascii_lowercase , string.ascii_uppercase , string.punctuation])
random_count = random.randint(1 , len(x))
print(random_count)
random_str = "".join([random.choice(x) for i in range(random_count)])
print(random_str)
def test_timeit() :
"""
:return:
"""
times = 1000
print(timeit.timeit("'-'.join(str(n) for n in range(100))" , number = times))
print(timeit.timeit("'-'.join(map(str , range(100)))" , number = times)) # map
if __name__ == "__main__" :
test_heap()
test_queue()
test_zip()
test_str_format()
test_str()
test_timeit()
generate_random_string()
:Python