Small turtle Lesson Eleven: List: A "on steroids" 3 summarizes the array of reflection

1. How will the bottom of the list 'Little Turtle' changed to 'small squid'?
list1 = [1, [1, 2, [ ' small Turtle']], 3, 5, 8, 13, 18]

#自己做的时候想了很久,也没找到很好的提取第三层修改的方法,所以用了个很基础的方法:
list1 = [1, [1, 2, ['小甲鱼']], 3, 5, 8, 13, 18]
list1.pop(1)
list2 = [1, 2, ['小鱿鱼']]
list1.insert(1,list2)
print(list1)
实际答案:    list1[1][2][0] = '小鱿鱼'              

Dictionary list1 [1] [2] [ 0] = ' small squid'
corresponds list1 [1] = [1, 2, [ ' small Turtle']]
List1 [. 1] [2] = [ 'small Turtle']
This a list of which only a small string of 0 turtle
so list1 [1] [2] [ 0] = small turtles
other then directly used instead of

PS: Have you heard of a list or list comprehensions resolve it?
Have not heard? ! It does not matter, our on-site to learn about it, look at the expression:

[I * i for i in range (10)]
What do you think will print what?

[ i*i for i in range(10) ]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Were actually printed square 0-9 each number, and then also on the list inside wood there? !
List comprehensions (List comprehensions), also known as a list comprehension, inspired by functional programming language Haskell. Ta is a very useful and flexible tool that can be used to dynamically create a list, the syntax as:
[A related expression for A in B]
For example:

list1 = [x**2 for x in range(10)]
list1
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Equivalent to

list1 = []
for x in range(10):
list1.append(x**2)

Author: Innocence villains
link: https: //www.jianshu.com/p/1c467e595507
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.
2. Question: Please list below the results obtained in IDLE and follow the example above list comprehensions to restore them.
list1 = [(x, y) for x in range (10) for y in range (10) if x% 2 == 0 if y% 2! = 0]

自己的答案:
list1 = []
#定义一个列表
for x in range(10):
    if x%2 == 0:
        for y in range(10):
               if y%2 != 0:
                   list1.extend([(x,y)])
    else:
        continue

print(list1)
标准答案:
list1 = []
for x in range(10):
    for y in range(10):
        if x % 2 == 0:
            if y % 2 != 0:
                list1.append((x, y))
                #这里要用进入列表的append,而不是直接print,这样不会输出列表

##双for循环,优先将第二层循环结束后再继续第一层

3. learn and use: Use a list comprehension replenishment is accidentally obliterated part of a small turtle

list1 = ['1.Jost do It','2.一切皆有可能','3.让变成改变世界','4.Impossible is nothing']
list2 = ['4.阿迪达斯','2.李宁','3.鱼C工作室','1.耐克']
list3 = [XXXXXXXXX]
print(list3)
for each in list3:
    print(each)

1.耐克:Jost do It
2.李宁:一切皆有可能
3.鱼C工作室:让变成改变世界
4.阿迪达斯:Impossible is nothing
list1 = ['1.Jost do It','2.一切皆有可能','3.让变成改变世界','4.Impossible is nothing']
list2 = ['4.阿迪达斯','2.李宁','3.鱼C工作室','1.耐克']
展开程序:
list3=[]
for app in list1:
    for bdd in list2:
        if app[0] == bdd[0]:
            print(bdd+':'+ app[2:])
            #这里和上题对比,就没有进入列表
            
组合程序:
list3=[(bdd+':'+ app[2:]) for app in list1 for bdd in list2 if app[0] == bdd[0]]
#####注意!1.开头的一定就是要输入进列表的程序
2.每个逻辑语句按照出现顺序层层递进
3.list=[]一定记住是中括号
4.因为':'也是字符串,所以可以直接相加


最终程序:
list1 = ['1.Jost do It','2.一切皆有可能','3.让变成改变世界','4.Impossible is nothing']
list2 = ['4.阿迪达斯','2.李宁','3.鱼C工作室','1.耐克']
list3=[(bdd+':'+ app[2:]) for app in list1 for bdd in list2 if app[0] == bdd[0]]
print(list3)
for each in list3:
    print(each)

最终答案:
['1.耐克:Jost do It', '2.李宁:一切皆有可能', '3.鱼C工作室:让变成改变世界', '4.阿迪达斯:Impossible is nothing']
1.耐克:Jost do It
2.李宁:一切皆有可能
3.鱼C工作室:让变成改变世界
4.阿迪达斯:Impossible is nothing

Summary:
1. a list of operators used:
comparison: If there are a plurality of elements, a default start from the first comparator element, comparing the size of the corresponding ASCII code value;
logical (and or):
connect (+): [1, 2 3] + [4, 5, 6] result [1, 2, 3, 4, 5, 6]
repeat (*): [ '! Hi '] * 4 result [, 'Hi' Hi! ' ! !! ',' Hi ', ' Hi ']
membership (in and not in): 3 in [1 , 2, 3] the result is True

Method list: the dir (List)
the append (): Add a new object at the end of the list
extend (): a plurality of one-time value is added at the end of the other sequence list (list with a new original extended list)
COUNT (): the number of statistics appeared
index (target, the starting position, the cutoff position): returns the position parameter in the list
insert (): the object is inserted into the list specified location
pop (): remove one element in the list (by default the last element You can specify other locations), and returns the value of the element
remove (): remove the first match of a list of values (can not delete the specified position)
Reverse (): flip a list of
sort (): according to the specified way to sort a list of members, the default is ascending order
special: sort (reverse = True) represents the descending, the default is False

Built-in functions list:
Comparison of two elements of the list: operator.eq (list1, list2) (provided for an import operator)
count the number of list elements: len (list1)
returns a list of elements in the maximum value: max (list1)
returns a list of minimum elements: min (list1)
converts a list of tuples: list (tuple1)

Author: Innocence villains
link: https: //www.jianshu.com/p/1c467e595507
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

2. List derivation formula:
A (to be entered into the list of things, and the back to be processed) for A in. . . . .

3.list[0][1][2]=“aaaaa”

The first element in the list corresponds to a 0-th element in the second element, which was replaced aaaa

Published 17 original articles · won praise 1 · views 361

Guess you like

Origin blog.csdn.net/cccccccaaaaaaaaa/article/details/105228333