Python - DataType

文章介绍了Python中的数据结构,包括字典(如默认字典和有序字典的操作)、集合的功能以及在LeetCode问题128.LongestConsecutiveSequence中的应用。还讨论了列表、双端队列和字符串的操作,并提到了字符串匹配问题的解题策略,如滑动窗口和哈希算法。
摘要由CSDN通过智能技术生成


Dict

Functions

  • len(d) 测长度
  • del d[key]: remove key and associated value from dictionary d
  • key in d: return true if key exists in dictionary
  • d1.update(d2)

Traversal of a dict

  • dict.items() -> list of (key, value) paris
  • dict -> list of keys
  • dict.values() -> list of values

Defaultdict

  1. How does collections.defaultdict work?
    The defaultdict will create a default item (according to your definition) when you try to get an item with a key that is not currently in the dictionary.
    intDict = defaultdict(int) # default item = 0
    listDict = defaultdict(list) # default item = []
    
  2. self-defined class + defaultdict
    class GNode(object):
        def __init__(self):
            self.inDegree = 0
            self.outNode = []
    
  • Use self.inDegree rather than inDegree.
    import collections from defaultdict
    graph = defaultdict(GNode)
    
  • Pay attention to the spelling of defaultdict

Ordereddict


Set

Functions

  • s.add(elem) 添加一项
  • s.update(l/s2)
    OR
    s1 |= l/s2 添加多项 ???
  • len(s) set的长度
  • x in s
    OR
    x not in s 测试x是否是/不是s的成员
  • s1.issubset(s2)
    OR
    s1 <= s2 测试是否s1中的每一个元素都在s2中
  • s1.issuperset(s2)
    OR
    s1 >= s2测试是否s2中的每一个元素都在s1
  • s1.union(s2)
    OR
    s1 | s2 求并集
  • s1.intersection(s2)
    OR
    s1 & s2 求交集
  • s1.difference(s2)
    OR
    s1 - s2 返回一个新的set包含s1中有但是s2中没有的元素
  • s1.symmetric_difference(s2)
    OR
    s1 ^ s2 返回一个新的set包含s1和s2中不重复的元素
  • s.remove(elem) 从s中删除元素,如果不存在则引发KeyError
  • s.discard(elem) 如果在s中存在元素elem,则删除
  • s.clear() 删除s中的所有元素

Leetcode example

128. Longest Consecutive Sequence

Why we use set
we want to inquire an element in O(1) and do not need key

Main idea
We need run in O(n), which means we only run once for each consecutive elements sequence.
How to guarantee we only run once for each sequence? The solution is we only traverse the sequence from its end points (smallest or biggest value).
Therefore, only when num-1 not in the set, i.g., num is the smallest value of a sequence, we traverse the sequence and find the maximum value.

Code

class Solution(object):
    def longestConsecutive(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """

        nums = set(nums)

        longestNum = 0
        for num in nums:
            if num-1 not in nums:
                currElement = num
                currNum = 1
                while currElement + 1 in nums:
                    currNum += 1
                    currElement += 1
                if currNum > longestNum:
                    longestNum  = currNum
        
        return longestNum

List

Functions

i. Acquire information

  • len(l)
  • index = l.index(num): return the index position of num in list l

ii. 添加元素

  • l.append(elem) 在队尾添加元素
  • l.insert(pos,elem): insert element elem at the ith position in list l

iii. 删除元素

  • l.pop(pos) 可作过程,可作函数
    当pos为空时,取出并删除队尾元素
    当pos不为空时,取出并删除指定位置元素
  • l.remove(elem) 删除指定元素(若有多个,只删除一个)

iv. 倒转

  • l.reverse()
    例:x=[1,2,3,4,5]
    x.reverse() -> [5,4,3,2,1]

v. 排序

vi. 最大与最小

  • min(l) / l.min() -> return the minimum number
  • max(l) / l.max() -> return the maximum number

vii. 合并

  • s.join(l): use str to join the list

viii. 计数

  • l.count(target) -> 输出l中target的个数

Note

  1. Define 2D array

    Correct

    a = [[0]*n for i in range(m)]
    

    Incorrect

    a = [[0]*n]*m ####WRONG!!!!!!!
    #The adress of m rows are the same
    

Deque

from collections import deque
nodeOrder = deque()
nodeOrder.append(node1) # append a new node to tail
nodeOrder.appendleft(node2) # append a new node to head
nodeOrder.pop() # pop from tail
nodeOrder.popleft() # pop from head
## Topological ordering

Leetcode example: 210. Course Schedule II


String

Functions

  • len(str) 测量string长度
  • str = str.split(str2) 将string中以str2相隔的部分分离成列表
    特例:str.split() 将string中以空格相隔的部分分离成列表 student.split() ->
    [‘your_first_name’,’your_last_name’,’your_student_ID’]
  • str = str.lower() 将string中的字母全部变为小写
  • str = str.upper() 将string中的字母全部变为大写
  • str = str.capitalize() 若string的第一个字符为字母,则将其变为大写
  • str = str.replace(str1, str2) 将str中的子串str1替换为str2

Substring

  1. s[-1:] -> the last character of s
  2. s[:] -> copy the string s and generate a string with new address

String Matching Problem

Leetcode example: 28. Find the Index of the First Occurrence in a String

Approach 1: Sliding Window

Iterate every position of the original string, and take each position as a beginning.

Approach 2: Rabin-Karp Algorithm (Single Hash)

Main idea: use hash value to detect part of unequal strings in O(1)
“If two hash values are NOT equal, then strings will NOT be equal”

Algorithm

  1. compare hash value of the substring and the target
  2. If two hash values are NOT equal, skip
  3. If two hash values are equal, compare two strings

Key point: how can we reduce the chance of spurious hits?

  1. large RADIX (larger than the number of unique characters):
    pos_1 * RADIX + pos_2 * RADIX ^ 2 + … + pos_n * RADIX ^ n
  2. use MOD to reduce overflow, e.g.,
    MOD = 10**9+33
    MOD = 2**31-1
    
  3. update the hash value in O(1)
    请添加图片描述

Approach 3: Double Hash

Main idea: use two hash methods to essentially reduce the chance of spurious hits.

Use two different mods:

  • MOD_1 = 10**9 + 33
  • MOD_2 = 2**31 - 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值