python数组列表、字典、拷贝、字符串
python中字符串方法
name = "I teased at life as if it were a foolish game"
print(name.capitalize())#首字母大写
print(name.count("a"))#查找字符串中a的个数
print(name.center(50,"-"))#长度为50将name放中间不够的用-补全
print(name.endswith("ex"))#字符串是否以ex结尾 true \ false
print(name.expandtabs(tabsize=30))#将tab键转化为多少空格
print(name.find("k"))#字符串切片
print(name.format())#格式化输出
# print(name.format_map({'name'}))
print('lsjf342'.isalnum())#是否含有数字,有特殊符号不能检测
print('adK'.isalpha())#是否有大写字母
print(''.isdecimal())#是否为十进制数
print(''.isdigit())#是否为整数
print('a32f'.isidentifier())#是否为合法标识符
print(name.isspace())#是否为空格
print(name.istitle())#是否首字母大写
print(name.isprintable())#是否可以打印 字符串不考虑 tty file,drive file不可打印
print(name.isupper())#是否全为大写
print(''
'*'.join(['','','','']))#字符串拼接
print(name.ljust(80,"*"))#将字符串放在左边,长度不够80用*补全
print(name.rjust(10,""))
print(name.lower())#大写变小写
print(name.upper())#小写变大写
print('mark\n'.lstrip())#去左边的特殊符
print('\nmaek\n'.rstrip())
print('---')
p = str.maketrans("abcdef",'')#对应替换
print("mark".translate(p))
print(name.replace('a','A',2))#替换
print(name.rfind('A'))
print(name.split())#将字符串按照空格分成不同列表
print(name.split('r'))#按照r分成不同列表
print(name.splitlines())#按照换行分列表
print(name.swapcase())#大小写变换
print(name.title())#单词首字母大写
print(name.zfill(20))#不够20用0填充
结果输出:
1 D:\exploit\python\anaconda\python.exe D:/exploit/python/workSapce/day2/string.py
I teased at life as if it were a foolish game
5
--I teased at life as if it were a foolish game---
False
I teased at life as if it were a foolish game
-1
I teased at life as if it were a foolish game
True
True
True
True
True
False
False
True
False
1*2*3*4
I teased at life as if it were a foolish game***********************************
I teased at life as if it were a foolish game
i teased at life as if it were a foolish game
I TEASED AT LIFE AS IF IT WERE A FOOLISH GAME
mark maek
---
m1rk
I teAsed At life as if it were a foolish game
-1
['I', 'teased', 'at', 'life', 'as', 'if', 'it', 'were', 'a', 'foolish', 'game']
['I teased at life as if it we', 'e a foolish game']
['I teased at life as if it were a foolish game']
i TEASED AT LIFE AS IF IT WERE A FOOLISH GAME
I Teased At Life As If It Were A Foolish Game
I teased at life as if it were a foolish game Process finished with exit code 0
python中拷贝
import copy#完全复制需要导入的包 names = "ZhangYan Guyun DingXiaoPing"
names = ["ZhangYan","Guyuan","LiSi",["nothing","no"],"nothing","WangWu"] print(names[0:-1:2])
print(names[:])
print(names[::2])
for i in names:#切片
print(i) '''# name2 = names.copy() #浅拷贝
name2 = copy.deepcopy(names)#深层次拷贝
print(name2)
print(names)
names[2] = "橡皮"
names[3][1] = "神马"
print(names)
print(name2)#第一层拷贝的数据,第二层拷贝的是内存地址
''' '''names.append("nothing")#结尾插入
names.insert(2,"nomore")#任意位置插入
names[1] = "ZhangShan"#修改
print(names) # print(names.index("nothing"))#获得nothing的角标(第一次出现nothing的角标)
#
# print(names[names.index("nothing")])
#
# print(names.count("nothing"))#计数
#
# names.reverse()#反转
names.sort()#排序 ascii 码排序
print(names)
names2 = [1,2,3,4]
names.extend(names2)#合并
print(names,names2)
del names2#删除
print(names2)
# print(names[0],names[3])
# print(names[1:3])#取角标1和角标2
# print(names[2:])#取角标从2到结束
# print(names[-1])#取最后一位
# print(names[-2:])#取值后两位
# print(names[0:3])#零可以省略 # names.remove("nothing")
#
# del names[2]
# print(names)
#
names.pop()#括号内可以写角标
print(names)
'''
结果输出:
1 ['ZhangYan', 'LiSi', 'nothing']
['ZhangYan', 'Guyuan', 'LiSi', ['nothing', 'no'], 'nothing', 'WangWu']
['ZhangYan', 'LiSi', 'nothing']
ZhangYan
Guyuan
LiSi
['nothing', 'no']
nothing
WangWu
python字典
#key-value
info = {
'stu1001': "NWuTengLan",
'stu1002': "LongZeLuoLa",
'stu1003': "MaLiYa", }
print(info)
print(info["stu1002"])#存在字典中才可用这种方式查找,容易出错
info["stu1002"]="泷泽萝拉"
info["stu1005"]="泷泽萝拉"#存在就修改,不存在就创建
print(info)
print(info.get("stu1004"))#不出错的查找 print('stu1002' in info)#判断是否在字典中 #del
# del info["stu1005"]删除
# info.pop("stu1005")删除
# info.popitem()随缘删除
print(info)
运行结果:
1 {'stu1003': 'MaLiYa', 'stu1002': 'LongZeLuoLa', 'stu1001': 'NWuTengLan'}
LongZeLuoLa
{'stu1003': 'MaLiYa', 'stu1005': '泷泽萝拉', 'stu1002': '泷泽萝拉', 'stu1001': 'NWuTengLan'}
None
True
{'stu1003': 'MaLiYa', 'stu1005': '泷泽萝拉', 'stu1002': '泷泽萝拉', 'stu1001': 'NWuTengLan'}
python数组列表
输出数组第一层,让操作者输入数据,查看数据是否在第一层内,在的话就进入第二层,不在得话重新选择,以此类推。如果用户输入b返回,输入q退出
data = {
'china':{
"北京":{
"朝阳区":["区政府","七天酒店"],
"海淀区":["航空大学","地铁十号线"]
},
"河南":{
"郑州市":["桂林路","新区"],
"开封市":["民权","老城区"]
}, },
'USA':{
"加州":{},
"佛罗里达州":{}
}, } exitFlag = False while not exitFlag:
for i in data:
print(i)
choice = input("选择进入》》")
if choice in data:
for i2 in data[choice]:
print(i2)
choice2 = input("选择进入》》")
if choice2 in data[choice]:
for i3 in data[choice][choice2]:
print(i3)
choice3 = input("选择进入》》")
if choice3 in data[choice][choice2]:
for i4 in data[choice][choice2][choice3]:
print(i4)
choice4 = input("最后一层,按b返回》》")
if choice4 == "b" or choice4 == "B":
pass
elif choice4 =="q" or choice4 == "Q":
exitFlag = True#跳出循环
if choice3 == "b" or choice3 == "B":
pass#返回
elif choice3 == "q" or choice3 == "Q":
exitFlag = True
if choice2 == "b" or choice2 == "B":
break#跳出循环
elif choice2 =="q" or choice2 == "Q":
exitFlag = True
python数组列表、字典、拷贝、字符串的更多相关文章
- Python数组列表(List)
Python数组列表 数组是一种有序的集合,可以随时添加和删除其中的元素. 一.数组定义: 数组是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 数组的数据项不需要具有相同的类 ...
- 使用NSJSONSerialization将数组或字典转为字符串
IOS中将数组或字典转为字符串可以用NSJSONSerialization,代码如下: NSData* data = [NSJSONSerialization dataWithJSONObject:a ...
- Python【列表 字典 元组】
列表列表用中括号[ ]把各种数据框起来,每一个数据叫作“元素”.每个元素之间都要用英文逗号隔开各种类型的数据(整数/浮点数/字符串)————————————————————————————从列表提取单 ...
- python 将列表中的字符串转为数字
本文实例讲述了Python中列表元素转为数字的方法.分享给大家供大家参考,具体如下: 有一个数字字符的列表: numbers = ['1', '5', '10', '8'] 想要把每个元素转换为数字: ...
- python 列表,字典,元组,字符串,QuerySet之间的相互转换
1. 列表转换成字典list1 = ['key1','key2','key3']list2 = ['value1','value2'] dict1 = zip(list1,list2) # dict( ...
- Python 关于列表字典的键值修改
list (修改列表的索引值) 循环一个列表时,最好不要对原列表有改变大小的操作,这样会影响你的最终结果. #使用负索引进行修改列表 print('First') lis = [11, 22, 33, ...
- python 元组 列表 字典
type()查看类型 //取整除 **幂 成员运算符: in x在y序列中,就返回true 反之 not in 身份运算符: is is not 逻辑运算符 and or not 字符编码 问题 ...
- python中 列表 字典 元组的了解
#######列表######1.列表的特性 server = [['http'],['ssh'],['ftp']] server1 = [['mysql'],['firewalld']] 连接 ...
- [Swift通天遁地]五、高级扩展-(10)整形、浮点、数组、字典、字符串、点、颜色、图像类的实用扩展
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
随机推荐
- mysql 配置utf8 编码,支持 emoji 方法!!!
utf8_general_ci 已经 过时了...请以后用mysql 考虑使用 utf8mb4, utf8mb4_unicode_ci!!! 兼容性更好. mysql的utf8编码的一个字符最多3个字 ...
- BeautifulSoup4模块的使用
1. 安装 pip3 install beautifulsoup42. 使用 from bs4 import BeautifulSoup obj = BeautifulSoup("HTML内 ...
- SQL server数据库端口访问法
最近数据库连接,也是无意中发现了这个问题,数据库可根据端口来连接 我用的是sql2014测试的,在安装其他程序是默认安装了sql(sql的tcp/ip端口为xxx),服务也不相同,但是由于比较不全,我 ...
- Day03 知识点
一.单行注释与多行注释 # 用来标记不运行的程序 pycharm 快捷键 ctrl+/ 可以在程序上方 也可以在程序后面 (PEP8) 多行注释 用三引号,一般推荐三双引号来做注释. 二.数据类型 ( ...
- JS数组去重总结
方法一: 双层循环,外层循环元素,内层循环时比较值 如果有相同的值则跳过,不相同则push进数组 Array.prototype.distinct = function(){ var arr = th ...
- linux下pid命令
ps aux | grep tomcat| awk '{if(NR==1)print $2}' Linux:批量修改分隔符(awk.BEGIN.FS.OFS.print.tr命令) 批量修改文件的分 ...
- SpringBoot初始教程之Servlet、Filter、Listener配置
1.介绍通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个jar ...
- VS 2010 快捷键大全
Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...
- Beam概念学习系列之PCollection数据集
不多说,直接上干货! PCollection数据集 PCollection是Apache Beam中数据的不可变集合,可以是有限的数据集合也可以是无限的数据集合. 有限数据集,这种一般对应的是批处理 ...
- 自定义java代码快捷生成器使用与问题解决
对于很多的工作了有几年的开发人员来说,初期都是逐个单词语法的自己编写的.而一旦技术水平提高了到了一定的层次之后,在同时工作量的加大,要求我们必须加快提高工作效率.因此就可以利用必要的快捷开发手段和工具 ...