程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python algorithm practice week4 search algorithm

編輯:Python

0x01 list

list : Data sequence , Is a continuous storage space that can store multiple data .

  • List creation
a_list = [1,2,3]
a_list = ['this','is','a','list']
a_list = ['this','is','test',1,2,3]
a_list = list('hello')
# ['h', 'e', 'l', 'l', 'o']
a_list = list(range(1,20,2))
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
  • Access to the list
a_list = ['this','is','a','list']
print(a_list[0])
# this
print(a_list[-1])
# list
print(a_list[5])
# IndexError: list index out of range
print(a_list[1:3])
# ['is', 'a']
print(a_list[1:])
# ['is', 'a', 'list']
  • Traversal of list
a_list = ['this', 'is', 'a', 'list']
for x in a_list:
print(x)
for i in range(len(a_list)):
print(a_list[i])

0x02 In order to find

  • Basic implementation
list = list(range(1,20,2))
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
x = int(input(' Please enter an integer to find :'))
for i in range(len(list)):
if i == x:
print(' eureka , Integers {} In the list '.format(x))
  • To optimize , Add feedback when you can't find it
list = list(range(1, 20, 2))
print(list)
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
sign = False
x = int(input(' Please enter an integer to find :'))
for i in range(len(list)):
if list[i] == x:
print(' eureka , Integers {} In the list '.format(x))
sign = True
if not sign:
print(' I didn't find it ')
  • Data search correlation function
# Data search correlation function
list = list(range(1, 20, 2))
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
print(list.index(13))
# 6
# The first data returned is 13 The subscript
print(list.index(15, 2, 10))
# 7
# return 2 To 9(10-1) The first data between is 15 The subscript
temp_list = [1, 1, 1, 1, 1, 2, 3]
print(temp_list.count(1))
# 5
# The returned data is 1 The number of 
  • Use built-in functions to implement the above sequential search algorithm
list = [1, 2, 2, 3, 3, 3, 4, 4, 5, 6]
x = int(input(' Please enter an integer to find x:'))
n = list.count(x)
if n == 0:
print(' Did not find {}'.format(x))
else:
j = 0
for i in range(n):
j = list.index(x, j, len(list)) + 1
# Specify a range to find
print(' eureka , The first {} The number is {}'.format(j, x))

0x03 Two points search

  • Comparison between binary search and sequential search
  • python Realization
# Two points search
list = list(range(1, 20, 2))
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
x = int(input(' Please enter an integer to find :'))
start = 0
end = len(list) - 1
while start <= end:
mid = (start + end) // 2
if list[mid] == x:
break
elif x < list[mid]:
end = mid - 1
else:
start = mid + 1
if start <= end:
print(' eureka , The first {} The number is {}'.format(mid + 1, x))
else:
print(' Did not find ')

0x04 Add data

  • python Realization
list = [1, 3, 5, 7, 9, 2, 4, -1, -1]
# here -1 Said is empty , Represents a placeholder
x = int(input(' please enter an integer x:'))
i = len(list) - 1
while i >= 0:
if list[i] != -1:
break
i -= 1
list[i + 1] = x
print(list)
# [1, 3, 5, 7, 9, 2, 4, 1, -1]
  • Built in functions
# append( Add data to the end of the list )
list = list(range(1,10,2))
# [1, 3, 5, 7, 9]
x = int(input(' please enter an integer x:'))
list.append(x)
print(list)
# [1, 3, 5, 7, 9, 1]
# ---
# extend() Function to append multiple values in another sequence at the end of the list at one time ( Extend the original list with the new list ).
list = list(range(1,10,2))
# [1, 3, 5, 7, 9]
list.extend([1,2,3])
print(list)
# [1, 3, 5, 7, 9, 1, 2, 3]
# ---
# insert() Function is used to insert the specified object into the specified position in the list .
list = list(range(1, 10, 2))
# [1, 3, 5, 7, 9]
list.insert(1, 100)
# list.insert(index, obj)
print(list)
# [1, 100, 3, 5, 7, 9]

0x05 Delete data

  • python Realization
list = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
x = int(input(' Please enter an integer to delete x:'))
n = len(list)
for i in range(n):
if list[i] == x:
list[i] = -1
print(list)
# [-1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
  • List delete operation built-in function
# pop() Function to remove an element from the list ( Default last element ), And returns the value of that element
list = [1, 3, 5, 7, 9]
list.pop()
# [1, 3, 5, 7]
# Delete the last... By default
list.pop(1)
# [1, 5, 7]
# Delete the second element of the list
# ---
# remove() Function to remove the first match of a value in the list
list = [1, 3, 5, 7, 9]
list.remove(3)
# [1, 5, 7, 9]
  • problem : Delete all duplicate elements
list = [1, 1, 1, 1, 1, 1, 3, 5, 5, 5, 5, 5, 5, 5, 7, 9]
x = int(input(' Please enter an integer to delete :'))
n = list.count(x)
# n How many for the list x, perform n Time remove() Function to delete all in the list x value
for i in range(n):
list.remove(x)
print(list)
# [3, 5, 5, 5, 5, 5, 5, 5, 7, 9]

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved