python高级语法生成式

生成式

列表生成式

 

# 需求1:

# 1. 接收变量k, a, b

s = '51 5000 10000'

 

#  ['51', '5000', '10000']

# 存储整形的k, a, b

# li = []

# for item in s.split():

#     li.append(int(item))

# k,a,b = li

 

 

k,a,b = [int(item) for item in s.split()]

print(k,a,b)

 

 

 

# 需求2: 生成一个列表, 列表元素分别为[1**1,2**2,3**3,4**4....n**n],

#

# li = []

# for i in range(1,8):

#     li.append(i**i)

# print(li)

 

print([i**i for i in range(1,8)])

 

 

 

# 总结:

#       [ experssion(或者函数)  for item1 in 序列  if 判断语句

#                              for item2 in 序列  if 判断语句

#                               for item3 in 序列  if 判断语句

#       ]

 

print([i**2 for i in range(1,8) if i%2==0])

 

 

列表生成式实现矩阵的转置

li =[

[1,2,3,4],

[5,6,7,8],

[9,1,2,3],

[4,5,6,7]

]

print [[row(i) for row in li ]for i in range(4)]

 

 

 

列表生成式变形之for循环嵌套

 

将3*3的矩阵转换为一维数组

 3*3矩阵:[

           [1,2,3],

           [4,5,6],

           [7,8,9]

       ]

 

 一维数组: [1,2,3,4,5,6,7,8,9]

 

li = [

    [1,2,3],

    [4,5,6],

    [7,8,9]

]

 resLi = []

 for item1 in li: # [1,2,3]   [4,5,6]  [7,8,9]

     for item2 in item1:

         resLi.append(item2)

 print(resLi)

 

 print([item2 for item1 in li for item2 in item1])

 

 

 resLi = []

 for item1 in li: # [1,2,3]   [4,5,6]  [7,8,9]

     for item2 in item1:

         if item2 %2 ==0:

             resLi.append(item2)

 print(resLi)

 

 print([item2 for item1 in li for item2 in item1 if ite2 %2==0])

 

 

 

 

 

 

 

字典生成式:

# 需求1: 假设有20个学生,学生分数在60-100之间,筛选出成绩在90分以上的学生

import random

 

# stuInfo={}

# for i in range(20):

#     # name = "westos"+ str(i)

#     # score = random.randint(60,100)

#     # stuInfo[name] = score

#     stuInfo["westos"+ str(i)] = random.randint(60,100)

# print(stuInfo)

 

 

 

stuInfo = {"westos"+ str(i):random.randint(60,100)

            for i in range(20)}

 

# 筛选出score>90

 

# highScore = {}

# for name, score in stuInfo.items():

#     if score > 90:

#         highScore[name] = score

# print(highScore)

 

print({ name:score for name, score in stuInfo.items() if score > 90 })

 

 

 

# 需求2:

"""                                                                                                           

** 需求:

    假设已有若干用户名字及其喜欢的电影清单,

    现有某用户, 已看过并喜欢一些电影, 现在想找

    新电影看看, 又不知道看什么好.

 

** 思路:

    根据已有数据, 查找与该用户爱好最相似的用户,

    即看过并喜欢的电影与该用户最接近,

    然后从那个用户喜欢的电影中选取一个当前用户还没看过的电影进行推荐.

    

    

 100:    

userInfo = {

    'user1':{'file1', 'file2', 'file3'},

    'user2':{'file2', 'file2', 'file4'},

}    

"""

 

# # 1. 随机生成电影清单

# import random

# data = {}   # 存储用户及喜欢电影清单的信息;

# for userItem in range(100):

#     files = set([])

#     for fileItem in range(random.randint(4,15)):

#         files.add( "film" + str(fileItem))

#     data["user"+str(userItem)] = files

# print(data)

#

import pprint

pprint.pprint({

    "user"+str(userItem):

        {"film" + str(fileItem) for fileItem in range(random.randint(4,15))}

        for userItem in range(100)   })

 

 

 

 

# 3. 将字典的key值和value值调换;

d = {'a':'apple', 'b':'bob', 'c':'come'}

 

print({v:k for k,v in d.items()})

你可能感兴趣的:(python高级语法生成式)