说明

最近在用Python的join函数连接多个列表时,出现了如下两个错误,即合并类型不一致。折腾了很久才找到原因,真是基础不牢,地动山摇。

TypeError: sequence item 3: expected str instance, int found



TypeError: can only concatenate list (not "str") to list

数据说明

a = ['a','c','c']
b = ['a','s','e']
c = [4,5,'e']
d = ['f']
e = 'g'
f = 'a'

正确示例

以下运行成功。

print('\t'.join(a+b))
print('\t'.join(a+d))
print('\t'.join(e+f)) # a c c a s e
# a c c f
# g a

错误示例

join函数只能连接同类型的数据,要么都为list,要么都为str等。

同样列表内元素也是如此,必须是同类型的,否则需要转换。

print('\t'.join(a+c))
# TypeError: sequence item 3: expected str instance, int found
print('\t'.join(a+e))
# TypeError: can only concatenate list (not "str") to list

解决办法

格式转换

print('\t'.join(a+[str(i) for i in c]))
print('\t'.join(a+[e])) # a c c 4 5 e
# a c c g

Ref:https://my.oschina.net/u/4307735/blog/3647949

Python中关于join函数的陷阱?的更多相关文章

  1. Python中的join()函数的用法

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下:    join():    连接字符串数组.将字符串.元组.列表中的元素以指定的字 ...

  2. Python中的join()函数split()函数

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下:     join():    连接字符串数组.将字符串.元组.列表中的元素以指定的 ...

  3. Python中的join()函数的用法及列表推导式

    [红色为转载后新增部分] 函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join():连接字符串数组.将字符串.元组.列表中的元 ...

  4. 详解Python中的join()函数的用法

    函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下:     join():    连接字符串数组.将字符串.元组.列表中的元素以指定的 ...

  5. [转载]python中multiprocessing.pool函数介绍

    原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...

  6. python中multiprocessing.pool函数介绍_正在拉磨_新浪博客

    python中multiprocessing.pool函数介绍_正在拉磨_新浪博客     python中multiprocessing.pool函数介绍    (2010-06-10 03:46:5 ...

  7. python --- Python中的callable 函数

    python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...

  8. python中使用zip函数出现<zip object at 0x02A9E418>

    在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...

  9. Python 中的isinstance函数

    解释: Python 中的isinstance函数,isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是cla ...

随机推荐

  1. Vite启动后提示Network: use `--host` to expose

    当使用 Vite 构建项目后,发现只有localhost + 端口 服务,没有 IP + 端口服务. 运行npm run dev,终端提示Vite启动后提示Network: use '--host' ...

  2. 改善深层神经网络-week1编程题(GradientChecking)

    1. Gradient Checking 你被要求搭建一个Deep Learning model来检测欺诈,每当有人付款,你想知道是否该支付可能是欺诈,例如该用户的账户可能已经被黑客掉. 但是,反向传 ...

  3. Beta阶段第四次会议

    Beta阶段第四次会议 时间:2020.5.20 完成工作 姓名 工作 难度 完成度 ltx 1.对小程序进行修改2.提出相关api修改要求 轻 85% xyq 1.设计所需api文档2.编写相关技术 ...

  4. stm32学习笔记之串口通信

    在基础实验成功的基础上,对串口的调试方法进行实践.硬件代码顺利完成之后,对日后调试需要用到的printf重定义进行调试,固定在自己的库函数中. b) 初始化函数定义: void USART_Confi ...

  5. Celery Task(定时任务)及参数

    celery beat 是一个调度器:它以常规的时间间隔开启任务,任务将会在集群中的可用节点上运行. 默认情况下,入口项是从 beat_schedule 设置中获取,但是自定义的存储也可以使用,例如在 ...

  6. 15个问题自查你真的了解java编译优化吗?

    摘要:为什么C++的编译速度会比java慢很多?二者运行程序的速度差异在哪? 了解了java的早期和晚期过程,就能理解这个问题了. 本文分享自华为云社区<你真的了解java编译优化吗?15个问题 ...

  7. Python NameError: name 'unicode' is not defined

    Python2 的unicode 函数在 Python3 中被命名为 str.在 Python3 中使用 ·str 来代替 Python2 中的 unicode.

  8. 痞子衡嵌入式:借助Serial Plot软件测量i.MXRT系列FlexSPI驱动Flash页编程执行时间

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT系列FlexSPI驱动Flash页编程执行时间. 痞子衡之前写过一篇文章 <串行NOR Flash的页编程模式对于量产 ...

  9. 开发笔记----- python3 小甜点

    一.字典内容排序 1.根据 值大小排序,默认reverse=False:从小到大排序,True:从大到小排序.例: >>> dic1 = {'a1':4,'b1':12,'c1':1 ...

  10. Ubuntu16.04安装apache hive

    0.常规配置操作可参照以下网址: 0.1 Ubuntu安装hive,并配置mysql作为元数据库http://dblab.xmu.edu.cn/blog/install-hive/ ------以下为 ...