Python的逻辑操作

  1. “==", “>” , "<” , “in” , “not in” ,“+” , “ * ”
>>> list1 = [123]
>>> list2=[234]
>>> list1==list2
False
>>> list1<list2
True
>>> list1*2
[123, 123]
>>> list1
[123]
>>> 12 in list1
False
>>> 123 in list1
True
>>>
  1. 列表中的列表成员
>>> list1=[1,['abc'],2]
>>> list1
[1, ['abc'], 2]
>>> 'abc' in list1[1]
True
>>>

类似二维数组的访问方法

>>> list1=[1, ['abc','cde'], 2]
>>>
>>> list1
[1, ['abc', 'cde'], 2]
>>> list1[1][1]
'cde'
  1. 列表的所有支持的方法

[‘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’]

例如:

>>> list1=[1,2,3,4,5,6]
>>> list1
[1, 2, 3, 4, 5, 6]
>>> list1.count(1)
1
>>> list1.index(5)
4
>>> list1.reverse()
>>>
>>> list1
[6, 5, 4, 3, 2, 1]

排序

>>> list1=[1,4,2,5,3]
>>> list1.sort()
>>> list1
[1, 2, 3, 4, 5]
>>> list1.sort(reverse=True)
>>> list1
[5, 4, 3, 2, 1]

猜你喜欢

转载自blog.csdn.net/zhuyong006/article/details/83904412