Python 基础数据类型一览:
列表 list
1. 初始化列表:
(1)指定元素初始化列表:
num = ['aa','bb','cc',1,2,3]
print(num)
# 输出结果:
#
# ['aa', 'bb', 'cc', 1, 2, 3]
(2)从字符串初始化列表:
a = 'oiawo1048f'
num = list(a)
print(num)
# 输出结果:
#
# ['o', 'i', 'a', 'w', 'o', '1', '0', '4', '8', 'f']
(3)从元组初始化列表:
a = (1,2,3,4,5,6,7,8)
num = list(a)
print(num)
# 输出结果:
#
# [1, 2, 3, 4, 5, 6, 7, 8]
(4)创建空列表:
num = []
print(num)
# 输出结果:
#
# []
(5)用某个固定值初始化列表:
initial_value = 0
list_length = 5
sample_list1 = [initial_value]*list_length
print(sample_list1)
sample_list2 = [initial_value for i in range(10)]
print(sample_list2)
# 输出结果:
#
# [0, 0, 0, 0, 0]
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
2. 访问列表:
(1)访问单个元素:
num=[0,1,2,3,4,5,6,7]
print(num[3])
print(num[0])
print(num[-1])
print(num[-3])
# 输出结果:
#
# 3
# 0
# 7
# 5
(2)遍历整个列表:
num=[0,1,2,3]
for a in num:
print(a)
for i in range(len(num)):
print(num[i])
# 输出结果:
#
# 0
# 1
# 2
# 3
# 0
# 1
# 2
# 3
3. 列表操作:
(1)加号操作符示例:
a=['a','b']
b=['d','e']
c=a+b
print(c)
d=a*3
print(d)
# 输出结果:
#
# ['a', 'b', 'd', 'e']
(2)星号 (乘号) 操作符示例:
a=['a','b']
b=['d','e']
d=a*3
print(d)
# 输出结果:
#
# ['a', 'b', 'a', 'b', 'a', 'b']
4. 列表函数:
以下是 help(list) 的结果中关于重点函数的介绍部分:
Help on list object:
class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
|
| Methods defined here:
|
| append(...)
| L.append(object) -> None -- append object to end
|
| clear(...)
| L.clear() -> None -- remove all items from L
|
| copy(...)
| L.copy() -> list -- a shallow copy of L
|
| count(...)
| L.count(value) -> integer -- return number of occurrences of value
|
| extend(...)
| L.extend(iterable) -> None -- extend list by appending elements from the iterable
|
| index(...)
| L.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
|
| insert(...)
| L.insert(index, object) -- insert object before index
|
| pop(...)
| L.pop([index]) -> item -- remove and return item at index (default last).
| Raises IndexError if list is empty or index is out of range.
|
| remove(...)
| L.remove(value) -> None -- remove first occurrence of value.
| Raises ValueError if the value is not present.
|
| reverse(...)
| L.reverse() -- reverse *IN PLACE*
|
| sort(...)
| L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
元组 tuple
1. 元组初始化:
t = (1, 2, 3)
print(t)
t = tuple(range(3))
print(t)
# 输出结果:
#
# (1, 2, 3)
# (0, 1, 2)
2. 元组函数:
help(tuple)
Help on class tuple in module builtins:
class tuple(object)
| tuple() -> empty tuple
| tuple(iterable) -> tuple initialized from iterable's items
|
| If the argument is a tuple, the return value is the same object.
|
| Methods defined here:
|
| count(...)
| T.count(value) -> integer -- return number of occurrences of value
|
| index(...)
| T.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
3.命名元组:
Python 有一个类似 tuple 的容器 —— namedtuples (命名元组),位于 collection 模块中。
namedtuples 是继承自 tuple 的子类,可创建一个和 tuple 类似的对象,而且对象拥有可访问性。命名元组的具体使用如下:
from collections import namedtuple #依赖collections包的namedtuple模块
Point = namedtuple('Point', 'x,y')
p1 = Point(11, y=22)
print(p1)
print(type(p1))
print(p1.x)
print(p1.y)
print(p1[0] + p1[1])
a, b = p1
print(a, b)
# 输出结果:
#
# Point(x=11, y=22)
# <class '__main__.Point'>
# 11
# 22
# 33
# 11 22