zoukankan      html  css  js  c++  java
  • Python---基础----数据类型的内置函数(主要介绍字符串、列表、元组、字典、集合的内置函数)(二)

    2019-05-24

    --------------------------------

    一、

    # splitlines()    以换行切割字符串
    s = '''日照香炉生紫烟 疑是银河落九天 飞流直下三千尺'''
    print(s.splitlines())

    二、

    # join()   将列表按照指定字符串连接
    list1 = ['日照香炉生紫烟','疑是银河落九天','飞流直下三千尺']
    s = '*'.join(list1)
    print(s)

    三、

    # ljust() 指定字符串的长度,内容靠左边
    s = 'abc'
    print(len(s))
    print(s.ljust(5, '#'))

    四、

    # ljust() 指定字符串的长度,内容靠左边,不足的地方用空格填充,默认空格,返回字符串
    s = 'abc'
    print(len(s))
    print(s.ljust(5)+'aa')
    # center()  指定字符串长度,内容居中,不足的地方用空格填充,默认空格,返回字符串
    print(s.center(5, '#'))
    # rjust() 指定字符串的长度,内容靠右边,不足的地方用空格填充,默认空格,返回字符串
    print(s.rjust(5, '#'))
    五、
     
    # strip() 去掉左右两边指定字符,默认是去掉空格
    # lstrip() 去掉左侧指定字符,默认空格
    # rstrip() 去掉右侧指定字符,默认空格
    s = '      abc     '
    print('- - -'+s.strip()+'- - -')
    print('- - -'+s+'- - -')
    s = 'aaabcc'
    print(s.lstrip('a'))
    print(s.lstrip('b'))
    print(s.rstrip('b'))
    print(s.rstrip('c'))
    ---------------------------------
    六、
    # zfill() 指定字符串长度
    s = 'abc'
    print(s.zfill(5))
    ---------------------------------
    七、
    # maketrans()  生成用于字符串的映射表
    # translate()  进行字符串替换
    s = '今天晚上我吃的是小炒肉,可好吃了'
    table = s.maketrans('小炒肉', '大白菜')
    print(table)
    print(s.translate(table))
    --------------------------------
    八、
    help(list)
    ----------------------
    Help on class list in module builtins:

    class list(object)
    |  list(iterable=(), /)

    |  Built-in mutable sequence.

    |  If no argument is given, the constructor creates a new empty list.
    |  The argument must be an iterable if specified.

    |  Methods defined here:

    |  __add__(self, value, /)
    |      Return self+value.

    |  __contains__(self, key, /)
    |      Return key in self.

    |  __delitem__(self, key, /)
    |      Delete self[key].

    |  __eq__(self, value, /)
    |      Return self==value.

    |  __ge__(self, value, /)
    |      Return self>=value.

    |  __getattribute__(self, name, /)
    |      Return getattr(self, name).

    |  __getitem__(...)
    |      x.__getitem__(y) <==> x[y]

    |  __gt__(self, value, /)
    |      Return self>value.

    |  __iadd__(self, value, /)
    |      Implement self+=value.

    |  __imul__(self, value, /)
    |      Implement self*=value.

    |  __init__(self, /, *args, **kwargs)
    |      Initialize self.  See help(type(self)) for accurate signature.

    |  __iter__(self, /)
    |      Implement iter(self).

    |  __le__(self, value, /)
    |      Return self<=value.

    |  __len__(self, /)
    |      Return len(self).

    |  __lt__(self, value, /)
    |      Return self<value.

    |  __mul__(self, value, /)
    |      Return self*value.

    |  __ne__(self, value, /)
    |      Return self!=value.

    |  __repr__(self, /)
    |      Return repr(self).

    |  __reversed__(self, /)
    |      Return a reverse iterator over the list.

    |  __rmul__(self, value, /)
    |      Return value*self.

    |  __setitem__(self, key, value, /)
    |      Set self[key] to value.

    |  __sizeof__(self, /)
    |      Return the size of the list in memory, in bytes.

    |  append(self, object, /)
    |      Append object to the end of the list.

    |  clear(self, /)
    |      Remove all items from list.

    |  copy(self, /)
    |      Return a shallow copy of the list.

    |  count(self, value, /)
    |      Return number of occurrences of value.

    |  extend(self, iterable, /)
    |      Extend list by appending elements from the iterable.

    |  index(self, value, start=0, stop=9223372036854775807, /)
    |      Return first index of value.
    |     
    |      Raises ValueError if the value is not present.

    |  insert(self, index, object, /)
    |      Insert object before index.

    |  pop(self, index=-1, /)
    |      Remove and return item at index (default last).
    |     
    |      Raises IndexError if list is empty or index is out of range.

    |  remove(self, value, /)
    |      Remove first occurrence of value.
    |     
    |      Raises ValueError if the value is not present.

    |  reverse(self, /)
    |      Reverse *IN PLACE*.

    |  sort(self, /, *, key=None, reverse=False)
    |      Stable sort *IN PLACE*.

    |  ----------------------------------------------------------------------
    |  Static methods defined here:

    |  __new__(*args, **kwargs) from builtins.type
    |      Create and return a new object.  See help(type) for accurate signature.

    |  ----------------------------------------------------------------------
    |  Data and other attributes defined here:

    |  __hash__ = None
    -----------------------------
    九、
    #  append    向列表末尾添加新元素   返回值None
    list1 = [1,2,3,4,5]
    print(list1.append(5))
    ----------------------------
    十、
    # count() 计算某个元素在列表中出现的次数
    list1 = [1,1,2,5,1,3]
    print(list1.count(5))
    ----------------------------
    十一、
    # extend() 将一个列表继承另一个列表
    list1 = [1,2,3,4,5]
    list2 = [6,7,8,9,10]
    list3 = list1.extend(list2)
    print(list1)
    print(list2)
    print(list3)
    print(list1 + list2)
    ------------------------------
    十二、
    # index() 获取值在列表中的索引
    list1 = [1,2,3,4,5,3]
    print(list1.index(3))
    #print(list1.index)
    print(list1.index(3, 2, 5))
    -----------------------------
    十三、
    # insert() 在指定位置前插入元素    2个参数
    list1 = [1,2,3,4,5]
    list1.insert(2,9)
    print(list1)
    -------------------------------
    十四、
    # pop() 根据索引移除列表内一个元素,不给索引默认移除最后一个
    list1 = [1,2,3,4,5]
    list1.pop()
    print(list1)
    -------------------------------
    十五、
    # remove() 移除列表中指定的值     返回None
    list1 = ['a', 'b', 'c', 'd', 'e']
    list1.remove('b')
    print(list1)
    --------------------------------
    十六、
    # sort() 排序   默认从小到大
    list1 = [5,2,4,6,1,9]
    list1.sort()
    print(list1)
    -------------------
    十七、
    # reverse() 列表反转
    list1 = [1,2,3,4]
    list1.reverse()
    print(list1)
    ----------------------
    十八、
    # sort() 排序   默认从小到大
    list1 = [5,2,4,6,1,9]
    list1.sort()
    print(list1)
    #从大到小
    list1.sort(reverse=True)
    ------------------------
    十九、
    # count() 计算某个元素在元组中出现的次数
    tuple1 = (3, 2, 4, 1, 3, 6)
    print(tuple1.count(3))
    ------------------------
    二十、
    # index() 获取值在元组中的索引
    tuple1 = (3, 2, 4, 1, 3, 6)
    print(tuple1.index(3))
    -------------------------
    二十一、
    # index() 获取值在元组中的索引
    tuple1 = (3, 2, 4, 1, 3, 6)
    print(tuple1.index(3))
    print(tuple1.index(3,1,5))
    ------------------------
    二十二、
    # copy() 复制字典
    dict1 = {'a':1, 'b':2, 'c':3}
    dict2 = dict1.copy()
    print(dict2)
    -------------------------
    二十三、
    # fromkeys() 按照指定的序列为键创建字典,值都是一样的
    list1 = ['a', 'b', 'c']
    dict1 = {}.fromkeys(list1)
    dict2 = {}.fromkeys(list1, 3)
    print(dict1,dict2)
    --------------------------
    二十四、
    # get() 根据键获取指定的值
    dict1 = {'a':1, 'b':2, 'c':3}
    print(dict1.get('b'))
    --------------------------
    二十五、
    # get() 根据键获取指定的值    找不到的键如何没默认值则返回默认值,如果没设默认值,则返回None
    dict1 = {'a':1, 'b':2, 'c':3}
    print(dict1.get('b'))
    print(dict1.get('d'))
    print(dict1.get('d', 4))
    --------------------------------
    二十六、
    # items()  将字典变成类似于元组的形式方便遍历
    dict1 = {'a':1, 'b':2, 'c':3}
    for k,v in dict1.items():
        print(k,v)
    print(dict1.items())
    -------------------------------
    二十七、
    # popitem()   移除字典的键值对   返回移除的键和值
    dict1 = {'d':4, 'a':1, 'b':2, 'c':3}
    print(dict1.popitem())
    ---------------------------------
    二十八、
    # setdefault() 在字典里添加一个元素
    dict1 = {'d':4, 'a':1, 'b':2, 'c':3}
    print(dict1.setdefault('e',5))
    print(dict1)
    -------------------------------
    二十九、
    # update() 修改字典中的值
    dict1 = {'d':4, 'a':1, 'b':2, 'c':3}
    dict1.update({'a':3})
    print(dict1)
    ----------------------------------
    三十、
    help(set)
    ---------------------
    Help on class set in module builtins:

    class set(object)
    |  set() -> new empty set object
    |  set(iterable) -> new set object

    |  Build an unordered collection of unique elements.

    |  Methods defined here:

    |  __and__(self, value, /)
    |      Return self&value.

    |  __contains__(...)
    |      x.__contains__(y) <==> y in x.

    |  __eq__(self, value, /)
    |      Return self==value.

    |  __ge__(self, value, /)
    |      Return self>=value.

    |  __getattribute__(self, name, /)
    |      Return getattr(self, name).

    |  __gt__(self, value, /)
    |      Return self>value.

    |  __iand__(self, value, /)
    |      Return self&=value.

    |  __init__(self, /, *args, **kwargs)
    |      Initialize self.  See help(type(self)) for accurate signature.

    |  __ior__(self, value, /)
    |      Return self|=value.

    |  __isub__(self, value, /)
    |      Return self-=value.

    |  __iter__(self, /)
    |      Implement iter(self).

    |  __ixor__(self, value, /)
    |      Return self^=value.

    |  __le__(self, value, /)
    |      Return self<=value.

    |  __len__(self, /)
    |      Return len(self).

    |  __lt__(self, value, /)
    |      Return self<value.

    |  __ne__(self, value, /)
    |      Return self!=value.

    |  __or__(self, value, /)
    |      Return self|value.

    |  __rand__(self, value, /)
    |      Return value&self.

    |  __reduce__(...)
    |      Return state information for pickling.

    |  __repr__(self, /)
    |      Return repr(self).

    |  __ror__(self, value, /)
    |      Return value|self.

    |  __rsub__(self, value, /)
    |      Return value-self.

    |  __rxor__(self, value, /)
    |      Return value^self.

    |  __sizeof__(...)
    |      S.__sizeof__() -> size of S in memory, in bytes

    |  __sub__(self, value, /)
    |      Return self-value.

    |  __xor__(self, value, /)
    |      Return self^value.

    |  add(...)
    |      Add an element to a set.
    |     
    |      This has no effect if the element is already present.

    |  clear(...)
    |      Remove all elements from this set.

    |  copy(...)
    |      Return a shallow copy of a set.

    |  difference(...)
    |      Return the difference of two or more sets as a new set.
    |     
    |      (i.e. all elements that are in this set but not the others.)

    |  difference_update(...)
    |      Remove all elements of another set from this set.

    |  discard(...)
    |      Remove an element from a set if it is a member.
    |     
    |      If the element is not a member, do nothing.

    |  intersection(...)
    |      Return the intersection of two sets as a new set.
    |     
    |      (i.e. all elements that are in both sets.)

    |  intersection_update(...)
    |      Update a set with the intersection of itself and another.

    |  isdisjoint(...)
    |      Return True if two sets have a null intersection.

    |  issubset(...)
    |      Report whether another set contains this set.

    |  issuperset(...)
    |      Report whether this set contains another set.

    |  pop(...)
    |      Remove and return an arbitrary set element.
    |      Raises KeyError if the set is empty.

    |  remove(...)
    |      Remove an element from a set; it must be a member.
    |     
    |      If the element is not a member, raise a KeyError.

    |  symmetric_difference(...)
    |      Return the symmetric difference of two sets as a new set.
    |     
    |      (i.e. all elements that are in exactly one of the sets.)

    |  symmetric_difference_update(...)
    |      Update a set with the symmetric difference of itself and another.

    |  union(...)
    |      Return the union of sets as a new set.
    |     
    |      (i.e. all elements that are in either set.)

    |  update(...)
    |      Update a set with the union of itself and others.

    |  ----------------------------------------------------------------------
    |  Static methods defined here:
    --------------------------
    三十一、
    a = set()
    print(a)
    list1 = [1,2,3,4]
    a = set(list1)
    print(a)
    -----------------------
    三十二、
    # add()    向集合中添加元素
    set1 = {5, 1, 2, 3, 4}
    set1.add(6)
    print(set1)
    --------------------------
    三十三、
    # pop() 随机弹出一个元素
    a = {'a', 'b', 'f', 4}
    a.pop()
    print(a)
    -------------------------
    三十四、
    # remove() 删除集合中的某个值
    a = {'a', 'b', 'f', 4}
    a.remove(4)
    print(a)
    ----------------------------
    三十五、
    # discard()   删除集合中的某个值,如果这个值不在集合中什么也不做
    a = {'a', 'b', 'f', 4}
    a.discard(4)
    print(a)
    ----------------------------
    三十六、
    # discard()   删除集合中的某个值,如果这个值不在集合中什么也不做
    a = {'a', 'b', 'f', 4}
    a.discard(4)
    print(a)
    a.discard(4)
    print(a)
    ----------------------------
    三十七、
    # difference()  差集     概念:
    # difference_update()  区别就是第一个返回一个新的集合,第二个是把原来集合覆盖
    set1 = {1,2,3,4,7}
    set2 = {2,4,8,111,24}
    set3 = set1.difference(set2)
    print(set3)
    set4 = set1.difference_update(set2)
    print(set4)
    ---------------------------
    三十八、
     
  • 相关阅读:
    图像细节是高频,轮廓是低频
    Matlab实现西格玛(Sigma)平滑滤波
    Matlab实现细线化方法
    Matlab均匀性度量法实现图像二值化
    Matlab图像处理基础算法集锦(转载)
    Win7 下打开wifi共享的方法
    Day29 Matlab实现图像膨胀源代码
    时间类型和字符串类型之间的转换
    error: illegal character 'ufeff' 的解决方案
    无法卸载Sql Server 的解决办法
  • 原文地址:https://www.cnblogs.com/niaocaizhou/p/10918065.html
Copyright © 2011-2022 走看看