美文网首页
Python基础学习Day4--元组、列表、字符串

Python基础学习Day4--元组、列表、字符串

作者: 怕热的波波 | 来源:发表于2020-07-29 00:06 被阅读0次

1.列表

x = [1,2,3,4,5,6]
print(x, type(x))
#[1, 2, 3, 4, 5, 6] <class 'list'>

x = list(range(10))
print(x)
print('-----------------')
x = [i for i in range(10)]
print(x)
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#-----------------
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

x = [[col for col in range(4)] for row in range(3)]
print(x)
#[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]

list可修改,附加append,extend,insert,remove,pop

  • append添加一个值
  • extend添加多个值到最后
  • insert(index, obj)
  • remove移除第一个匹配的值,指定值
  • pop可以指定位置,移除并返回对应值
x = [1,2,3,4]
x.insert(3,100)
print(x)
#[1, 2, 3, 100, 4]

x = [1,2,3,4]
x.remove(3)
print(x)
#[1, 2, 4]

y = x.pop(2)
print(x, y)
#[1, 2] 4

x = [1,2,3,3,4,5,56,6,67,4]
del x[0:5]
print(x)
#[5, 56, 6, 67, 4]

# + 比extend耗费内存
x = [1,2,3,3,4,5,5,6]
y = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
z1 = x+y
print(z1)
x.extend(y)
print(x)
#[1, 2, 3, 3, 4, 5, 5, 6, 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
#[1, 2, 3, 3, 4, 5, 5, 6, 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']

列表的其他方法

  • count()
  • index() 返回索引位置
  • reverse() 反向列表元素
  • sort(key, reverse)
list = [1,2] * 5
list.index(2,3)
#3

#按第二个值排序
x = [(2, 2), (3, 4), (4, 1), (1, 3)]
x.sort(key=lambda x:x[1])
print(x)
#[(4, 1), (2, 2), (1, 3), (3, 4)]

2.元组

#单个元素的元组,添加逗号
x = (1)
print('Type of x: ', type(x))
y = (1,)
print('Type of y: ', type(y))
#Type of x:  <class 'int'>
#Type of y:  <class 'tuple'>

#元组不可更改,但是元组中元素可更改时,可更改
x = (1,2,3,4,5,[1,3,4])
x[5][0] = 9
print(x)
#(1, 2, 3, 4, 5, [9, 3, 4])

元组常用内置方法

  • count()
  • index()
#元组解压
t = 1,2,3,4,5
(a,b,c,d,e) = t
print(a,b,c,d,e)
#1 2 3 4 5

#可以用rest或者通配符'_'匹配不需要的变量
a, b, *rest, c = t
print(*rest)
#3 4

a, b, *_ = t
print(*_)
#3 4 5

3.字符串

常用内置方法

  • capitalize()
  • lower()
  • upper()
  • swapcase()
  • count(str)
  • endswith() startswith()
  • find() rfind()从右边开始查找
  • isnumeric()
  • ljust(width[, fillchar]), rjust(width[, fillchar])
  • lstrip(), rstrip(), strip()
  • partition() rpartition()
  • replace()
  • split()
str = 'abcdEFG'
print(str.capitalize())
print(str.upper())
print(str.lower())
print(str.swapcase()) #大小写互换
#Abcdefg
#ABCDEFG
#abcdefg
#ABCDefg

str.count('d')
#1

print(str.endswith('g'))#区分大小写
print(str.endswith('G'))
print(str.find('D')) #区分大小写,找不到则显示-1
#False
#True
#-1

print(str.ljust(20,'_'))
#abcdEFG_____________

str1 = ' Zen of Python '
print(str1.rstrip())
print(str1.strip())
print(str1.strip().strip('n'))  #只能去除边缘的字符
# Zen of Python
#Zen of Python
#Zen of Pytho

print(str1.partition('o'))
print(str1.rpartition('o'))
#(' Zen ', 'o', 'f Python ')
#(' Zen of Pyth', 'o', 'n ')

str2 = 'I love C, I love Java, I love Python.'
print(str2.replace('love', 'hate', 1))
#I hate C, I love Java, I love Python.

print(str2.split())
print(str2.split(' ', 2))
#['I', 'love', 'C,', 'I', 'love', 'Java,', 'I', 'love', 'Python.']
#['I', 'love', 'C, I love Java, I love Python.']

c = '''say
hello
baby'''
print(c.split('\n')) #去掉换行符
#['say', 'hello', 'baby']

#字符串格式化
print("我叫 %s 今年 %d 岁! 体重 %.2f公斤" % ('小明', 10, 50))
#我叫 小明 今年 10 岁! 体重 50.00公斤

相关文章

  • 2018-06-29

    python学习 学习python字符串、列表、元组、字典、日期和时间模块

  • Python基础学习Day4--元组、列表、字符串

    1.列表 list可修改,附加append,extend,insert,remove,pop append添加一个...

  • Python ☞ day 3

    Python学习笔记之 字符串 & 列表 & 元组 & 字典 字符串 什么是字符串? 字符串运算 字符串方法 列表...

  • Python notes(2/3)

    目录 一,python 字符串 二,Python列表(List) 三,Python 元组 四,Python字典 五...

  • 2018-01-13 python学习第一天

    第二章 列表和元组 列表和元组区别:列表可以修改,二元组则不能 python的6种內建的序列: 列表和元组,字符串...

  • 第三周总结

    列表 元组 字典 字符串 List(列表) 是Python中使用最频繁的...

  • 基础·数据容器·列表&元组

    Python基础教程 类型 数据集合包括:列表,元组,字典 列表=========================...

  • python_20190407

    1. 关于python基础元素可分为: 数字类型num; 字符串str; 列表list;元组tuple; 字典di...

  • Python中的变量分类以及常用操作

    Python的变量分类: 列表、元组、字典、字符串常用操作

  • v1.5.2 python数据结构的嵌套

    python数据结构列表,字典,元组,字符串;再分组就是1.不可改变型:元组,字符串2.可改变型: 列表,字典...

网友评论

      本文标题:Python基础学习Day4--元组、列表、字符串

      本文链接:https://www.haomeiwen.com/subject/boscrktx.html