python basic data type exercise

1. Write code, have the following list, and implement each function as required
li = ['alex', 'wusir', 'eric', 'rain', 'alex']

Calculate the length of the list and output
print(len(li))
Answer: The result is 5

Append the element 'seven' to the list, and output the added list
li.append('seven')
print(li)
The result is: ['alex', 'wusir', 'eric', 'rain', 'alex', 'seven']

Please insert the element 'Tony' at the first position of the list, and output the added list
li.insert(0, 'Tony')
print(li)
The result is:
['Tony', 'alex', 'wusir', 'eric', 'rain', 'alex']

Please modify the element in the second position of the list to 'Kelly', and output the modified list
li[1] = 'Kelly'
print(li)
The result is:
['alex', 'Kelly', 'eric', 'rain ', 'alex']

Please add each element of the list l2=[1, 'a', 3, 4, 'heart'] to the list li, which is implemented in one line of code and does not allow cyclic addition.
li.extend([1, 'a', 3, 4, 'heart'])
print(li)
The result is:
['alex', 'wusir', 'eric', 'rain', 'alex', 1, 'a', 3, 4, 'heart']

Please add each element of the string s = 'qwert' to the list li, one line of code, no circular addition is allowed.
li.extend('qwert') The result of
print(li)
is:
['alex', 'wusir', 'eric', 'rain', 'alex', 'q', 'w', 'e', ​​'r ', 't']

Please delete the element 'eric' in the list, and output the added list
li.remove('eric')
print(li)
The result is:
['alex', 'wusir', 'rain', 'alex']

Please delete the second element in the list, and output the deleted element and the list after the deleted element
print(li.pop(1)) The result of
print(li)
is:
wusir
['alex', 'eric', 'rain' , 'alex']

Please delete the 2nd to 4th elements in the list, and output the list after deleting the elements
del li[1:4]
print(li)
The result is:
['alex', 'alex']

Please reverse all the elements of the list and output the reversed list
li.reverse()
print(li)
The result is:
['alex', 'rain', 'eric', 'wusir', 'alex']

Please count the number of times the 'alex' element appears in the list li and output the number of times.
print(li.count('alex'))
result:
2

2. Write code, have the following list, use slices to implement each function
li = [1, 3, 2, 'a', 4, 'b', 5, 'c']

Form a new list by slicing the li list l1,l1 = [1,3,2]
l1 = li[:3]
print(l1)

Form a new list by slicing the li list l2,l2 = ['a',4,'b']
l2 = li[3:6]
print(l2)

Form a new list by slicing the li list l3,l3 = ['1,2,4,5]
l3 = li[::2]
print(l3)

Form a new list l4 by slicing the li list, l4 = [3,'a','b']
l4 = li[1:6:2]
print(l4)

Form a new list l5 by slicing the li list, l5 = ['c']
l5 = li[-1:-2:-1] #Note that it is easy to mistakenly type li[-1] here, it is not a slice, Instead, it is directly assigned to the c string.
# Supplement: l5 = li[-1:] directly also outputs ['c'], which is more elegant.
print(l5)

Form a new list by slicing the li list l6,l6 = ['b','a',3]
l6 = li[-3:0:-2]
print(l6)

3. Write code, have the following list, and implement each function as required.
lis = [2, 3, 'k', ['qwe', 20, ['k1', ['tt', 3, '1']], 89], 'ab', 'adv'']

Make 'tt' in list lis uppercase (in two ways).
The first: upper method
lis[3][2][1][0] = lis[3][2][1][0].upper()

The second: direct index modification
lis[3][2][1][0] = 'TT'
print(lis)

Turn the number 3 in the list into the string '100' (in two ways).
The first method, direct index position assignment modification.
lis[1] = '100'
lis[3][2][1][1] = '100'
print(lis)

The second method utilizes deletion and insertion and replacement of lists.

lis.insert(1, '100')
lis.pop(2)
lis2 = ['qwe', 20, ['k1', ['tt', '100', '1']], 89]
lis.pop(3)
lis.insert(3, lis2)
print(lis)

Turn the string '1' in the list into the number 101 (in two ways).
The first method, direct index modification:
lis[3][2][1][2] = 101
print(lis)

# Second method, replace
lis2 with deletion and insertion of the list = ['qwe', 20, ['k1', ['tt', 3, 101]], 89]
lis.pop(3)
lis.insert (3, lis2)
print(lis)

4. Please use code to implement:
li = ['alex','eric','rain']
Use underscore to splice each element of the list into the string "alex_eric_rain"

Answer: The code is implemented as follows:

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

5. Find the elements in the list li, remove the spaces of each element, and find all elements starting with 'A' or 'a' and ending with 'c', and add them to a new list, and finally loop Print this new list.
li = [ 'taibai ' ,'alexC' ,'AbC ',' egon', ' Ritian', ' Wusir' ,' aqc' ]

answer:

li = [ 'taibai ' ,'alexC' ,'AbC ',' egon', ' Ritian', ' Wusir' ,' aqc' ] li2 = [ ] #print(li2) for i in li: #print(i.strip()) #注意逻辑运算,要先算or再算and,所以要用优先级高的括号把or的运算先括起来。 if (i.strip().startswith('A') or i.strip().startswith('a')) and i.strip().endswith('c'): li2.append(i.strip()) #print(li2) for j in li2: print(j) 最终结果为: aqc

6. Develop a filter program for sensitive words, prompting users to enter comments, if the content entered by the user contains special characters:
Sensitive word list li = ["Teacher Cang", "Tokyo Hot", "Mutolan", "Hatano Yui" ”]
, replace the sensitive words in the content entered by the user with *** and add it to a list; if the content entered by the user has no sensitive words, it is directly added to the above list.

# li = ["Teacher Cang", "Tokyo Hot", "Muto Ran", "Hatano Yui"] 
# l2 = []
# i = input('Please input:').strip()
# if i in li :
# i = '**'
# l2.append(i)
# else:
# l2.append(i)
# print(l2)

7. There is the following list li = [1,3,4',alex',[3,7,8,'taibai'],5,'ritian']
loop to print each element in the list, and then when the list is encountered The loop prints out the elements inside it.
The result I want is (achieved in two ways, one of which is done with range):
1
3
4
'alex'
3
7,
8
'taibai'
5
ritian

第一种方法:
li = [ 1, 3, 4, 'alex', [3, 7, 8, 'taibai' ], 5, 'ritian' ] for i in li: if type(i) == list: for j in i: print(j) else:print(i) 第二种方法:(range) for i in range(len(li)): if type(li[i]) == list: for j in li[i]: print(j) else: print(li[i])

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325040068&siteId=291194637