【Python3 NoteBook】001 Arrays & Hashing

  1. set():Python内置函数,用于创建一个空的集合(set)或将可迭代对象(如列表、元组、字符串等)转换为集合。集合是一种无序的、包含唯一元素的数据结构。
    (1)用add()向集合添加元素。
    (2)常用于创建哈希表。
    s = set()
    print(s)
    # output:set()
    
    s.add(1)
    print(s)
    # output:{1}
    
    s.add(2)
    print(s)
    # output:{1, 2}
    
    s.add(1)
    print(s)
    # output:{1, 2}
    
    l = [0, 1, 2, 3, 3]
    sl = set(l)
    print(sl)
    # output:{0, 1, 2, 3}
  2. Counter():collections模块中的一个类,用于统计可迭代对象中各元素的出现次数,并将结果存储在一个字典中。这个字典以元素作为键,以出现次数作为值。
    from collections import Counter
    
    str1 = 'apple'
    str2 = 'banana'
    
    list1 = list(str1)
    list2 = list(str2)
    
    print(list1)
    print(list2)
    
    print(Counter(list1))
    print(Counter(list2))
    
    # output:
    # ['a', 'p', 'p', 'l', 'e']
    # ['b', 'a', 'n', 'a', 'n', 'a']
    # Counter({'p': 2, 'a': 1, 'l': 1, 'e': 1})
    # Counter({'a': 3, 'n': 2, 'b': 1})
  3. sorted():Python内置函数,用于对可迭代对象进行排序并返回一个新的已排序的列表。该函数可以按升序(默认)或降序对元素进行排序,并且适用于各种可迭代对象,如列表、元组、字符串等。
    str1 = 'apple'
    str2 = 'banana'
    
    list1 = list(str1)
    list2 = list(str2)
    
    print(list1)
    print(list2)
    
    print(sorted(list1))
    print(sorted(list2))
    
    print(sorted(list1,reverse=True))
    print(sorted(list2,reverse=True))
    
    # output:
    # ['a', 'p', 'p', 'l', 'e']
    # ['b', 'a', 'n', 'a', 'n', 'a']
    # ['a', 'e', 'l', 'p', 'p']
    # ['a', 'a', 'a', 'b', 'n', 'n']
    # ['p', 'p', 'l', 'e', 'a']
    # ['n', 'n', 'b', 'a', 'a', 'a']
  4. get():字典(dictionary)对象的方法之一,用于获取字典中指定键(key)的值。该方法可以安全地访问字典的键值对,即使键不存在也不会引发错误。
    # dictionary.get(key, default)
    
    my_dict = {'apple': 3, 'banana': 5, 'cherry': 2}
    
    # 获取存在的键的值
    apple_count = my_dict.get('apple')
    print(apple_count)  
    # output: 3
    
    # 获取不存在的键时,返回默认值
    orange_count = my_dict.get('orange', 0)
    print(orange_count)  
    # output: 0
    
    # 不提供默认值,返回 None
    grape_count = my_dict.get('grape')
    print(grape_count)  
    # output: None
  5. enumerate(): Python内置函数,用于在迭代中同时获取元素的索引(index)和元素的值(value)。这个函数返回一个枚举对象,其中包含了迭代对象的元素以及它们对应的索引。通常在for循环中使用。字典的items()方法类似
    my_list = {'apple', 'banana', 'cherry'}
    
    for index, value in enumerate(my_list):
        print(index, value)
    
    # output:
    # 0 apple
    # 1 banana
    # 2 cherry
    
    my_dict = {'apple': 3, 'banana': 5, 'cherry': 2}
    
    for key, value in my_dict.items():
        print(key, value)
    
    # output:
    # apple 3
    # banana 5
    # cherry 2
  6. defaultdict():collections模块中的一个类,它是字典的一个变种,用于创建具有默认值的字典。与普通的字典不同,defaultdict允许在访问不存在的键时,自动创建该键并设置一个默认值,而不会引发KeyError异常。

    from collections import defaultdict
    
    # 创建一个 defaultdict,指定默认值为 int 类型的 0
    my_dict = defaultdict(int)
    
    # 访问不存在的键 'key1',将其创建并设置默认值为 0
    my_dict['key1'] += 1
    
    # 访问不存在的键 'key2',将其创建并设置默认值为 0
    my_dict['key2'] += 1
    
    # 访问已存在的键 'key1',增加其值
    my_dict['key1'] += 1
    
    print(my_dict)  
    # output: defaultdict(<class 'int'>, {'key1': 2, 'key2': 1})
  7. ord():Python内置函数,用于返回给定字符的Unicode码点(Unicode code point)。Unicode是一种国际字符编码标准,它为世界上几乎所有的字符分配了唯一的数字标识。

    unicode_value = ord('A')
    print(unicode_value)  
    # ouput: 65
    
    unicode_value = ord('中')
    print(unicode_value)  
    # ouput: 20013
    
    unicode_value = ord('1')
    print(unicode_value)  
    # ouput: 49
    
  8. l = [ [] for i in range(5) ]:创建一个包含5个空列表的列表。

    my_list1 = [ [] for i in range(5) ]
    print('list 1:', my_list1)
    # ouput:
    # list 1: [[], [], [], [], []]
    
    my_list2 = [ 0 for i in range(5) ]
    print('list 2:', my_list2)
    # ouput:
    # list 2: [0, 0, 0, 0, 0]
  9. for i in range(5, 0, -1):反向迭代循环,从5开始递减到1。

    (1)起始值:5,循环的起始值是 5。
    (2)结束值:0,循环在到达或超过结束值时停止。
    (3)步长:-1,循环在每次迭代时递减 1。
    for i in range(5, 0, -1):
        print(i)
    # output:
    # 5
    # 4
    # 3
    # 2
    # 1
  10. //:运算符,通常用于执行整数除法操作,它返回除法结果的整数部分(去掉小数部分)。

    print('4/3 =',4/3)
    print('4//3 =',4//3)
    # output:
    # 4/3 = 1.3333333333333333
    # 4//3 = 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值