代码如下:

  • 列表list_study)
  • 元组(tuple_study)
  • 字典(dictionary_study)
  • 字符串(string_study)

列表list_study)

name_list =["xiao li", "xiao hong", "xiao huang"]
# 定义列表print(name_list[0])
# 取值print(name_list.index("xiao li"))
# 取索引print(name_list)
name_list[1] = "xiao he"
print(name_list)
# 修改对应索引下的数据temp_list=["xiao he", "xiao xu"]
# 定义临时列表name_list.insert(1,"xiao wu")
print(name_list)
# 在指定索引插入数据name_list.append("xiao hei")
print(name_list)
# 在末尾追加数据name_list.extend(temp_list)
print(name_list)
# 将列表二追加到列表一print(temp_list)
# 列表二依旧是存在的temp_list.clear()
print(temp_list)
# 清空列表name_list.pop()
print(name_list)
# 删除末尾的一个数据name_list.pop(3)
print(name_list)
# 或者
del name_list[3]
print(name_list)
# 删除指定索引的数据len = len (name_list)
print(len)
#统计列表中的元素个数count = name_list.count("xiao he")
print(count)
# 列表中某个数据出现的次数name_list.sort()
print(name_list)
# 升序排序name_list.sort(reverse=True)
print(name_list)
# 降序排序name_list.reverse()
print(name_list)
# 列表逆序

元组(tuple_study)

info_tuple = ("xiao he", 1.80 , 70 , "xiao he")print(info_tuple[0])
# 取值print(info_tuple)
# 打印元组数据print(len(info_tuple))
# 元组长度,元组的元素个数print(info_tuple.index("xiao he"))
# 取元组对应数据的索引print(info_tuple.count("xiao he"))
# 数据在元组内的出现次数

字典(dictionary_study)

a_dict = {"name": "xiao li","age": 18,"sex": "female"}
# 定义字典temp_dict = {"list": [3, 2, 1,],"number": 3}
temp1_dict = temp_dict
temp2_dict = temp_dict.copy()
print(temp1_dict)
print(temp2_dict)
temp_dict["list"].remove(2)
print(temp1_dict)
print(temp2_dict)
# 比较拷贝与赋值的差别print(list(a_dict.items()))
print(a_dict.items())
# 获取字典的全部键值对print(list(a_dict.keys()))
print(a_dict.keys())
# 获取字典的键print(list(a_dict.values()))
print(a_dict.values())
# 获取字典的值print(a_dict.get("name"))
print(a_dict.get("xiao hei"))
print(a_dict.get("xiao hei", 0.0))
# 获取指定键的值 当键不存在时 返回None 或者自己设定的值print(len(a_dict))
# 获取字典键值对的数量print(a_dict.setdefault("name"))
print(a_dict.setdefault("xiao hei"))
print(a_dict)
print(a_dict.setdefault("xiao zi", 30))
print(a_dict)
# 返回指定key的value 没有该key则进行插入 有设置值还会一并插入对应值tuple = ("xiao hei", "xiao wu")
b_dict = dict.fromkeys(tuple, 50)
print(b_dict)
list = ["xiao lan", "xiao he"]
c_dict = dict.fromkeys(list, 50)
print(c_dict)
# 利用元组和列表创建字典a_dict.update(b_dict)
print(a_dict)
print(b_dict)
# 合并字典 若合并的字典有相同key 则括号里的字典的键值对会对被合并的字典进行覆盖b_dict.clear()
print(b_dict)
# 清空字典a_dict.pop("sex")
print(a_dict)
# 删除指定键的键值对a_dict.popitem()
print(a_dict)
# 随机删除一个键值对(以出栈的概念来说是有序但是我们无法查看入栈顺序所以随机)a_dict["sex"] = "female"
print(a_dict)
# 增加键值对print(a_dict["sex"])
# 取值

字符串(string_study)

string=" 5"
print(string.isspace())
string="     \n\r\t"
print(string.isspace())
# 判断字符串是否只含有空格 或空白字符temp_str = " Ab B"
print(temp_str.istitle())
temp_str = "  Ab 5B "
print(temp_str.istitle())
temp = "break Ab"
print(temp.istitle())
# 查看是否所有单词首字母是大写a = "2"
print(a.isupper())
a = "2abA"
print(a.isupper())
a = "2A"
print(a.isupper())
# 判断是否是英文字符且全为大写字母b ="2"
print(b.islower())
b = "2abA"
print(b.islower())
b= "2a"
print(b.islower())
# 判断是否所有英文字符都是小写c = ""
print(c.isalpha())
c = "2a"
print(c.isalpha())
c = "aA"
print(c.isalpha())
# 判断是否均为字母d = ""
print(d.isalnum())
d = "2a"
print(d.isalnum())
# 判断字符串是否均为字母或数字e = "26s_5"
print(e.isidentifier())
e = "a3"
print(e.isidentifier())
# 判断是否是有效标识符f = "\t"
print(f.isprintable())
f = "2"
print(f.isprintable())
# 判断所有字符是否都可打印g ="1"
print(g.isdecimal())
print(g.isdigit())
print(g.isnumeric())
print("")
g = "\u00b2"
print(g.isdecimal())
print(g.isdigit())
print(g.isnumeric())
print("")
g = "三十"
print(g.isdecimal())
print(g.isdigit())
print(g.isnumeric())
# 判断是否只包含数字h = "abc"
print(h.startswith("ab"))
print(h.startswith("abcd"))
print(h.endswith("bc"))
print(h.endswith("abcd"))
# 判断是否以指定开头结尾temp_str = "hello world llo"
print(temp_str.index("llo"))
print(temp_str.rindex("llo"))
print(temp_str.find("llo"))
print(temp_str.rfind("llo"))
print(temp_str.find("lloi"))
# 查找字符串在字符串中的位置print(temp_str)
b = temp_str.replace("llo", "LLO", 1)
print(b)
b = temp_str.replace("llo", "LLO")
print(b)
print(temp_str)
# 替换字符中部分字符串为指定字符串b = temp_str.capitalize()
print(b)
print(temp_str)
# 第一个字符转换为大写b = temp_str.title()
print(b)
print(temp_str)
# 首字母大写b= temp_str.upper()
print(b)
print(temp_str)
# 全部转换为大写c = b.lower()
print(c)
print(b)
temp_str = "AAAA"
print(temp_str.casefold())
# 全部转为小写d = "AfdafaDE"
f = d.swapcase()
print(d)
print(f)
# 大小写翻转a_list=["饮湖上初晴后雨\t","苏轼","水光潋滟晴方好","\t山色空蒙雨亦奇","欲把西湖比西子","淡妆浓抹总相宜"]
for i in a_list:print("|%s|" % i.lstrip().center(10, " "))
for i in a_list:print("|%s|" % i.rstrip().rjust(10, " "))
for i in a_list:print("|%s|" % i.strip().ljust(10, " "))
for i in a_list:print("|%s|" % i)
# 对字符串进行对齐输出 和去除空白符操作string = "ni hao wo hao da jia hao"
s = string.split(" ")
print(string.split(" "))
print(string.rsplit(" "))
# 分割字符串b_str = "w".join(s)
print(b_str)
# 以指定字符串进行连接print(b_str.count("ow"))
# 统计指定字符的出现次数temp_str = "ni wo ni wo ni"
print(temp_str.partition("wo"))
print(temp_str.rpartition("wo"))
# 用指定字符串将字符串分隔成三个元素的元组temp_str = "i\two\nyou\r"
print(temp_str.splitlines())
# 以 \r \n 作为分界 分成多个元素的列表intab = "aeiou"
outtab = "12345"
trantab = string.maketrans(intab, outtab)
# 制作翻译表
test = "ni hao"
deltab = "nh"
print(test.translate(trantab))
# 翻译print(test.zfill(10))
# 返回指定长度的字符串 右对齐 不足以0填充左边txt = "My name is Ståle"
print(txt.encode())
# 利用utf-8进行编码报错(questioned)???txt = "H\te\tl\tl\to"
print(txt)
print(txt.expandtabs())
# 将\t进行空格的替换

python中列表、元组和字典、字符串的方法汇总相关推荐

  1. 列表/元组/切片/字典/字符串处理方法

    1.列表 stus='赵传慧,贾丹,段家林,刘伟,梁盼,刘艳' new_stus = ['赵传慧','贾丹','张流量','李wifi','颜军田'] #列表.list.数组.array #下标.索引 ...

  2. python中列表 元组 字典 集合的区别

    参考文章:python中列表 元组 字典 集合的区别

  3. python中string什么意思_python字符串(string)方法整理

    C C语言开发 python字符串(string)方法整理 python中字符串对象提供了很多方法来操作字符串,功能相当丰富. print(dir(str)) [..........'capitali ...

  4. PYTHON自动化Day3-列表/元组/切片/字典/字符串处理方法

    1.列表: stus='赵传慧,贾丹,段家林,刘伟,梁盼,刘艳'new_stus = ['赵传慧','贾丹','张流量','李wifi','颜军田'] #列表.list.数组.array #下标.索引 ...

  5. 总结python中列表、元组、字典、集合的共同点和不同点

    前言: 今天就和大家只聊聊python中列表.元组.字典.集合的共同点和不同点,能力有限,写的不好的地方,请大家多多海涵! 列表list 列表和元组·都是有序的 # 元组与列表相似可以通过索引与切片获 ...

  6. python中列表字典和字符串的相互转化

    python中列表字典和字符串的相互转化有两种方法: (1)使用str和eval的方法,一个简单的例子如下: data = {'name' : 'ACME','shares' : 100,'price ...

  7. Python中列表和字符串的反转

    要求: 原列表:[13, 30, 42, 85, 9, 45] 反转后的列表:[45, 9, 85, 42, 30, 13] 原字符串:hello 反转后的字符串:olleh Python现成的反转功 ...

  8. 手写Python中列表和字符串的反转

    要求: 原列表:[13, 30, 42, 85, 9, 45] 反转后的列表:[45, 9, 85, 42, 30, 13] 原字符串:hello 反转后的字符串:olleh Python现成的反转功 ...

  9. python有哪几种基本数据类型_Python最基本的数据类型以及对元组的介绍 Python 中列表和元组有哪些区别...

    python的数据类型有哪些?道不同不相为谋,你讨厌我,我也未必喜欢你.各走各的人岂不是更潇洒何必咄咄逼人费了口舌也讨人嫌.你闲得慌,我可没空陪你. 1. 数字类型 Python数字类型主要包括int ...

  10. python列表和元组的应用,Python中列表和元组的使用方法和区别

    一.二者区别 列表: 1.可以增加列表内容 append 2.可以统计某个列表段在整个列表中出现的次数 count 3.可以插入一个字符串,并把整个字符串的每个字母拆分当作一个列表段追加到列表当中 e ...

最新文章

  1. mysql存储引擎6_Mysql各种存储引擎对比总结
  2. 李开复:AI行业正在回归商业本质,技术公司要有服务心态落地为王
  3. SQL Server执行计划的理解
  4. [转载] 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis
  5. 763. 划分字母区间(JavaScript)
  6. Skip level 1 on 1
  7. 计算机主机序列,SCCM任务序列自动根据SN号命名计算机
  8. 能转PDF格式用的在线软件
  9. 威纶通触摸屏232脚位_威纶通各系列触摸屏引脚排列.pdf
  10. Tesla Autopilot技术架构整理(引用自EatElephant)
  11. 重新发现科技与人文的互动
  12. 关于SVN安装目录下,没有svn.exe程序的解决
  13. Windows下的二进制文件工具
  14. Grafana变量介绍
  15. 关于设计(三)设计存在的意义
  16. GAMES101蒙特卡洛光线追踪及Assignment7
  17. 可以和你女朋友一起玩的 《扑克牌游戏》
  18. ubuntu联网_高云半导体EDA开发工具增加了对Ubuntu的支持,集成人工智能和物联网开发工具链...
  19. OSError: cannot write mode F as BMP
  20. Ubuntu中package ‘xxxx‘has no installtion candidate 解决办法

热门文章

  1. Windows 入门 AI 视觉处理 --- 搭建姿势识别应用 之 利用NVIDIA Maxine识别人体姿势
  2. (魔兽世界)老MT经验谈
  3. 火狐marquee_firefox 2 marquee兼容
  4. Android studio下进行9patch图片的编辑
  5. mysql代码生成器 java_auto-code
  6. 烤咸味小吃的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  7. 千万级流量 H5 应用涉及到的技能点(图片篇)
  8. 互联网公司消息的年终奖,成了大多数人的伤心地
  9. 常见红外遥控设备及协议简介
  10. 用编程给正处于高考考场的小伙伴们加油