Some examples

Python in accordance with indexed access list

50 = [95.5,85,59]
print 50 [0]
print 50 [1]
Print 50 [2]

 

Python's reverse access list

50 = [95.5, 85, 59]
Print 50 [-1]
print 50 [-2]
print 50 [3]

 

Adding a new element of Python

L = ['Adam', 'Lisa', 'Bart']
L.insert(2,'Paul')
print L

 

Python remove elements from the list

L = ['Adam', 'Lisa', 'Paul', 'Bart']
L.pop(2)
L.pop(2)
print L

 

Python is replaced elements

L = ['Adam', 'Lisa', 'Bart']
L[2]='Adam'
L[0]='Bart'
print L

 

Create a tuple of Python

t = (0,1,2,3,4,5,6,7,8,9)
print t

 

Creating a single-element tuple of Python

t = ('Adam',)
print t

 

Python's "variable" of the tuple

t = ('a', 'b', ['A', 'B'])
print t

 

What is Python's dict

d = {
'Adam':95,
'Lisa':85,
'Bart':59,
'Paul':75
}
print d

 

Features in Python dict

d = {
95:'Adam',
85:'Lisa',
59:'Bart'
}
print d[95]

 

Update Python dict

d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
d[72]='Paul'
print d

 

Python's dict traversal

d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key+':',d[key]

 

Python what is set

s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
print s

 

Python's visit set

s = set(['adam','bart'])
print 'adam' in s
print 'bart' in s

 

set of Python's features

Dict internal structure and the like set, the only difference is not stored value, thus determining whether an element in the set quickly.

Similar elements of the set and stored dict key, the object must be constant, therefore, the object can not be placed in any of the variable in the set.

months = set(['Jan','Feb'])
x1 = 'Feb'
x2 = 'Sun'

if x1 in months:
print 'x1: ok'
else:
print 'x1: error'

if x2 in months:
print 'x2: ok'
else:
print 'x2: error'

 

Traversing the set of Python

s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print x[0]+':',x[1]

 

Guess you like

Origin www.cnblogs.com/wushujun/p/11431062.html
Recommended