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

Python container

編輯:Python

One 、 character string

 

# Subscripts are also indexes
my_str = 'hello'
print(my_str[0]) # Negative number from -1 Start
print(my_str[-1])
# [start:end:step]
# step The default is 1, Indicates the interval between subscripts
# It doesn't contain end The corresponding subscript
print(my_str[2:4:1])
# end The position is not written , Said is len(), That is, you can get the last element
my_str3 = my_str[2:]
print(my_str3)
# start The position is not written , Express 0 Start
my_str4 = my_str[:3] # Be careful 3 Cannot get the position of
print(my_str4)
# start and end Don't write , But you need a colon
my_str5 = my_str[:]
print(my_str5)
print(my_str[-4: -1]) # -1 Position not available ell
print(my_str[3:1]) # No result output
print(my_str[3:1:-1], '2') # ll 2
print(my_str[::-1]) # The inverse of a string , olleh
print(my_str[::2]) # 0 2 4 h l o == my_str[0:5:2]

  String slice :

 

String lookup method :

find() & rfind()

my_str = 'hello world itcast and itcastcpp'
# find() Find out whether a string exists in the string
# my_str.find(sub_str,start,end)
# sub_str: What to find in the string , similar str
# start : Start looking for locations , Default 0
# end: End position , Find where it ends , The default is len()
# Return value : That is, what is the result of method execution , If you can find sub_str, Back to sub_str stay my_str The positive subscript of the position in
# If not found , Then the return -1
index = my_str.find('hello')
print(index) # hello stay my_str Subscript to be 0 The location of
# From the subscript for 3 Find the string at the position of hello
print(my_str.find('hello', 3))
print(my_str.find('itcast')) # 12 Add a space
print(my_str.find('itcast', 15)) # 23
# rfind() right find() From the right ( Back ) Start looking for
print(my_str.rfind('itcast')) # 23

index & rindex() ------ If it is not found, it will directly report an error

print(my_str.index('hello')) # 0
# print(my_str.index('hello', 5)) # error
print(my_str.rindex('itcast')) # 23
print(my_str.index('itcast')) # 12 Add a space , Subscript from 0 Start 

count()--- Count the number of times

print(my_str.count('aaaa')) # 0
print(my_str.count('hello')) # 1
print(my_str.count('itcast')) # 2
print(my_str.count('itcast'), 20) # 1 , from 20 After the itcast Number of occurrences 

String substitution replace

# my_str.replace(old_str, new_str, count)
# old_str: The string to be replaced
# new_str: String replaced with
# count: Number of replacements , Replace all by default
# Return value : Get a new string , It doesn't change the original string
my_str = 'hello world itcast and itcastcpp'
my_str1 = my_str.replace('itcast', 'itheima')
print('my_str:', my_str)
print('my_str1:', my_str1)
my_str2 = my_str.replace('itcast','itheima', 1) # Replace... Once
print('my_str2:%s' % my_str2)

String delimitation split()

# String segmentation split()
result = my_str.split()
print(result)
print(my_str.split('itcast')) # The default is to cut all
print(my_str.split('itcast', 1)) # cutting 1 Time
print(my_str.rsplit('itcast', 1)) # Cut from the far right 

String connection join()

# join()
# my_str.join( Iteratable object )
my_str = '_'.join('hello')
print(my_str)
print('_*_'.join('hello'))
# Definition list
my_list = ['hello', 'cpp', 'python']
print("_".join(my_list))
print("_*_".join(my_list))
print(" ".join(my_list))

Two 、list

1. What is a list ?

The list is python A data type in , Can store multiple data , The data in the list can be of any type

list list, Define the use of [], Define .

# Define an empty list
my_list = []
print(my_list, type(my_list)) # '[]' , <class 'list'>
my_list1 = list() # An empty list
print(my_list1, type(my_list1))
# Define a list with data , Data elements are separated by commas
my_list2 = [1, 3.14, True, 'isaac']
print(my_list2, type(my_list2)) # [1, 3.14, True, 'isaac'] <class 'list'>
# Find the number of data elements in the list , That is, the length of the list
num = len(my_list2)
print(num)
# The list supports subscript and slice operations
print(my_list2[1]) # 3.14
print(my_list2[-1]) # isaac
print(my_list2[1:3]) # [3.14,True] Because left closed right open
# The difference between subscript operation and string is : The data in the string cannot be modified by subscript , However, the list can use subscripts to modify the data in the list
my_list2[0] = 18
print(my_list2)
my_list2[-1] = 'hello'
print(my_list2)
my_list2[0] = 'python'
print(my_list2)
# Output results
D:\Python\Day03String\venv\Scripts\python.exe D:/Python/Day03String/list.py
[] <class 'list'>
[] <class 'list'>
[1, 3.14, True, 'isaac'] <class 'list'>
4
3.14
isaac
[3.14, True]
[18, 3.14, True, 'isaac']
[18, 3.14, True, 'hello']
['python', 3.14, True, 'hello']

2. Traverse

# Traverse
my_list = ['A', 'B', 'C', 'D']
for i in my_list:
print(i, end=' ')
print()
print('*' * 30)
j = 0
while j < len(my_list):
print(my_list[j], end=' ')
j += 1

 

3. Add data to the list

# Add data to the list
# How to add data to the list , Are added directly in the original list , No new list will be returned
my_list = ['A', 'B', 'C', 'D']
print(my_list)
# list .append( data ), Append data to the end of the list
my_list.append('aa')
print(my_list)
result = my_list.append('12') # Don't write like this
print(result) # Don't write like this , The result is ‘None’
print(my_list) # ['A', 'B', 'C', 'D', 'aa', '12']
# list .insert( Subscript , data ) Add data at the specified subscript position
my_list.insert(0, 'isaac')
print(my_list)
# print(my_list.insert(5, 3.14)) None
# list .extend( Iteratable object ) # str list , The data in the iteratable object will be added to the end of the original list one by one
my_list.extend('he')
print(my_list)
my_list.extend([1, 'python', 3])
print(my_list)

4. Data query operations in the list ( Not in the list find() Method , Only index() Method ) 

my_list = [1, 3.14, 'issca', False]
num = my_list.index(3.14) # 1
print(num)
# num1 = my_list.index(100) # Application error , Because the program doesn't exist
# count() Count the number of times
num3 = my_list.count(1)
print(num3)
# in/not in Judge whether it exists , Being is True, When there is no False, In general, and if Use a combination of
num4 = 3.14 in my_list # True
num4 = 3.14 not in my_list # False
print(num4)

5. Delete from the list

# Delete... According to subscript
# pop() The last data is deleted by default
num = my_list.pop() # 9
print(num)
print(my_list)
num = my_list.pop(2) # Delete the subscript as 2 The data of namely 5
print(num)
print(my_list)
# del_ list [ Subscript ]
del my_list[1] # Delete the subscript as 1 The data of 2
print(my_list)

 

6. Sorting and inversion of lists

# Want to sort the data in the list , The premise is that the data types in the list are the same
my_list = [1, 5, 3, 7, 9, 6]
# list .sort() Sort directly in the list
# my_list.sort() # The default is sort from small to large , In ascending order
# my_list.sort(reverse = True) # adopt reverse= True, From large to small
my_list1 = sorted(my_list)
my_list2 = sorted(my_list,reverse=True)
print(my_list)
print(my_list1)
print(my_list2)
my_list3 = ['a', 'b', 'c', 'd', 'e']
my_lise4 = my_list3[::-1]
print(my_list3)
print(my_lise4)
# Direct inverse value in the original list
my_list3.reverse()
print(my_list3)

 7. Nesting of lists

school_names = [[' Peking University, ', ' Tsinghua University '], [' Nankai University ', ' Tianjin University ', ' Tianjin Normal University '], [' Shandong University ', ' Ocean University of China ']]
print(school_names[1])
print(school_names[1][1])
print(school_names[1][1][1])
# Shandong University
print(school_names[2][0])
# All over
for schools in school_names:
for name in schools:
print(name)

3、 ... and 、 Tuples  

Interested in running your own code ...

# Tuples are very similar to lists , Can store multiple data , It can store data of different data types
# Difference : List with [], Tuples use ()
# The data in the list can be modified , Data in tuples cannot be modified
my_lists = [18, 3.14, True, 'issca']
my_tuple = (18, 3.14, True, 'issca')
print(my_tuple, type(my_tuple))
# Tuples support subscript and slice requirements
print(my_tuple[1])
# Define an empty array , It makes no sense
my_tuple1 = ()
print(my_tuple1, type(my_tuple1))
my_tuple2 = tuple()
# Elements that define a data element , Behind the data element , There must be a comma , Otherwise, it will report a writing error
my_tuple3 = (3) # 3 <class 'int'>
my_tuple4 = (3,)
print(my_tuple3, type(my_tuple3))
print(my_tuple4, type(my_tuple4))


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