首页 > 编程语言 >python 基础操作 plus

python 基础操作 plus

时间:2022-11-16 15:37:08浏览次数:33  
标签:python reduce print plus import operator 操作 out

  1. 将python列表中的多个列表转化为一个列表:列表的扁平化处理总结

    方法一: 使用 itertools
    # 速度最快
    import itertools
    
    a = [[1, 2, 3, 4], [4, 4, 5, 6], [7], [7, 8, 9]]
    out = list(itertools.chain.from_iterable(a))
    print(out)
    
    output:[1, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 9]

     方法二: 使用 sum() 函数

    a = [[1, 2, 3, 4], [4, 4, 5, 6], [7], [7, 8, 9]]
    out = sum(a, [])
    print(out)
    
    output:[1, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 9]

     方法三:使用operator、reduce函数

    import operator
    from functools import reduce
    
    a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    print(reduce(operator.add, a))
    
    a:[1, 2, 3, 4, 5, 6, 7, 8, 9]

     

     









标签:python,reduce,print,plus,import,operator,操作,out
From: https://www.cnblogs.com/luochunxi/p/16896031.html

相关文章

  • python---三十五期
    昨日内容回顾软件开发架构1.C/S架构 客户端、服务端2.B/S架构 浏览器、服务器B/S本质也是C/S架构网络编程前戏基于网络实现数据交互计算机之间要想实现数据......
  • plus
    目录分页查询分页查询视图层用对象接所有参数(c工程,YqOpinionController.java)点击查看代码@ApiOperation(value="多条件分页查询",notes="<bstyle='color:re......
  • python multiprocessing 多进程
    1获取进程id当我们运行py文件时,该程序的运行就是一个进程,如果在该进程中又创建了其他进程,那么该进程就是主进程,创建的其他进程就是子进程。下面我们通过通过os库中的方法......
  • QT 应用程序执行 Linux 指令,以操作 I2c tools 读写举例
    头文件:#include<string.h>#include<QProcess>#include<QDebug>#include<QString>写数据到I2c:voidwriteData(uint8_tdata){QProcessp;//声明......
  • Python的类和对象
    1.类和对象的定义类用class关键字声明,类中的变量成为属性,函数成为方法,定义方法的时候,需要带上self参数。例:classPerson:#默认继承Object父......
  • 操作数据库-2022-11-16
    1、操作数据库-》2、操作数据库中的表-》3、操作数据库表中的字段MYSQL的关键字不分大小写1、操作数据库(了解)创建数据库,createdatabase[ifnotexist]west02;......
  • c++调用python
    cmakelistscmake_minimum_required(VERSION3.20)project(python_test)set(PYTHON_INCLUDE_DIRS"/home/ubuntu/miniconda3/envs/python38/include/python3.8")INCLUD......
  • windows下开机自启python服务
    一、编辑statrtRunRecService.bat文件@echooffcallactivateD:\applications\Anaconda\envs\py_36_flaskechopwddiris%cd%cd/dD:\work_dwy\AisaBankRecon......
  • 习题整理【Python】
    习题整理【Python】​​前言​​​​Python​​​​Python_1​​​​Python_2​​​​Python_3​​​​Python_4​​​​Python_5​​​​Python_6​​​​Python_7​​​​......
  • 微软开源最强Python自动化神器Playwright!不用写一行代码!自动生成代码还竟然如此流畅!妈
    安装#安装playwright库pipinstallplaywright#安装浏览器驱动文件python-mplaywrightinstall#再安装playwrightinstall要求:python版本3.7+使用Playwright无需写一行......