美文网首页
数据结构 | 元组

数据结构 | 元组

作者: 简子逍 | 来源:发表于2019-07-16 23:49 被阅读0次

基本用法

  1. 创建元组
tup1 = ()    # 创建一个空元组
tup2 = (1,)  # 当元组内只有一个元素时,需要在元素后面添加逗号
tup3 = (1, 2, 3)
  1. 修改元组

元组内的元素一旦创立后是不可改变的,但可以对元组进行连接组合。

# 连接元组
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2
print(tup3)  # (12, 34.56, 'abc', 'xyz')

# 删除元组
del tup3
print(tup3)  # NameError: name 'tup3' is not defined

元组内置方法

  • len(tuple):计算元组元素个数
  • max(tuple):返回元组中元素最大值
  • min(tuple):返回元组中元素最小值
  • tuple(seq):将列表转换为元组
car = ['audi', 'bmw', 'benchi', 'lingzhi']
print(len(car))  # 4

tuple1 = ('5', '4', '8')
print(max(tuple1))  # 8
print(min(tuple1))  # 4

list1 = car
tuple1 = tuple(list1)
print(tuple1)  # ('audi', 'bmw', 'benchi', 'lingzhi')

序列分解

Python 允许任何序列(或可迭代对象)通过简单的赋值操作分解为单独的变量,要求是变量的总数和结构要与序列相吻合

p = (4, 5, 6)
x, y, z = p
print(x)  # 4
print(y)  # 5
print(z)  # 6

x, y = p
print(x, y)  # ValueError: too many values to unpack (expected 2)

星号表达式

适用于分解未知或任意长度的可迭代对象。参数使用了*args,关于其详细用法参见 Python | 动态参数的使用

records = [
    ('A', 1, 2),
    ('B', 'hello'),
    ('C', 5, 3)
]

def do_A(x, y):
    print('A', x, y)

def do_B(s):
    print('B', s)

for tag, *args in records:
    if tag == 'A':
        do_A(*args)
    elif tag == 'B':
        do_B(*args)

# 输出结果
A 1 2
B hello
line = 'www.computer.com/dsffe-3fdcd.d/we/index.html'
domain, *uri, file = line.split('/')
print(domain)  # www.computer.com
print(file)    # index.html

优先级队列

使用内置模块heapq实现一个简单的优先级队列。

import heapq

class PriorityQueue:
    def __init__(self):
        self._queue = []
        self._index = 0

    def push(self, item, priority):
        heapq.heappush(self._queue, (-priority, self._index, item))
        self._index += 1

    def pop(self):
        return heapq.heappop(self._queue)[-1]


class Item:
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return 'Item({!r})'.format(self.name)

q = PriorityQueue()
q.push(Item('A'), 1)
q.push(Item('B'), 4)
q.push(Item('C'), 5)
q.push(Item('D'), 1)
print(q.pop())  # Item('C')
print(q.pop())  # Item('B')
print(q.pop())  # Item('A')

上述代码中,每次执行pop()操作时返回的元素具有最高的优先级,拥有相同优先级的两个元素返回的顺序,同插入到队列时的顺序相同。

函数heapq.heappush()heapq.heappop()分别实现了列表_queue中元素的插入和移除操作,并且保证列表中第一个元素的优先级最低。一般情况下,堆是按从小到大的顺序进行排序的,和优先队列相反。因此在 push()函数中使用了负的priority值。_index的作用是通过维护一个不断递增的索引,将具有相同优先级的元素以加入队列时的顺序排列。

相关文章

  • Python 基础(常用数据结构)

    常用数据结构 1)元组 元组是一种静态的数据结构,无法修改,若要修改只能重新生成新的元组。 输出结果: 元组元素的...

  • 2021-12-1 Python-8

    内建数据结构 Python 常用的数据结构主要有元组、列表、字典和集合。 元组 元组长度固定,是不可变 Pytho...

  • Rust 数组与元组

    元组(tuple) 元组是长度固定并且各项值的类型可以不同的数据结构 解构元组 访问元组 元组的下标从 0 开始,...

  • ch03

    3 Python 的数据结构、函数和文件 3.1 数据结构和序列 元组 元组是一个固定长度,不可改变的Python...

  • Python基础-元组,字典

    Python基础-元组(tuple),字典 元组: 数据结构与列表类似,其中元素可以有不同的类型,但是元组中的元素...

  • 2.python语法基础

    2.1 变量 2.2控制语句 2.3数据结构 2.3.2 元组 ···print('【tips】元组可以理解成c+...

  • 六、元组的操作

    元组也是python中常见的数据结构,与列表类似,不同的是元组中的元素不可修改。 创建一个元组 访问元组的元素 删...

  • iOS - swift 中元组的使用

    元组的基本使用 元组是一种数据结构, 元组中的数据称为元素 一般用于方法的返回值 元组用() 表示, 写法类似数组...

  • 第3章:内建数据结构、函数及文件

    python的常用数据结构:元组、列表、字典和集合 元组(tuple):固定长度、不可变的python序列 列表:...

  • python笔记2_数据结构

    三大数据结构:tuple元组,list数组,dic字典

网友评论

      本文标题:数据结构 | 元组

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