小甲鱼教学笔记——列表:常用操作符、字符串格式化、序列
程序员文章站
2024-01-25 22:10:58
...
1. 列表:常用操作符
list1=[123]
list2=[234]
list1>list2
False
list1=[1,2,3]
list2=[2,3,4]
list1>list2
False
list3=[1,2,3]
(list1<list2)and(list1==list3)
True
list4=list1+list2
list4
[1, 2, 3, 2, 3, 4]
list1*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
list3=[1,2,3]
list3*=3
list3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
1 in list3
True
'yuhongxia' not in list3
True
list5=[123,['余红霞','最漂亮'],456]
'余红霞'in list5
False
'余红霞'in list5[1]
True
dir(list)#列表的所有方法
['__add__',
'__class__',
'__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']
list3.count(1)
3
list3.index(1)
0
list3.index(1,2,5)
3
list3.reverse()
list3
[3, 2, 1, 3, 2, 1, 3, 2, 1]
list6=[4,5,6,2,1,8]
list6.sort()
list6
[1, 2, 4, 5, 6, 8]
list6.sort(reverse=True)
list6
[8, 6, 5, 4, 2, 1]
2. 字符串格式化
- format():位置参数、关键字参数
- {}表示replacement字段
"{0} love {1}.{2}".format("i","china","com")
'i love china.com'
"{a} love {b}.{c}".format(a="I",b="china",c="com")
'I love china.com'
"{0} love {b}.{c}".format("I",b="china",c="com")
'I love china.com'
"{{0}}".format("不打印,因为0已经被解释掉了")
'{0}'
"{0:.1f}.{1}".format(27.658,"GB")#:.1f表示保留一位小数
'27.7.GB'
%c:格式化字符及ASCII码
"%c" % 97
'a'
'%c %c %c' % (97,98,99)
'a b c'
%s:格式化字符串
"%s" % "i love china"
'i love china'
%d:格式化整数
'%d + %d = %d' % (4,5,4+5)
'4 + 5 = 9'
'%o' % 10
'12'
'%x' % 10
'a'
'%X' % 10
'A'
'%f' % 27.658
'27.658000'
'%.2f' % 27.658999
'27.66'
'%e' % 27.658999
'2.765900e+01'
'%g' % 27.658999
'27.659'
格式化操作辅助指令:
'%6.1f' % 27.658999
' 27.7'
'%.2e' % 27.658999
'2.77e+01'
'%10d'% 10
' 10'
'%-10d'% 10
'10 '
'%+d'% 10
'+10'
'%+d'% -10
'-10'
'%#o' % 10
'0o12'
'%#x' % 10
'0xa'
'%010d'% 10
'0000000010'
字符串转义字符含义:
3. 序列:列表、元组和字符串
列表、元组和字符串的共同特点:
- 都可以通过索引得到每一个元素
- 默认索引值总是从0开始
- 可以通过分片的方法得到一个范围
- 有很多共同的操作符(重复操作符、拼接操作符、成员关系操作符)
list():把一个可迭代对象转换为列表
a=list()
a
[]
b="I love china"
b=list(b)
b
['I', ' ', 'l', 'o', 'v', 'e', ' ', 'c', 'h', 'i', 'n', 'a']
c=(1,1,2,3,5,8,13,21,34)
c=list(c)
c
[1, 1, 2, 3, 5, 8, 13, 21, 34]
tuple():把一个可迭代对象转换为元组
help(tuple)
Help on class tuple in module builtins:
class tuple(object)
| tuple(iterable=(), /)
|
| Built-in immutable sequence.
|
| If no argument is given, the constructor returns an empty tuple.
| If iterable is specified the tuple is initialized from iterable's items.
|
| If the argument is a tuple, the return value is the same object.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __getnewargs__(self, /)
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __rmul__(self, value, /)
| Return value*self.
|
| count(self, value, /)
| Return number of occurrences of value.
|
| index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value.
|
| Raises ValueError if the value is not present.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
str(obj):把obj对象转换为字符串
str(123)
'123'
其他
sorted(c)
[1, 1, 2, 3, 5, 8, 13, 21, 34]
reversed(c)
<list_reverseiterator at 0x13a5b945788>
list(reversed(c))
[34, 21, 13, 8, 5, 3, 2, 1, 1]
enumerate(c)
<enumerate at 0x13a5c86fae8>
list(enumerate(c))
[(0, 1), (1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), (7, 21), (8, 34)]
a=[1,2,3,4,5,6,7,8]
b=[3,4,5,6,7,8]
list(zip(a,b))
[(1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8)]