基本数据结构

Arrays

最基本的数据结构就是数组。计算机的内存可以看成是一个内存槽(memory slots)的长链,数组在内存中的存储方式十分简单和直观。数组的第二个元素存储在第一个元素的旁边,第三个存储在第二个的旁边,以此类推。我们可以很确定的提取出某个槽的元素。这种结构的存储易于访问,但是当进行删除和添加操作时,会很费时,并且数组通常不会超出他们的初始大小。

Dynamic Arrays

许多语言都有原始的內建数组结构,一个原始的数据结构不用我们手动去编写数据结构的一些方法和操作。但是Python没有內建的固定大小的数组结构。但是Python的list数据结构实际上就是一个动态的数组结构。我们可以利用lists进行熟悉的数组操作。

# Retrieving an item in an array by index
retrieval_by_index = "constant"

# Searching for a value in an unordered array
search = "linear"

# Deleting an item from an array, and filling the gap
#     by shifting all later items back by one
deletion = "linear"

# Inserting an item into the array, and shifting forward
#     every item that comes after it
insertion = "linear"

Array Insertion Practice

players = ["Reggie Jackson"]
print(players)
players.insert(1, "C.J. Watson")
print(players)
players.insert(0, "Jeff Adrien")
print(players)
players.remove("Reggie Jackson")
print(players)
'''
['Reggie Jackson']
['Reggie Jackson', 'C.J. Watson']
['Jeff Adrien', 'Reggie Jackson', 'C.J. Watson']
['Jeff Adrien', 'C.J. Watson']
'''

2-Dimensional Arrays

  • 一维数组可以被称为向量,二维数组可以被称为矩阵。
'''
checker_board : list (<class 'list'>)
[['blank', 'blank', 'blank', 'blank', 'blank', 'black', 'blank', 'blank'],
 ['blank', 'blank', 'red', 'blank', 'blank', 'blank', 'blank', 'blank'],
 ['blank', 'blank', 'blank', 'black', 'blank', 'red', 'blank', 'blank'],
 ['red', 'blank', 'black', 'blank', 'blank', 'blank', 'red', 'blank'],
 ['blank', 'blank', 'blank', 'black', 'blank', 'red', 'blank', 'blank'],
 ['blank', 'blank', 'black', 'blank', 'blank', 'blank', 'blank', 'blank'],
 ['blank', 'blank', 'blank', 'black', 'blank', 'blank', 'blank', 'blank'],
 ['blank', 'blank', 'red', 'blank', 'red', 'blank', 'red', 'blank']]
'''
red_pieces = 0
black_pieces = 0

# Find how many red and black pieces there are
for row in checker_board:
    for piece in row:
        if piece == "red":
            red_pieces += 1
        elif piece == "black":
            black_pieces += 1

Hash Tables/Dictionaries

  • dictionary是哈希表的一个最常见的例子,通过键值对的形式存储数据。
# Population of Rio de Janeiro
rio_population = city_populations["Rio de Janeiro"]
boston_population = city_populations["Boston"]
paris_population = city_populations["Paris"]
city_populations["Beijing"] += 1
city_populations["Boston"] -= 1
'''
city_populations : dict (<class 'dict'>)
{'Beijing': 11510001,
 'Boston': 645965,
 'Paris': 2244000,
 'Rio de Janeiro': 6320000}
'''

Choosing A Data Structure

  • 提供几个常见分析哪种存储结构比较合适:
# Scenario A: You need to keep track of people sitting in an auditorium for a play. You will have to identify which seats are empty, and sell tickets until the auditorium is completely full. How will you store the names of who is sitting where?
scenario_A_data_structure = "2d array"

# Scenario B: You are in charge of maintaining a guest list for a wedding. You are only concerned with a list of who is coming to the party. You have to add someone's name whenever they RSVP that they will be attending the wedding.
scenario_B_data_structure = "dynamic array"

# Scenario C: Now every person who RSVP's for the wedding indicates which meal they will be eating. You have to keep track of the person, and the meal order. You need to be able to quickly find out what meal a particular person ordered, so that the waiters don't delay too long when it comes time to eat.
scenario_C_data_structure = "hash table"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值