Python学习笔记——CSDN学习记录十五:内置类——列表与元组

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HaJucy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值