天天看點

Python訓練營筆記 資料結構大彙總 Day5

天池龍珠計劃 Python訓練營

所記錄的知識點

  1. isnumeric
  2. ljust rjust
  3. split splitlines
  4. maketrans translate
  5. 不可變類型 與 hash
  6. dict(mapping) 與 dict.items

1、isnumeric

isnumeric:判斷字元串中是否隻包含數字
In [3]: '123124'.isnumeric()
Out[3]: True

In [4]: '123124a'.isnumeric()
Out[4]: False

In [5]: '123124..'.isnumeric()
Out[5]: False

In [6]: help(str().isnumeric)
Help on built-in function isnumeric:

isnumeric() method of builtins.str instance
    Return True if the string is a numeric string, False otherwise.

    A string is numeric if all characters in the string are numeric and there is at
    least one character in the string.
           

2、ljust rjust

ljust:左對齊,填充右邊

rjust:右對齊,填充左邊

In [9]: '1111'.ljust(8,'0')
Out[9]: '11110000'

In [10]: '1111'.rjust(8,'0')
Out[10]: '00001111'

In [11]: help(str().ljust)
Help on built-in function ljust:

ljust(width, fillchar=' ', /) method of builtins.str instance
    Return a left-justified string of length width.

    Padding is done using the specified fill character (default is a space).


In [4]: help(str().rjust)
Help on built-in function rjust:

rjust(width, fillchar=' ', /) method of builtins.str instance
    Return a right-justified string of length width.

    Padding is done using the specified fill character (default is a space).           

3、split splitlines

split:分割兩次,将三個部分變量分别儲存

split splitlines:去除換行符

In [16]: url="www.baidu.com"

In [17]: a,b,*rest = url.split('.',2)

In [18]: a
Out[18]: 'www'

In [19]: b
Out[19]: 'baidu'

In [20]: rest
Out[20]: ['com']

In [21]:

In [21]:

In [21]: c = '''hello
    ...: world
    ...: ha
    ...: ha
    ...: ha'''

In [22]: c.split("\n")
Out[22]: ['hello', 'world', 'ha', 'ha', 'ha']

In [23]: c.splitlines()
Out[23]: ['hello', 'world', 'ha', 'ha', 'ha']

In [24]: c.splitlines(False)
Out[24]: ['hello', 'world', 'ha', 'ha', 'ha']

In [25]: c.splitlines(True)
Out[25]: ['hello\n', 'world\n', 'ha\n', 'ha\n', 'ha']

In [26]: help(str().split)
Help on built-in function split:

split(sep=None, maxsplit=-1) method of builtins.str instance
    Return a list of the words in the string, using sep as the delimiter string.

    sep
      The delimiter according which to split the string.
      None (the default value) means split according to any whitespace,
      and discard empty strings from the result.
    maxsplit
      Maximum number of splits to do.
      -1 (the default value) means no limit.


In [27]: help(str().splitlines)
Help on built-in function splitlines:

splitlines(keepends=False) method of builtins.str instance
    Return a list of the lines in the string, breaking at line boundaries.

    Line breaks are not included in the resulting list unless keepends is given and
    true.
           

4、maketrans translate

maketrans:建立字元映射轉換表

translate:根據字元映射轉換表,轉換字元串中的字元元素

In [6]: table = str.maketrans("hd","az")

In [7]: table
Out[7]: {104: 97, 100: 122}

In [8]: "hello world".translate(table)
Out[8]: 'aello worlz'

In [9]: help(str().maketrans)
Help on built-in function maketrans:

maketrans(...)
    Return a translation table usable for str.translate().

    If there is only one argument, it must be a dictionary mapping Unicode
    ordinals (integers) or characters to Unicode ordinals, strings or None.
    Character keys will be then converted to ordinals.
    If there are two arguments, they must be strings of equal length, and
    in the resulting dictionary, each character in x will be mapped to the
    character at the same position in y. If there is a third argument, it
    must be a string, whose characters will be mapped to None in the result.


In [10]: help(str().translate)
Help on built-in function translate:

translate(table, /) method of builtins.str instance
    Replace each character in the string using the given translation table.

      table
        Translation table, which must be a mapping of Unicode ordinals to
        Unicode ordinals, strings, or None.

    The table must implement lookup/indexing via __getitem__, for instance a
    dictionary or list.  If this operation raises LookupError, the character is
    left untouched.  Characters mapped to None are deleted.           

5、不可變類型 與 hash

用hash(x),隻要不報錯,證明X可被哈希,即為不可變類型

數值、字元、元組 都能被hash,是以它們是不可變類型

清單、集合、字典不能被hash,是以它們是可變類型

In [15]: hash(1),hash("hello"),hash((1,2,3))
Out[15]: (1, 5141752251584472646, 2528502973977326415)

In [16]: hash(list())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-3e2eb619e4e4> in <module>
----> 1 hash(list())

TypeError: unhashable type: 'list'

In [17]: hash(set())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-2699417ebeac> in <module>
----> 1 hash(set())

TypeError: unhashable type: 'set'

In [18]: hash(dict())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-7762fff637c6> in <module>
----> 1 hash(dict())

TypeError: unhashable type: 'dict'

In [19]: help(hash)
Help on built-in function hash in module builtins:

hash(obj, /)
    Return the hash value for the given object.

    Two objects that compare equal must also have the same hash value, but the
    reverse is not necessarily true.

           

6、dict(mapping) 與 dict.items

dict(mapping) 方式建立字典
In [23]: a= [('apple',123),('banana',456)]

In [24]: dict(a)
Out[24]: {'apple': 123, 'banana': 456}

In [25]: b=(('peach',789),('cherry',901))

In [26]: b
Out[26]: (('peach', 789), ('cherry', 901))

In [27]: dict(b)
Out[27]: {'peach': 789, 'cherry': 901}

In [28]: dict(b).items()
Out[28]: dict_items([('peach', 789), ('cherry', 901)])

In [29]: help(dict().items)
Help on built-in function items:

items(...) method of builtins.dict instance
    D.items() -> a set-like object providing a view on D's items
           

歡迎各位同學一起來交流學習心得!