本文共 1709 字,大约阅读时间需要 5 分钟。
风格编程规范部分笔记:
各种类型转换:
-
-
-
- string = 'list'
-
- list1 = list(string)
-
- string1 = ''.join(list1)
-
-
- list1 = list('this is a list')
-
- list1.append('!')
- list1.insert(2,'this is index 3')
-
-
- list1.pop(-1)
- del list1[-1]
- if '!' in list1:
- list1.remove('!')
-
- list1[0] = '0'
- list1[0:2] = list('05')
- list1[1:1] = list('1234')
- list1[1:5] = []
-
- if 'a' in list1:
- index = list1.index('a')
-
- list2 = ['new','list']
- list1.extend(list2)
- print list1
-
- list1.reverse();
- print list1
-
-
- l1 = ['b','c','d','c','a','a']
- l2 = list(set(l1))
-
- l2.sort(key=l1.index)
-
- l1 = ['b','c','d','c','a','a']
- l2 = []
- for i in l1:
- if not i in l2:
- l2.append(i)
- print l2
-
-
-
-
-
-
-
-
- tuple1 = ()
- tuple1 = 1,
- tuple1 = 1,2,3
- tuple1 = tuple([1,2,3,4])
- print tuple1
-
-
-
-
-
-
-
- dict1 = {
- 'key':'value',
- 'key1':'value1'
- }
- a = [('key1','value1'),('key2','value2')]
- dict1 = dict(a)
- dict1 = {}.fromkeys(['key1','key2'],'default_value')
- dict1 = dict(key1='value1',key2='value2')
-
-
- dict1['key3']='value3'
- dict1.setdefault('key5','N/A')
-
-
- del dict1['key3']
- print dict1.pop('key2')
-
-
-
- if 'key1' in dict1:
- dict1['key1']='new_value_1'
-
- if 'key1' in dict1:
- print dict1['key1']
- if dict1.has_key('key1'):
- print dict1['key1']
- print dict1.get('key3','not exists')
- print dict1.keys(),dict1.values()
-
-
- dict2 = dict(key4 = 'value4')
- dict1.update(dict2)
转载地址:http://qotqb.baihongyu.com/