Python 列表推导式实战示例
列表推导式是Python中一种简洁高效的创建列表的方式,可以替代传统的for循环,使代码更加简洁易读。
1. 基本用法
# 传统for循环方式 squares = [] for i in range(10): squares.append(i * i) 列表推导式方式(更简洁) squares = [i * i for i in range(10)] print(squares) # 输出: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]2. 带条件的列表推导式
# 只生成偶数的平方 even_squares = [i * i for i in range(10) if i % 2 == 0] print(even_squares) # 输出: [0, 4, 16, 36, 64] 多个条件 filtered_numbers = [x for x in range(20) if x > 5 and x % 3 == 0] print(filtered_numbers) # 输出: [6, 9, 12, 15, 18] 条件表达式(类似三元运算符) numbers = [1, 2, 3, 4, 5] result = ['偶数' if x % 2 == 0 else '奇数' for x in numbers] print(result) # 输出: ['奇数', '偶数', '奇数', '偶数', '奇数']3. 与普通循环对比示例
# 场景:从字符串列表中提取长度大于3的字符串并转为大写 words = ['apple', 'cat', 'banana', 'dog', 'elephant', 'fox'] 传统for循环方式 result1 = [] for word in words: if len(word) > 3: result1.append(word.upper()) 列表推导式方式 result2 = [word.upper() for word in words if len(word) > 3] print("传统循环结果:", result1) # 输出: ['APPLE', 'BANANA', 'ELEPHANT'] print("列表推导式结果:", result2) # 输出: ['APPLE', 'BANANA', 'ELEPHANT'] 性能对比(列表推导式通常更快) import time 测试传统循环 start1 = time.time() for _ in range(100000): result = [] for word in words: if len(word) > 3: result.append(word.upper()) end1 = time.time() 测试列表推导式 start2 = time.time() for _ in range(100000): result = [word.upper() for word in words if len(word) > 3] end2 = time.time() print(f"传统循环耗时: {end1 - start1:.4f}秒") print(f"列表推导式耗时: {end2 - start2:.4f}秒") print(f"列表推导式比传统循环快: {(end1 - start1) - (end2 - start2):.4f}秒")4. 嵌套列表推导式
# 创建二维矩阵 matrix = [[i * j for j in range(1, 4)] for i in range(1, 4)] print(matrix) # 输出: [[1, 2, 3], [2, 4, 6], [3, 6, 9]] 展平二维列表 matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] flattened = [num for row in matrix for num in row] print(flattened) # 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9]总结:列表推导式让代码更简洁、更Pythonic,但在复杂逻辑时仍需考虑可读性。对于简单的转换和过滤操作,列表推导式是首选。
1.my-tag标签组件封装
作用:双击显示输入框,失去焦点后隐藏输入框,修改内容并回车保存
<template> <div class="my-tap"> <input v-if="isEdit" class="input" type="text" placeholder="输入标签" /> <div v-else class="text" @dblclick="handleClick">新兰</div> </div> </template> <script> export default { data() { return { isEdit: false, } }, methods: { handleClick() { //双击后切换到显示状态 this.isEdit = true //等dom更新完获取焦点 this.$nextTick(() => { this.$refs.inp.focus() }) } } } </script>2.my-table 表格组件封装
1.数据不能写死,动态传递表格渲染的数据
2.结构不能写死:多处结构自定义
(1)表头支持自定义
(2)主体支持自定义
<script> import MyTable from './App.vue' export default { components: { MyTable }, data() { return { tempText: 'cr', tempText1: '温简言', goods: [ { id: 101, picture: 'https://c-ssl.dtstatic.com/uploads/blog/202407/30/jJS5ZpX0Se9G2v6.thumb.1000_0.jpeg' }, { id: 101, picture: 'https://c-ssl.dtstatic.com/uploads/blog/202407/30/jJS5ZpX0Se9G2v6.thumb.1000_0.jpeg' }, {id:101, picture:'https://c-ssl.dtstatic.com/uploads/blog/202407/30/jJS5ZpX0Se9G2v6.thumb.1000_0.jpeg'} ] } }, } </script>3.单页面应用
单页面开发效率高性能好的原因是:页面按需更新
要按需更新首先要明确:访问路径和组件的对应关系 ---->路由
4.插件VueRouter
2个核心步骤
(1)创建需要的组件,配置路由规则
(2)配置导航,配置路由出口
<div class="footer_wrap"> <a href="#/find">发现</a> <a href="#/my">我的</a> <a href="#/friend">朋友</a> </div> <div class="top"> <router-view></router-view> </div>组件分类
1.页面组件和复用组件(都是.vue类型文档)
2.页面组件-views文件夹=>配合路由,页面展示
复用组件-components.文件夹 => 封装复用
5.路由的封装抽离
import App from "./App.vue" import MyTable from "./MyTable.vue" Vue.use(vueRouter) //插件初始化 const router = new vueRouter({ //route 一条路由器规则 { path:路径, component:组件 } routes: [ { path: './App.vue', component: App }, { path:'/MyTable',component:MyTable }, ] })6.声明式导航
1.vue-router提供一个全局组件router-link(取代a标签)
(1)能跳转,配置to 属性指定路径。
(2)能高亮,默认就会提供高亮类名,可直接设置高亮样式
<template> <div class="table-case"> <MyTable :data="goods"></MyTable> <div class="footer_wrap"> <router-link to="/find">发现</router-link> <router-link to="/my">我的</router-link> <router-link to="/part">朋友</router-link> </div> <div class="top"> <router-view></router-view> </div> </div> </template>2.两个类名
① router-link-active模糊匹配(用的多)
to="/my"可以匹配/my/my/a/my/6
② router-link-exact-active精确匹配
to="/my"仅可以匹配/my
3.跳转传参
①查询参数传参: to="/path?参数名=值”
¥route.query.参数名
②动态路由传参:
//创建一个路由对象 const router = new VueRouter({ router: [ { path: '/searth/:words', component: Search } ] })7.Vue路由重定向
匹配path后,强制跳转path路径
import NotFind from '@/views/NotFind' const router new VueRouter({ routes:[ { path:'/'redirect:'/home'}, { path:'/home',component:Home }path:'/search/words?',component:Search}, { path:'*',component:NotFind } ] })