组合数据类型

字符串
>>> str='a','b','c'
>>> str
('a', 'b', 'c')
>>> str[-1]
'c'
>>> str[1:2]
('b',)
>>>
遍历:>>> for i in str:
...   print(i)
...
a
b
c
>>>


列表
ls=['this','is','string',123]
>>> ls
['this', 'is', 'string', 123]

>>> ls2=['this','is','string',123]
>>> ls2
['this', 'is', 'string', 123]
>>> ls=['this','is','string',123]ls=['this','is','string',123]ls=['this','is','string',123]ls=['this','is','string',123]ls=['this','is','string',123]ls=['this','is','string',123]

>>> ls=['this','is','string','exaple!']
>>> ls
['this', 'is', 'string', 'exaple!']
>>> ls[-1]
'exaple!'
>>> ls[1:2]
['is']
>>> ls[-1]=123
>>> ls
['this', 'is', 'string', 123]

>>> len(ls)
4
>>> classmates=['Micheal','Bob','Nacy','小三','Rose']
>>> classmates
['Micheal', 'Bob', 'Nacy', '小三', 'Rose']
>>> ls.count('is')
1

>>> ls.index('is')
1
>>> ls1=[]
>>> ls1.append(123)
>>> ls1
[123]
>>> ls1.append('123')
>>> ls1
[123, '123']
>>> ls1.insert(1,'HELLO')
>>> ls1
[123, 'HELLO', '123']
>>>
>>>
>>> ls2=list('hello')
>>> ls2
['h', 'e', 'l', 'l', 'o']
>>> ls2=list(range(5))
>>> ls2
[0, 1, 2, 3, 4]
>>> ls1.extend(ls2)
>>> ls1
[123, 'HELLO', '123', 0, 1, 2, 3, 4]
>>> ls2
[0, 1, 2, 3, 4]
>>> ls3=ls1.extend(ls2)
>>> ls3
>>> ls1
[123, 'HELLO', '123', 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
>>> ls2+ls
[0, 1, 2, 3, 4, 'this', 'is', 'string', 123]
>>> ls.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
>>> ls.pop()
123
>>> ls
['is', 'string', 'this']
>>> ls.sort()
>>> ls.append(list('123'))
>>> ls
['is', 'string', 'this', ['1', '2', '3']]
>>> ls[-1][-1]
'3'
>>> ls[2][-1]
's'

遍历
>>> for i in ls2:
...  print(i)
...
0
1
2
3
4
>>> for i in ls1:
...  print(i)
...
123
HELLO
123
0
1
2
3
4
0
1
2
3
4
>>> for i in a:
...  print(i)
...
1
2
3
4

>>> a=tuple('1234')
>>> a
('1', '2', '3', '4')
>>> list('1234')
['1', '2', '3', '4']
>>> a[-2]
'3'




>>> tup=('a',[2,4])
>>> tup[1][1]
4
>>> tup[1][1]=4
>>> tup
('a', [2, 4])
>>> for i in ls:
...  print(i)
...
is
string
this
['1', '2', '3']


字典
>>> name=['Michel','Rose','Nacy','Niki','Niky']
>>> name
['Michel', 'Rose', 'Nacy', 'Niki', 'Niky']
>>> score=[90,100,80,88,99]
>>> score
[90, 100, 80, 88, 99]
>>> d=dict(zip(name,score))
>>> d
{'Michel': 90, 'Rose': 100, 'Nacy': 80, 'Niki': 88, 'Niky': 99}
>>> name
['Michel', 'Rose', 'Nacy', 'Niki', 'Niky']
>>> score
[90, 100, 80, 88, 99]
>>> d1=dict(zip(name,score))
>>> d1
{'Michel': 90, 'Rose': 100, 'Nacy': 80, 'Niki': 88, 'Niky': 99}
>>> d1=dict(zip('cvb','wer'))
>>> d1
{'c': 'w', 'v': 'e', 'b': 'r'}

>>> d['Niki']
88
>>> dict={}
>>> dict
{}
>>> dict={'a'}
>>> dict
{'a'}

>>> dd.keys()
dict_keys(['Michel', 'Rose', 'Nacy', 'Niki', 'Niky'])
>>> dd.values()
dict_values([90, 100, 80, 88, 99])
>>> dd.items()
dict_items([('Michel', 90), ('Rose', 100), ('Nacy', 80), ('Niki', 88), ('Niky', 99)])
>>> dd.get('Niki')
88


集合
colors = {'red','blue','pink'}
sizes = {36,37,38,39}
result = {c + str(s) for c in colors for s in sizes}
print(result)

 

posted @ 2018-03-22 12:08  248黄迎迎  阅读(157)  评论(0编辑  收藏  举报