Key battle wander 002_ practice

1. Write the code, the following list, each function according to requirements

li = [ "alex", " WuSir", "ritian", "barry", "wenzhou"]
length calculating list and outputs
as the element "seven", lists and outputs the added
In the first of a list position of the insertion element "Tony", and outputs the added list
edit list element to the second position "Kelly", and outputs the modified list
set list l2 = [1, "a" , 3,4, " heart "] is added to each element in the list li, a line of code to achieve, does not allow the cycle is added.
Enclose the string s = "qwert" each li element added to the list, the line of code to achieve, does not allow the cycle is added.
Please delete elements in the list "ritian", list and outputs the add
remove list of two elements, a list of the elements and remove elements and outputs to delete
delete the list of 2-4 elements, and the output list after removing elements

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]
print(len(li))
li.append('seven')
print(li)
li.insert(0,'Tony')
print(li)
li[1] = 'Kelly'
print(li)
l2=[1,"a",3,4,"heart"]
li.extend(l2)
print(li)
s = "qwert"
li.extend(s)
print(li)
li.remove('ritian')
print(li)
li.pop(1)
print(li.pop(1))
print(li)
del li[1:4]
print(li)

2. Write code that has the following list, use slices to achieve each function

li = [1, 3, 2 , "a", 4, "b", 5, "c"]
is formed by slicing a new list of l1 li list, l1 = [1,3,2]
By List li slices to form a new list l2, l2 = [ "a" , 4, "b"]
is formed by slicing a new list of li l3 list, l3 = [ "1,2,4,5]
by list li slice forming new list l4, l4 = [3, " a", "b"]
to form a new list l5 by slicing li list, l5 = [ "c"]
to form a new list by slicing li list l6 , l6 = [ "b", "a", 3]

l1 = li[:3]
l2 = li[3:6]
l3 = li[::2]
l4 = li[1:6:2]
l5 = li[-1:]
l6 = li[-3:-8:-2]

3. Write code that has the following list, as required to achieve each function.

lis = [2, 3, " k", [ "qwe", 20, [ "k1", [ "tt", 3, "1"]], 89], "ab", "adv"]
list lis the "tt" capitalized (two ways).
3, the list of numbers into the string "100" (in two ways).
The list of strings of "1" to numeral 101 (in two ways).

lis[3][2][1][0] = 'TT'
print(lis)
lis[3][2][1][0] = lis[3][2][1][0].upper()
print(lis)

lis[3][2][1][1] = 100
print(lis)
lis[3][2][1][1] = str(lis[3][2][1][1] + 97)
print(lis)

lis[3][2][1][-1] = 101
print(lis)
lis[3][2][1][-1] = int(lis[3][2][1][-1]) + 100
print(lis)

4. implemented in code:
Li = [ "Alex", "wusir", "Taibai"]
using each element of the list spliced into underscore character string "alex_wusir_taibai"

li = ["alex", "wusir", "taibai"]
l1 ="_".join(li)
print(l1)

s =''
for i in li:
    s += i+'_'
print(s[:-1])

The use for the index print range loop and the following list.

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"]

index = 0
for i in li:
    print(index)
    index += 1
    
for i in range(len(li):
    print(i)

6. The use for loop and within the range 100 to find all of these and even-even inserted into a new list.

lis = []
for i in range(0,101,2):lis.append(i)
print(lis)

7. Use for loop and to identify within the range 503 can be divisible, and insert these numbers into a new list.

lis = []
for i in range(0,51):
    if i%3 == 0:
        lis.append(i)
print(lis)

8. The use for loop and range from 100 to 1, reverse printing.

for i in range(100,0,-1):print(i)

9. The use for loop and range from 100 to 10, all even reverse added to a new list, then the list of filter elements, the number divisible by four stay.

lis = []
for i in range(100,9,-2):
    lis.append(i)
for i in lis:
    if i%4 == 0:
        lis.remove(i)
print(lis)

10. The use for loop and Range, will add a digital 1-30 into a list, the list and the cycle will be changed to a number divisible by 3 *.

lis = []
for i in range(1,31):
    lis.append(i)
for s in lis:
    if s%3 == 0:
        lis[lis.index(s)] = '*'
    index += 1
print(lis)

11. Find a list of elements of li, remove each element of the space, and identify with "A" or "a" at the beginning, and with "c" at the end of all the elements, and add to a new list, the last cycle Print this new list.
li = [ "TaiBai", " alexC", "AbC", "egon", "riTiAn", "WuSir", "aqc"]

li = ["TaiBai ", "alexC", "AbC ", "egon", " riTiAn", "WuSir", " aqc"]
lis = []
for i in li:
    i = i.strip()
    if (i.startswith('A') or i.startswith('a') )and i.endswith('c'):
        lis.append(i)
for i in lis:
    print(i)

12. The development of sensitive words filtered program prompts the user to type a comment, if the user input contains special characters:
the sensitive word list li = [ "Cang teacher", "Tokyo hot", "Muto blue", "Yui Hatano "]
content input by the user will be sensitive to the spelling of equal length (Cang teacher Alternatively **), and added to a list; if the user input is not sensitive words, directly added to the above list .

li = ["太白", "东京", "武功", "日本"]
s = input('请输入评论内容:')
lis = []
for i in li:
    if i in s:
        s = s.replace(i,'*'*len(i))
lis.append(s)
print(lis)

13. The following list (optional problems)
Li = [. 1,. 3,. 4, "Alex", [. 3,. 7,. 8, "Taibai"],. 5, "RiTiAn"]
loop to print each element of the list, Print out the list met its recycling elements inside.
I want results are:
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 s in i:
            if type(s) == str:
                print(s.lower())
            else:
                print(s)
    elif type(i) == str:
        print(i.lower())
    else:
        print(i)
      

Guess you like

Origin www.cnblogs.com/xiaohei-chen/p/11863211.html