1.集合
string = ‘list’
list1 = list(string)
string1 = ‘’.join(list1)
list1 = list(‘I have a pen’)
list1.append(’!’)
list1.insert(2,'this is a chuanqi ')
list1.pop(-1)
del list1[-1]
list1.remove(‘n’)
list_new = [1,2,3,4,5]
list_new[0] = 8
list_new[0:2] = list(‘05’)
list_new[1:1] = list(‘1234’)
list_new[1:3] = list[]
list_new = [1,2,3,4,5]
if 1 in list_new:
index = list_new.index(1)
print(index)
list_new = [1,2,3,4,5]
list_n = [8,9,10]
list_new.extend(list_n)
print(list_new)
list_new = [1,2,3,4,5]
list_new.reverse()
print(list_new)
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
2.元组
tuple1 = tuple([1,2,3,4])
3.字典
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’)
dic = {‘name’:‘egon’,‘age’:18,‘salary’:50000,‘hobbies’:‘happy’,‘height’:180}
dic.setdefault(‘height’,185)
print(dic)
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)