The second day of Python learning route: lists and strings

list method

CRUD (addition)

  1. .append # default increase
  2. .insert # Custom position list insertion
  3. .extend # Enter the sequence type in the brackets

Example 1:

.append # 增加列表

Code demo:

>>> wpon = [1,2,3,4,5,6]
>>> wpon
[1, 2, 3, 4, 5, 6]
>>> wpon.append('夏柔')
>>> wpon
[1, 2, 3, 4, 5, 6, '夏柔']

Example 1.2:

.insert # 自定义列表位置插入

Code demo:

>>> wpon.insert(0,'夏山如碧,怀柔天下')
>>> wpon
['夏山如碧,怀柔天下', 0, 1, 2, 3, 4, 5, 6, '夏柔']

Example 1.3:

.extend # 括号里面传入序列类型

Code demo:

>>> wpon.extend([7,8,9]) # 增加序列
>>> wpon
['夏山如碧,怀柔天下', 0, 1, 2, 3, 4, 5, 6, '夏柔', 7, 8, 9]

Add, delete, modify, check (delete)

  1. .pop # Delete from the end by default
  2. .pop(1) # Delete data at a custom location
  3. .remove # specify to delete
  4. .clear # delete all

Example 2.1:

.pop # 默认从最后面删除

Code demo:

>>> wpon.pop()
9
>>> wpon
['夏山如碧,怀柔天下',0, 1, 2, 3, 4, 5, 6, '夏柔', 7, 8]
原来列表: ['夏山如碧,怀柔天下',0, 1, 2, 3, 4, 5, 6, '夏柔', 7, 8,9]

Example 2.2:

.pop(1) # 自定义删除

Code demo:

>>> wpon = ['夏山如碧,怀柔天下',0, 1, 2, 3, 4, 5, 6, '夏柔', 7, 8,9]
>>> wpon.pop(1) # 删除位置1上的数字
0
>>> wpon
['夏山如碧,怀柔天下', 1, 2, 3, 4, 5, 6, '夏柔', 7, 8, 9]

Example 2.3:

.remove() # 这个括号里面不是填索引, 而是填数据

Code demo:

>>> wpon.remove(3)
>>> wpon
['夏山如碧,怀柔天下', 1, 2, 4, 5, 6, '夏柔', 7, 8, 9]
# 原来列表: ['夏山如碧,怀柔天下', 1, 2, 3, 4, 5, 6, '夏柔', 7, 8, 9]

Example 2.4:

.clear() # 删除所有

Code demo:

>>> wpon.clear() # 删除所有
>>> wpon
[]

Add, delete, modify, check (modify)

Example 3.1:

wpon[5] = 'list' # 代表第三位的字符换成list

Code demo:

>>> wpon
[6, 5, 4, 3, 2, 1]
>>> wpon[5] = 'list'
>>> wpon
[6, 5, 4, 3, 2, 'list']

CRUD (Check)

  1. .index # query the location of the specified data
  2. .index(6,3) # Find the number 6, find it from the position of 3
  3. .count # Query how many times the specified number appears in the list

Example 4.1:

.index # 查询指定的数据的位置

Code demo:

>>> wpon = [1,2,3,4,5,6,7,8,9]
>>> wpon
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> wpon.index(1) # 查询数字1在第几个位置
0
>>> wpon.index(9) # 查询数字9在第几个位置
8

Example 4.2:

.index(6,3) # 找数字6,从3的位置找

Code demo:

>>> wpon
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> wpon.index(6,3) 
5

Example 4.3:

.count # 查询指定的数字在列表中出现了几次

Code demo:

>>> wpon3 = [1,1,1,1,1,3,4,5,6,7,8,9,111,1] 
>>> wpon3.count(1) # 查询数字1在上面的列表出现了多少次
6

copy method

Example 5:

.copy # 复制a给b 例如: 2 = 1.copy()

Code demo:

>>> wpon = [1,2,3,4,5,6]
>>> wpon
[1, 2, 3, 4, 5, 6]
>>> wpon2
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'wpon2' is not defined

The above error is reported because there is no wpon2

>>> wpon2 = wpon.copy() # 把wpon的列表复制给wpon2
>>> wpon2 # 复制成功
[1, 2, 3, 4, 5, 6]

return method

Example 6:

.reverse # 会倒过来原来的列表

Code demo:

>>> wpon = [1,2,3,4,5,6] # 重新编写
>>> wpon.reverse() # 开始倒过来
>>> wpon
[6, 5, 4, 3, 2, 1]

Combined with simple print printing:

Explanation of Sequence Types-List-WordPress Minimalist Blog

It's okay if you don't understand... Follow up my learning process and take it slowly

I knocked one myself:
insert image description here
code:

wpon = [1,2,3,4,5,6]
wpon3 = [7,8,9,10]
wpon.insert(0,'夏山如碧,怀柔天下')
wpon.extend([7,8,9])
wpon.reverse()
wpon2 = wpon
w = wpon2+wpon
ww = w+wpon3
www = ww.index(6)
wwww = ww.index(5)
ww.append('666')
print(wpon)
print(wpon2)
print(wpon3)
print(w,"\n",ww)
print("查询结果6的出现结果:",www,"\n查询结果5的出现结果:",wwww)
print("教程结束")

output result:

[9, 8, 7, 6, 5, 4, 3, 2, 1, '夏山如碧,怀柔天下']
[9, 8, 7, 6, 5, 4, 3, 2, 1, '夏山如碧,怀柔天下']
[7, 8, 9, 10]
[9, 8, 7, 6, 5, 4, 3, 2, 1, '夏山如碧,怀柔天下', 9, 8, 7, 6, 5, 4, 3, 2, 1, '夏山如碧,怀柔天下'] 
 [9, 8, 7, 6, 5, 4, 3, 2, 1, '夏山如碧,怀柔天下', 9, 8, 7, 6, 5, 4, 3, 2, 1, '夏山如碧,怀柔天下', 7, 8, 9, 10, '666']
查询结果6的出现结果: 3 
查询结果5的出现结果: 4
教程结束
改变排序

Example seven:

.sort # 排序会改变原来的位置, 并且由小到大排序

Extension: sortable letters, numbers

Code demo:

>>> wpon = [1,2,3,4,5,6]
>>> wpon.reverse()
>>> wpon
[6, 5, 4, 3, 2, 1]
>>> wpon.sort() # 开始由小到大排序
>>> wpon
[1, 2, 3, 4, 5, 6]

Example 7.2:

.sort(reverse = True) # 排序会改变原来的位置, 并且由大到小排序

Code demo:

>>> wpon = [1,2,3,4,5,6]
>>> wpon.reverse()
>>> wpon
[6, 5, 4, 3, 2, 1]
>>> wpon.sort(reverse = True) # 开始由大到小排序
>>> wpon
[6,5,4,3,2,1]

View method

Example 8:

dir(wpon) # 查询所有方法

Code demo:

>>> dir(wpon)
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Yuanzu's method

Tip: Yuanzu belongs to immutable sequence type

  1. CRUD _
  2. .count # Query how many times a certain number appears in the entire Yuanzu?
  3. .index # query the location of the specified data

Example 1:

.index # 括号里面传入你想要查询的元素, 数据 得到下标 索引

Code demo:

>>> tu = (1,2,3,4,1,2,3) # tu的元祖为 (1,2,3,4,1,2,3)
>>> tu.index(2) # 查询从左往右数的数字2 的位置多少
1 # 和列表一样, 0,1,2,3,4,5,6 ... 
>>> tu.index(2,3) # 和列表一样, 查找数字2从3的位置找
5

Example 1.2:

.count # 查询在整个元祖里,某个数字出现了多少次?

Code demo:

>>> tu = (1,2,3,3,3,3,3,4,5,6,7,3,6,4,1,1,5,1,1)
>>> tu
(1, 2, 3, 3, 3, 3, 3, 4, 5, 6, 7, 3, 6, 4, 1, 1, 5, 1, 1)
>>> tu.count(3) # 在上面的元祖里, 3出现了6次
6

Tip written at the top: the tuple is an immutable object, but if you need to change it, convert it to a list

Conversion Tutorial:

>>> tu = (1,2,3,4,5,6)
>>> li = list(tu)
>>> tu
(1, 2, 3, 4, 5, 6)
>>> li
[1, 2, 3, 4, 5, 6]

Or use .copy to copy the past and change it

Note: There are only two methods of count and index in Yuanzu, which is convenient to find the data in Yuanzu

string

String splicing (understand first):

Extension: Strings can be added and multiplied, but not subtracted or divided;

addition:

>>> wpon = '夏柔'
>>> wpon2 = '很'
>>> wpon3 = '实用'
>>> wpon4 = '的'
>>> wpon5 = 'python学习路线'
>>> wpon6 = '教程'
>>> wpon+wpon4+wpon5+wpon6+wpon2+wpon3
'夏柔的python学习路线教程很实用'

multiplication:

>>> wpon*100
'夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔夏柔'

String (check):

  1. .find # Query the position of numbers/letters
  2. .index # same as list
  3. .count # same as list

Example 1:

.find # 查询 数字 / 字母 的位置

Code demo:

>>> wpon = 'xiarouya' 
>>> wpon.find('a',1) # 和列表一样, 从位置1往右查a在第几个
2

Extension: If .find cannot be found, no error will be reported, but if index cannot be found, an error will be reported

Analysis: wpon.find('a',1) # The first 'a' is the element you want to find, and the second 1 is where to find it

String (change)

  1. .isdigit() # Determine whether the value of the string is a number, if it returns True, if not return False
  2. .isalpha() # Determine whether the value of the string is all letters/Chinese, if it is return True, if not return False
  3. .endswish() # Determine whether the end of the letter is the same as the end of the value of the string, if it is true, return False if not
  4. .startswith() # Determine whether the beginning of the letter is the same as the beginning of the value of the string, if it is true, return True, if not return False
  5. .islower() # Determine whether the values ​​in the string are all lowercase, if so return True, if not return False
  6. .isupper() # Determine whether the values ​​in the string are all uppercase, if so return True, if not return False

Example 2:

.isdigit() # 判断字符串的值是否都是数字, 如果是返回True, 如果不是返回False

Code demo:

>>> wpon = 'wpon2' # 必须是纯数字, 包含字母返回就是False
>>> wpon.isdigit()
False
>>> wpon = '123'
>>> wpon.isdigit()
True

Example 2.2:

.isalpha() # 判断字符串的值是否都是字母 / 中文, 如果是返回True, 如果不是返回False

Code demo:

>>> wpon = 'wpon2'
>>> wpon.isdigit()
False
>>> wpon = '123'
>>> wpon.isdigit()
True
>>> wpon = 'wpon1'
>>> wpon.isalpha()
False
>>> wpon = 'wpon'
>>> wpon.isalpha()
True
>>> '夏柔'.isalpha()
True
>>> wpon = '夏柔'
>>> wpon.isalpha()
True

Example 2.3:

.endswith() # 判断字母结尾是否为字符串的值结尾相同 , 如果是返回True, 如果不是返回False

Code demo:

>>> wpon = 'wponwpon'
>>> wpon.endswith('w')
False
>>> wpon.endswith('n')
True

Example 2.4:

.startswith() # 判断字母开头是否为字符串的值开头相同, 如果是返回True, 如果不是返回False

Code demo:

>>> wpon = 'wponxiarouwww.wpon.cn'
>>> wpon.startswith('w')
True
>>> wpon.startswith('n')
False

Example 2.5:

.islower() # 判断字符串中的值是否都是小写, 如果是返回True, 如果不是返回False

Code demo:

>>> wpon = 'wponW'
>>> wpon.islower()
False
>>> wpon = 'wpon'
>>> wpon.islower()
True

Example 2.6:

.isupper() # 判断字符串中的值是否都是大写, 如果是返回True, 如果不是返回False

Code demo:

>>> wpon = 'wponW'
>>> wpon.isupper()
False
>>> wpon = 'WPON'
>>> wpon.isupper()
True

change of string(2)

  1. .upper() # Convert all letters to uppercase
  2. .lower() # Convert all letters to lowercase
  3. .strip() # Remove extra spaces on both sides
  4. .capitalize() # capitalize the first letter
  5. .title() # Convert the first letter of each word (one separated by a space) to uppercase
  6. .split() # After cutting the specified letter, it will be automatically replaced with a list
  7. .split('a',3) # In the list, assuming there are ten a's, only the first three will be removed from left to right, and the rest will continue to output
  8. .replace() # replace all selected values ​​in the list

Example 3:

.upper() # 将所有字母转换成大写

Code demo:

>>> wpon = 'WPON'
>>> wpon.isupper()
True
>>> wpon = 'wpon'
>>> wpon.upper()
'WPON'

But it will not be saved after conversion, you need to give the variable value

Code demo:

>>> wpon = 'wpon'
>>> w = wpon.upper()
>>> w
'WPON'

Example 3.2:

.lower() # 将所有字母转换成大写

Code demo:

>>> wpon = 'WPON'
>>> wpon
'WPON'
>>> wpon.lower()
'wpon'
>>> wpon
'WPON'

But it will not be saved after conversion, you need to give the variable value

Code demo:

>>> wpon = 'WPON' 
>>> w = wpon.lower() 
>>> w 
'wpon'

Example 3.3:

.strip() # 去除两边多余的空格

Code demo:

>>> wpon = '    wpon    wpon    '
>>> wpon
'    wpon    wpon    '
>>> wpon.strip()
'wpon    wpon'

Separately strip left spaces:

>>> wpon
'    wpon    wpon    '
>>> wpon.lstrip() # lstrip 代表拼音left缩写
'wpon    wpon    '

Separately remove spaces on the right:

>>> wpon
'    wpon    wpon    '
>>> wpon.rstrip() # rstrip 代表拼音right缩写
'    wpon    wpon'

Because none of the above is saved, as before, the variable can be

For example: w = wpon.lstrip() ie w = wpon.rstrip ...

Example 3.4:

.capitalize() # 给首字母大写

Code demo:

>>> wpon = 'wpon wpon wpon'
>>> wpon.capitalize()
'Wpon wpon wpon'

Example 3.5:

.title() # 在每个字符串(空格隔开算1个) 的首字母转换成大写

Code demo:

>>> wpon = 'wpon wpon xiarou lovexiarou '
>>> wpon.title()
'Wpon Wpon Xiarou Lovexiarou '

Example 3.6:

.split() # 切割指定字母后, 自动换成列表

Explanation of Sequence Type-String-WordPress Minimalist Blog

Code demo:

wpon = 'wpxhswp55p'
w = wpon.split('p')
 
print(w)
输出结果: ['w', 'xhsw', '55', '']

Of course, if the ending p is removed, the output will not have ' ';

Example 3.7:

If you need to remove a custom number of letters, it is:

.split('a',3) # 在列表中, 假设出现十个a, 则从左往右数只去除前三个, 剩下继续输出

Code demo:

Explanation of Sequence Type-String-WordPress Minimalist Blog

wpon = 'a666a555a444a333' # 总共有4个a
w = wpon.split('a',2)
print(w)

Then the print result is: ['', '666', '555a444a333'] The first two a's have been removed

Example 3.8:

.replace() # 替换在列表中所有被选中的值

Code demo:

Explanation of Sequence Type-String-WordPress Minimalist Blog

wpon = 'wpon   wpon   wpon   ' # 每次空4次格
w = wpon.replace('   ','') # 将空格3次输入, 转换成不需要空格的 ''
print(w)

Intimate tip: You can replace it with something else~

String escape

String escape meaning

  • \n newline
  • \t horizontal tab
  • \b backspace
  • \r Enter, move the current position to the beginning of the line
  • \ stands for backslash
  • ' represents a single quote
  • \0 represents a null character
  • \a system beep

How to type:

\n : print(" 520\n520")
\t : print(" 520\t521")
\r : print(" 520\r522")
\\ : print(" 520\\523")
\' : print(" 520\'524")
\0 : print(" 520\0525")
\a : print(" 520\a526")

The writing is not very good, just take notes to record the process of learning python, I hope strangers can give me a thumbs up~

Guess you like

Origin blog.csdn.net/YSP050310/article/details/110194614