说明

最近在用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. 【技术博客】Flutter—使用网络请求的页面搭建流程、State生命周期、一些组件的应用

    Flutter-使用网络请求的页面搭建流程.State生命周期.一些组件的应用 使用网络请求的页面搭建流程 ​ 在开发APP时,我们常常会遇到如下场景:进入一个页面后,要先进行网络调用,然后使用调用返 ...

  2. OO2020 助教工作总结

    1 我的初衷 这一学期的OO助教工作是我一段宝贵的经历,在其中我学习了很多.见识了很多,收获满满.当时报名OO的初衷主要有三方面.首先,我想说OO是我所上过的最好的一门课之一,这门课有这一套从理论讲授 ...

  3. 2020BUAA软工个人项目作业

    2020BUAA软工个人项目作业 17373010 杜博玮 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 个人项目作业 我在这个课程的目标是 学 ...

  4. elasticsearch基于RBAC认证和集群之间的TLS通讯

    elasticsearch基于RBAC认证和集群之间的TLS通讯 一.背景 二.需要解决的问题 三.给es增加用户名和密码访问 1.修改config/elasticsearch.yml 2.访问es集 ...

  5. Nginx(二):Nginx的四层(L4)和七层(L7)负载均衡

    OSI七层模型 和 TCP/IP四层模型 四层负载均衡( L4 Load Balancing ) 四层负载均衡,主要通过报文中的目标地址和端口,再加上负载均衡设备设置的服务器选择方式,决定最终选择的内 ...

  6. Jquery取值方法汇总

    一.下拉框 1.jquery获取当前选中select的text值 var a = $("#ShareMoneyType").find("option:selected&q ...

  7. GDI+图形图像技术1

    System.Drawing命名空间提供了对GDI+基本图形功能的访问,其中一些子命名空间中提供了更高级的功能. GDI+由GDI发展而来,是Windows图形显示程序与实际物理设备之间的桥梁. GD ...

  8. hdu 5095 Linearization of the kernel functions in SVM(模拟,分类清楚就行)

    题意: INPUT: The input of the first line is an integer T, which is the number of test data (T<120). ...

  9. DeWeb 与 Unigui的区别

    DeWeb 与 Unigui 相同: 都是采用Delphi开发网页的平台 不同: 1 DeWeb不需要安装控件, 而Unigui需要安装自己的控件 2 DeWeb无需要学习HTML/CSS/JavaS ...

  10. Unmount and run xfs_repair

    参考连接:https://blog.csdn.net/qq_35022803/article/details/109287086 如故障图所示,sda3出现问题, 下面的解决办法: 解决办法: 原因: ...