Road python 12

  1. Finishing notes today, the code in class at least knock three times.

  2. Do the following small problem with list comprehensions

  3. Filtered list of strings of length less than 3, and the remaining to uppercase

    l = ['wusir', 'laonanhai', 'aa', 'b', 'taibai']
    # print([i.upper() for i in l if len(i) > 3])
  4. Find (x, y) where x is an even number between 0-5, y is between ancestral odd list consisting of 0-5

    # print([(i,j) for i in range(6) if i % 2 == 0 for j in range(6) if j % 2 == 1])
  5. 3,6,9 seeking in the list consisting of M M = [[1,2,3], [4,5,6], [7,8,9]]

    print([[i-2,i-1,i] for i in range(3, 10, 3)])
  6. 50 can be obtained within a square of the number divisible by 3, and placed into a list.

    print([i**2 for i in range(50) if i % 3 == 0])
  7. Construction of a list: [ 'python1 of', 'python2 period', 'python3 period', 'python4 period', 'python6 period', 'python7 period', 'python8 period', 'python9 period', 'python10 of']

    print(['python%s期'% i for i  in range(1,11) if i != 5])
  8. Construction of a list: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]

     print([(i,i+1) for i in range(6)])
  9. Build a list of: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

    print([i+i for i in range(10)])
  10. There is a list of l1 = [ 'alex', 'WuSir', 'old boys', 'Bai'] which is configured in a list [ 'alex0', 'WuSir1', 'old boy 2', '3 Bai']

    print([l1[i] + str(i) for i in range(len(l1))])
  11. The following data types:

x = {'name':'alex',
     'Values':[{'timestamp':1517991992.94,'values':100,},
               {'timestamp': 1517992000.94,'values': 200,},
            {'timestamp': 1517992014.94,'values': 300,},
            {'timestamp': 1517992744.94,'values': 350},
            {'timestamp': 1517992800.94,'values': 280}],}

The above type of data into the following formula derived by the list: [[1,517,991,992.94, 100], [1,517,992,000.94, 200], [1,517,992,014.94, 300], [1,517,992,744.94, 350], [1,517,992,800.94, 280]]

print([[i['timestamp'], i['values']] for i in x['Values']])
  1. Complete with a Cartesian product list

    What is the Cartesian product? Cartesian product is a list, the list elements inside the iterative type is made available to elements of the input element of the group consisting of, Cartesian product of the list so that the length is equal to the product of the length of the input variables.

a. Construction of a list, which is a list of three different sizes of T-shirts, each color has two dimensions (a listing of elements inside a tuple type).

colors = ['black', 'white']
sizes = ['S', 'M', 'L']
tshirts = [(color, size)for color in colors for size in sizes]
print(tshirts)

B. Construction of a list, a list of cards which elements are removed after the king size, all classes of cards (list elements inside a tuple type).

l1 = [('A','spades'),('A','diamonds'), ('A','clubs'), ('A','hearts')......('K','spades'),('K','diamonds'), ('K','clubs'), ('K','hearts') ]

ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
lis = [(rank, suit) for suit in suits for rank in ranks]
  1. Briefly the difference between the yield from the yield.
    yield is the identification generator function, function as long as the yield then he is a generator function instead of the function.
    next gets the value from the generator function of yield.
    From the perspective of ease of understanding yield from speaking: it will be one for each iteration as a generator generating a target value inside:
    yield from [ 'Wei Long', 'Old popsicles',' Arctic ',' with sheep ']
    is equivalent to:
    the yield' Waylung '
    the yield' old popsicles'
    the yield 'Arctic'
    the yield 'with sheep'
    from deeper perspective to talk yield from two functions (must be memorized, face questions):

    1. He can completely replace the inner loop, improve efficiency, so be it reads more smoothly (you can verify the next problem).

    2. You can also create a channel, the inner generator connected directly to the generator outer client

  2. Look at the following code to its ability to simplify? After you talk about the advantages of simplified?

def chain(*iterables):

    for it in iterables:

        for i in it:

            yield i

g = chain('abc',(0,1,2))

print(list(g))  # 将迭代器转化成列表

def chain(*iterables):
  for it in iterables:
    yield from it
g = chain('abc',(0,1,2))
print(list(g))
优点: 他可以完全代替了内层循环,提高效率,让待吗读起来更顺畅(下一道题就可以验证)。
  1. Look at the code seeking results ( interview questions ):
v = [i % 2 for i in range(10)]
print(v)

v = (i % 2 for i in range(10))
print(v)

for i in range(5):
    print(i)
print(i)
  1. See the result of evaluating the code :( interview questions )
def demo():
    for i in range(4):
        yield i

g=demo()

g1=(i for i in g)
g2=(i for i in g1)

print(list(g1))
print(list(g2))
  1. See the result of evaluating the code :( interview questions )
def add(n,i):
    return n+i

def test():
    for i in range(4):
        yield i

g=test()
for n in [1,10]:
    g=(add(n,i) for i in g)

print(list(g))

# 分步去分析循环。

Guess you like

Origin www.cnblogs.com/zanao/p/11079849.html