随笔分类 - python
摘要:1、提取第一列 [root@PC1 test2]# ls a.txt test.py [root@PC1 test2]# cat a.txt ## 测试数据 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
阅读全文
摘要:1、问题 [root@PC1 test2]# pip install numpy bash: pip: command not found... 2、系统 [root@PC1 test2]# cat /etc/system-release Rocky Linux release 8.5 (Green
阅读全文
摘要:1、 >>> a = [111, 222, 333] >>> "-".join(a) ## 数值类型报错 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sequence item 0
阅读全文
摘要:1、 [root@PC1 test2]# ls a.txt test.py [root@PC1 test2]# cat a.txt ## 测试数据 dsfs JHDSF ADFD dfesr fKHJ FJfdf [root@PC1 test2]# cat test.py ## 转换程序 #!/us
阅读全文
摘要:1、输出文本的前三行 [root@PC1 test2]# ls a.txt test.py [root@PC1 test2]# cat a.txt ## 测试数据 1 u j d 2 s f f 3 z c e 4 z v k 5 c f y 6 w q r 7 a d g 8 e f s [roo
阅读全文
摘要:1、小写转换为大写 [root@PC1 test]# ls a.txt test.py [root@PC1 test]# cat a.txt U R D S F E A D E x v m e f x e r d z d v [root@PC1 test]# cat test.py ## 小写转换为
阅读全文
摘要:1、 [root@PC1 test]# ls a.txt test.py [root@PC1 test]# cat a.txt U R D S F E A D E j k x v m j e f x e r d k j k u z d v [root@PC1 test]# cat test.py #
阅读全文
摘要:1、提取以x开头的行 [root@PC1 test]# ls a.txt test.py [root@PC1 test]# cat a.txt u r d s f e a d e x v m e f x e r d z d v [root@PC1 test]# cat test.py ## 提取以x
阅读全文
摘要:1、 [root@PC1 test]# ls a.txt test.py [root@PC1 test]# cat a.txt u r d s f e a d e x v m e f x e r d z d v [root@PC1 test]# cat test.py ## 提取程序,提取文本中包含
阅读全文
摘要:1、 >>> a = ["aa", "xx", "mm", "xx", "xx", "kk", "aa"] >>> a ['aa', 'xx', 'mm', 'xx', 'xx', 'kk', 'aa'] >>> a.count("xx") ## 统计指定元素的次数 3 &
阅读全文
摘要:1、 >>> a = [1, 2, 3, 4, 5, 6] >>> a [1, 2, 3, 4, 5, 6] >>> a[2:] ## 删除列表的前两个元素 [3, 4, 5, 6] 2、 >>> a [1, 2, 3, 4, 5, 6] >>> a[:len(a)-2] ## 删除列表中的后两个元
阅读全文
摘要:1、 >>> a = ['bb', 'aa', 'aa', 'dd', 'aa', 'mm', 'yy', 'aa'] ## 测试列表 >>> b = [] >>> begin = 0 ## 设定查找起始位置 >>> for i in range(0, a.count("aa")): ## 限定循环
阅读全文
摘要:1、for 循环实现 >>> a ['aa', 'bb', 'aa', 'cc', 'bb', 'dd', 'cc'] >>> b = list() >>> b [] >>> for i in a: ... if i not in b: ## 利用for循环 + 关系成员操作符去重复 ... b.a
阅读全文
摘要:1、方法1 >>> a = ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj"] >>> a ['aa', 'bb', 'cc', 'dd', 'ee', 'ff', 'gg',
阅读全文
摘要:1、 >>> import random >>> a = ["aaa", "bbb", "ccc", "ddd"] >>> random.choice(a) ## 从列表中随机抽取元素 'aaa' >>> random.choice(a) 'ddd' >>> random.choice(a) 'aa
阅读全文
摘要:1、方法1 >>> a = ["aaa", "bbb", "ccc", "ddd", "eee", "fff"] >>> a ['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'] >>> temp = a[0] ## 交换位置1和位置3的元素, 使用第三方变量,避免覆
阅读全文
摘要:001、 >>> test ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'] >>> test[-1] ## 逆向输出 'ff' >>> test[-2] 'ee' 002、 >>> test = ["aa", "bb", "cc", "dd", "ee", "ff
阅读全文
摘要:001、replace:将字符串中的指定字符替换为另一个字符 >>> a = "abcd_abcd_abcd_abcd" >>> a 'abcd_abcd_abcd_abcd' >>> a.replace("a", "x") ## 将字符串中的指定字符替换为另一个字符 'xbcd_xbcd_xbcd
阅读全文
摘要:001、lower:将所有字母转换为小写(同casefold) >>> a = "ABCD" >>> a.lower() ## 将所有的字母转换为小写 'abcd' >>> b = "abCD" >>> b.lower() 'abcd' 002、upper:将所有的字母转换为大写 >>> a = "
阅读全文
摘要:1、 root@PC1:/home/test2# ls a.txt test.py root@PC1:/home/test2# cat a.txt ## 测试数据 z u x e j z e f a z x e w f e root@PC1:/home/test2# cat test.py ## p
阅读全文

浙公网安备 33010602011771号