In [14]:
for i in dir(str):
print(i,end = ', ')
In [16]:
help(str.join)
In [17]:
note = "I skipped many sections and only revisit the parts I usually forgot in the daily coding experience."
print(type(note))
In [20]:
note.isalpha()
# check whether the chracters in the string are all alphabetic. If there is one not letter, return False
# spacing is not the the letter, hence it returns false here
Out[20]:
In [34]:
note_list = note.split()
print(note_list)
note_no_spacing = ''.join(note_list)
note_no_spacing = note_no_spacing[:-1] # remove period in the end
print(note_no_spacing)
print(type(note_no_spacing))
print(note_no_spacing.isalpha())
In [35]:
note.istitle()
Out[35]:
In [38]:
note_being_title = note.title()
print(note_being_title)
print(note)
In [39]:
note.isupper()
Out[39]:
In [41]:
note.upper()
Out[41]:
In [42]:
note.capitalize()
Out[42]:
In [43]:
if 'skip' in note:
print(True)
In [49]:
note_with_spacing_start_end = ' ' + note + ' '
print(note_with_spacing_start_end)
print(len(note_with_spacing_start_end))
note_remove_spacing = note_with_spacing_start_end.strip()
print(len(note_remove_spacing))
print(len(note))
In [50]:
note
Out[50]:
In [52]:
lst = note.split()
print(lst)
In [53]:
id(lst)
Out[53]:
In [55]:
lst1 = lst.remove
print(id(lst1))
In [58]:
lst.append('hello')
In [60]:
print(lst)
In [61]:
id(lst) # list is changed in-place by "append", hence the id in memory is same.
Out[61]:
In [63]:
lst2 = ["we", "are", "finghting", "against", "COVID2019"]
print(lst2)
print(id(lst2))
In [64]:
lst.extend(lst2)
print(lst)
print(id(lst))
In [65]:
del lst[2]
print(lst)
In [66]:
lst[2] = 'lots of'
print(lst)
In [75]:
lst.insert(3,'sections')
print(lst)
In [79]:
lst.remove('usually')
print(lst)
In [81]:
lst.pop(0)
Out[81]:
In [83]:
print(lst)
In [84]:
lst[0] = "I"
print(lst)
In [86]:
lst.pop(3)
Out[86]:
In [87]:
print(lst)
In [89]:
lst.pop(3,4) # note that pop only accpets one argument
In [109]:
from __future__ import division
import numpy as np
a = np.random.randn(3)
print(type(a))
print(id(a))
a_list = a.tolist()
print(id(a_list))
In [110]:
a_list
Out[110]:
In [115]:
a_list.sort()
In [121]:
a_list
Out[121]:
In [122]:
a_list.reverse()
a_list
Out[122]:
In [120]:
a = [3,5,1,6]
a.reverse()
a
Out[120]:
In [127]:
reversed(a)
a
Out[127]:
In [128]:
a.reverse()
In [129]:
a
Out[129]:
In [137]:
b = [i for i in reversed(a)]
b
Out[137]:
In [134]:
help(reversed)
In [142]:
setence_1 = "I am a chef here, but not the bos! Bos is standing\nthere!"
In [143]:
setence_1.split()
Out[143]:
In [145]:
help(str.split)
In [148]:
setence_1.split(' ', 0)
Out[148]:
In [149]:
setence_1.split(' ', 1)
Out[149]:
In [150]:
setence_1.split(' ', 2)
Out[150]:
In [151]:
setence_1.split(' ', -1)
Out[151]:
In [ ]:
In [152]:
s = 'He is, a farmer.'
In [161]:
ass
Out[161]:
In [163]:
ass = s.split(',')
print(ass)
xx = []
for i in ass:
xx.extend(i.split())
print(xx)
new_str = ' '.join(xx)
print(new_str)
In [5]:
fang={} # an empty dict
print(id(fang))
fang['name'] = "Pipi"
print(fang)
print(fang.keys())
print(id(fang))
我们可以看出保存在内存中的字典可以被原地修改,这点类似于list
In [ ]:
方法2:利用元祖构建字典
In [6]:
name_list = (["family-name", "fang"], ["given-name", "Pipi"])
print(name_list)
name_dic = dict(name_list)
print(name_dic)
Alternatively, we can use:
In [7]:
name_dict = dict(family_name="FANG", given_name="Pupu")
print(name_dict)
In [ ]:
方法3:利用fromkeys
In [8]:
web = {}.fromkeys(("family-name", "given-name"), "FANG")
print(web)
In [12]:
web1 = {}.fromkeys(("family-name", "given-name"), ("FANG", "Pipi"))
print(web1)
In [13]:
person_info = {}.fromkeys(("lucky-numer1", "lucky-number2"), 99)
print(person_info)
In [14]:
person_info['lucky-number2']
Out[14]:
python调取列表时候总是需要从头开始读取,直到找到指定的那个索引值。但是,在字典中是通过“键”来得到值的,因此效率更高。
In [ ]: