python列表连接

Python join list means concatenating a list of strings with a specified delimiter to form a string. Sometimes it’s useful when you have to convert list to string. For example, convert a list of alphabets to a comma-separated string to save in a file.

Python连接列表意味着将字符串列表与指定的分隔符连接起来以形成字符串。 有时在必须将列表转换为字符串时很有用。 例如,将字母列表转换为逗号分隔的字符串以保存在文件中。

Python连接列表 (Python Join List)

We can use python string join() function to join a list of strings. This function takes iterable as argument and List is an interable, so we can use it with List.

我们可以使用python string join()函数来连接字符串列表。 该函数将iterable作为参数,并且List是一个可互操作的,因此我们可以将其与List一起使用。

Also, the list should contain strings, if you will try to join a list of ints then you will get an error message as TypeError: sequence item 0: expected str instance, int found.

另外,该列表应包含字符串,如果您尝试加入一个ints列表,则将收到错误消息,类型错误TypeError: sequence item 0: expected str instance, int found

Let’s look at a short example for joining list in python to create a string.

让我们看一个简短的示例,在python中加入list来创建一个字符串。

vowels = ["a", "e", "i", "o", "u"]vowelsCSV = ",".join(vowels)print("Vowels are = ", vowelsCSV)

When we run the above program, it will produce the following output.

当我们运行上面的程序时,它将产生以下输出。

Vowels are =  a,e,i,o,u

Python连接两个字符串 (Python join two strings)

We can use join() function to join two strings too.

我们也可以使用join()函数来连接两个字符串。

message = "Hello ".join("World")print(message) #prints 'Hello World'

为什么join()函数在String中而不在List中? (Why join() function is in String and not in List?)

One question arises with many python developers is why the join() function is part of String and not list. Wouldn’t below syntax be more easy to remember and use?

许多python开发人员都会想到一个问题,为什么join()函数是String的一部分而不是list的一部分。 下面的语法难道不容易记住和使用吗?

vowelsCSV = vowels.join(",")

There is a popular StackOverflow question around this, here I am listing the most important points from the discussions that makes total sense to me.

围绕此有一个流行的StackOverflow问题 ,在这里,我列出了讨论中最有意义的要点。

join() function can be used with any join()函数可以与任何iterable and result is always a String, so it makes sense to have this function in String API rather than having it in all the iterable classes.可迭代对象一起使用,并且结果始终是String,因此在String API中使用此函数而不是在所有可迭代类中使用它是有意义的。

多种数据类型的联接列表 (Joining list of multiple data-types)

Let’s look at a program where we will try to join list items having multiple data types.

让我们看一个程序,在该程序中,我们将尝试加入具有多种数据类型的列表项。

names = ['Java', 'Python', 1]
delimiter = ','
single_str = delimiter.join(names)
print('String: {0}'.format(single_str))

Let’s see the output for this program:

让我们看一下该程序的输出:

This was just a demonstration that a list which contains multiple data-types cannot be combined into a single String with join() function. List must contain only the String values.

这只是一个演示,表明无法使用join()函数将包含多个数据类型的列表组合为单个String列表必须仅包含String值

使用连接函数分割字符串 (Split String using join function)

We can use join() function to split a string with specified delimiter too.

我们也可以使用join()函数来使用指定的分隔符分割字符串。

names = 'Python'
delimiter = ','
single_str = delimiter.join(names)
print('String: {0}'.format(single_str))

This shows that when String is passed as an argument to join() function, it splits it by character and with the specified delimiter.

这表明,当将String作为参数传递给join()函数时,它将按字符并使用指定的分隔符对其进行拆分。

使用split()函数 (Using split() function)

Apart from splitting with the join() function, split() function can be used to split a String as well which works almost the same way as the join() function. Let’s look at a code snippet:

除了使用join()函数进行split()split()函数还可用于拆分String,其工作方式与join()函数几乎相同。 让我们看一下代码片段:

names = ['Java', 'Python', 'Go']
delimiter = ','
single_str = delimiter.join(names)
print('String: {0}'.format(single_str))split = single_str.split(delimiter)
print('List: {0}'.format(split))

Let’s see the output for this program:

We used the same delimiter to split the String again to back to the original list.

让我们看一下该程序的输出:

我们使用相同的定界符再次将String拆分为原始列表。

仅拆分n次 (Splitting only n times)

The split() function we demonstrated in the last example also takes an optional second argument which signifies the number of times the splot operation should be performed. Here is a sample program to demonstrate its usage:

我们在上一个示例中演示的split()函数还采用了可选的第二个参数,该参数表示应执行splot操作的次数。 这是一个示例程序来演示其用法:

names = ['Java', 'Python', 'Go']
delimiter = ','
single_str = delimiter.join(names)
print('String: {0}'.format(single_str))split = single_str.split(delimiter, 1)
print('List: {0}'.format(split))

Let’s see the output for this program:

This time, split operation was performed only one time as we provided in the split() function parameter.

让我们看一下该程序的输出:

这次,我们仅在split()函数参数中提供了一次拆分操作。

That’s all for joining a list to create a string in python and using split() function to get the original list again.

这就是为了加入列表以在python中创建字符串并使用split()函数再次获取原始列表而已。

翻译自: https://www.journaldev.com/18626/python-join-list

python列表连接

python列表连接_Python连接列表相关推荐

  1. python列表反向_python反向列表

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 我正在尝试根据最高值创建某种记分牌. 我添加了reverse = true标志但 ...

  2. python元组和列表字典_Python【列表 字典 元组】

    列表 列表用中括号[ ]把各种数据框起来,每一个数据叫作"元素". 每个元素之间都要用英文逗号隔开 各种类型的数据(整数/浮点数/字符串) -------------------- ...

  3. python占位符用元组列表填充_python字符串列表元组序列操作

    Table of Contents generated with DocToc python系列-字符串.列表.元组的操作 序列的访问及运算符 序列是为满足程序中复杂的数据表示,python支持组合数 ...

  4. python怎么删除列表创建_Python基础--列表创建访问删除

    创建列表 在 Python 中,创建列表的方法可分为两种,下面分别进行介绍. 1 使用 [ ] 直接创建列表 # 使用[ ]创建列表后,一般使用=将它赋值给某个变量,具体格式如下: # listnam ...

  5. 初学者对python的认识_Python初学者列表,python,初识

    1.认识列表 列表可以放入所有我们目前学习过的数据类型,甚至包括列表 2.有关列表的方法.内置函数(设列表的名称为list) 向列表中添加元素: append():list.append(要添加的元素 ...

  6. python拆分列表元素_Python将列表拆分为一个元素 - python

    我正在使用Python分析数据,并且有N个2d数据数组的列表.我想一一查看这些元素,并将它们与其他N-1个元素的平均值进行比较. Python中是否有内置方法可循环遍历列表,一方面具有单个元素,另一方 ...

  7. python列表常用方法_python之 列表常用方法

    更多列表的使用方法和API,请参考Python文档:http://docs.python.org/2/library/functions.html append:用于在列表末尾追加新对象: # app ...

  8. python连接数据库步骤_Python连接mysql数据库

    Python中连接MySQL的库主要有三个,Python-MySQL,PyMySQL和SQLAlchemy,其中Python-MySQL已经停止更新,且只支持Python2,目前使用最广泛的是PyMy ...

  9. python导出结果_Python连接Oracle数据查询导出结果

    python连接oracle,需用用到模块cx_oracle,可以直接pip安装,如网络不好,可下载离线后本地安装 本人由于工作需要,期望便捷查询所得结果,且固定输出某个格式 具体代码如下: #! c ...

最新文章

  1. Deep Learning基础理论--Classification RBM
  2. 成功解决ImportError: cannot import name 'PILLOW_VERSION'
  3. windows 下anaconda创建环境慢的解决办法
  4. php 子网掩码正则,验证子网掩码正则表达式代码范例
  5. android box2d运行小例子,配置eclipse【转】
  6. 國慶和中秋的學習成果
  7. pcm转换在线工具_有木有好用的CAD格式转换工具可以推荐?在线等,挺急的
  8. 设置时区,时间,日期的脚本
  9. CentOS系统优化
  10. React Router 全部
  11. Photoshop CC(2018)安装破解
  12. c语言一个字符串怎么做除法,c语言实数除法怎样保留小数部分
  13. pta mysql训练题集 (221-240)
  14. 加来道雄 基因编辑 纳米机器人_潜力巨大:纳米材料结合CRISPR基因编辑,成功改善阿尔茨海默病症状...
  15. Pytorch Tutorial 学习笔记(六)模型部署
  16. Android 仿照美团城市选择,微信小程序仿美团城市选择
  17. Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same
  18. 十个鲜为人知的 Linux 命令 - Part 3
  19. luci与页面的数据交互
  20. 【区块链】区块链是什么?20问:读懂区块链

热门文章

  1. R - 变化plot字形,嵌入字体以pdf
  2. [LeetCode][JavaScript]Palindrome Linked List
  3. vs2010上opencv配置
  4. Arrays.asList 方法注意事项
  5. [转载] python2.7中模块学习- textwrap 文本包装和填充
  6. [转载] python接口测试:封装get和post请求+重新封装requests类
  7. Selector提取数据1:XPath选择器
  8. 步骤1:mybatis工程的创建
  9. loadrunner四大部分
  10. 两百个jQuery插件集合