The second chapter problem sets

  1, use code implementation: the use of each element of the list underlined spliced ​​into a string, li = [ 'alex', 'eric', 'rain']

li = ['alex', 'eric', 'rain']
li1 = '_'.join(li)
print(li1)

  2, find a list of elements, each element of the space removed, and begin with a look or A and ending with all the elements of c

'''
li = ['alec', ' aric', 'Alex', 'Tony', 'rain']
tu = ('alec', ' aric', 'Alex', 'Tony', 'rain')
dic = {'k1': 'alec', 'k2': ' aric', 'k3': 'Alex', 'k4': 'Tony'}
'''
#对列表
for i in range(1, len(li)):
    li[i] = li[i].strip()
print(li)

for i in range(0,len(li)):
    if li[i].startswith('a') or li[i].startswith('A') and li[i].endswith('c'):
        print(li[i])

#对字典
for i in dic:
    dic[i] = dic[i].strip()
print(dic)

for i in dic:
    if dic[i].startswith('A 'or' A ') and DIC [I] .endswith (' C '): 
# because tuples are immutable, after not tuples of elements in the change can not Ganso amplitude to the original, but the search operation can be carried out
# of ancestral
        Print (DIC [I], End =' ')

for i in tu:
    if i.startswith('a' or 'A') and i.endswith('c'):
        print(i)

  3, to write the code, the following list, each function according to requirements

= Li [ 'Alex', 'Eric', 'Rain'] 

# and outputs the calculated length list 
Print (len (Li)) 

# as the element 'seven', and outputs the added list 
li.append ( 'seven' ) 
li.pop () 
Print (Li) 
Print (Li [-1]) 
li.insert (-1, 'Seven') # always insert a new element to the left index element 
Print (Li) 
li.remove ( 'Seven ') 
Print (Li) 
li.extend ([' Seven ']) 
must take it on the list when a new element is added with a # Extend, a list element is added only when using insert into another list, insert and will not append 
Print (Li) 

# modify the position of the second element of the list is 'Kelly', and outputs the modified list 
Li [. 1] = 'Kelly' 
Print (Li)

  

 

Guess you like

Origin www.cnblogs.com/DcentMan/p/11128777.html