Comparison of split and join functions
一、关于split 和 join 方法
1只针对字符串进行处理。split:拆分字符串、join连接字符串
2.string.join(sep): 以string作为分割符,将sep中所有的元素(字符串表示)合并成一个新的字符串
3.string.split(str=' ',num=string.count(str)): 以str为分隔,符切片string,如果num有指定值,则仅分隔num个子字符串。
4.对导入os模块进行os.path.splie()/os.path.join() 貌似是处理机制不一样,但是功能上一样。
二、split()方法
help后的信息如下:
split(…)
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator.
中文翻译:
split(…)
S.split([sep [,maxsplit]]) -> 由字符串分割成的列表
返回一组使用分隔符(sep)分割字符串形成的列表。如果指定最大分割数,则在最大分割时结束。如果分隔符未指定或者为none,则分隔符默认为空格。
实例:
s='a b c' print s.split(' ') st='hello world' print st.split('o') print st.split('o',1) --------output--------- ['a', 'b', 'c'] ['hell', ' w', 'rld'] ['hell', ' world'] os.path.split()
os.path.split是按照路径将文件名和路径分割开,比如d:\\python\\python.ext,可分割为['d:\\python', 'python.exe'],示例如下:
import os print os.path.split('c:\\Program File\\123.doc') print os.path.split('c:\\Program File\\') -----------------output--------------------- ('c:\\Program File', '123.doc') ('c:\\Program File', '')
三、join()
a='abcd' print '.'.join(a) print '|'.join(['a','b','c']) #可以把['a','b','c']看做是 a='abcd';下面同理 print '.'.join({'a':1,'b':2,'c':3,'d':4})
注意:'.'等做分隔符,将join里的所有元素(字符串)通过分隔符连接成一个新的字符串
可能有人像我一样咬文嚼字,针对string.join()的定义爱钻牛角尖,硬生生地将['a','b','c']先转换为字符串,然后在join
如:
b=str(['a','b','c'])
print '|'.join(b)
我以为这样是正解,但是不然。输出结果是:[|'|a|'|,| |'|b|'|,| |'|c|'|],而导致与上面不一致的原因就是画蛇添足了,把['a','b','c']转换成了字符串,在Python中,我们发现字符串、元祖、列表它们是序列类型,有着相同的访问方式,可以以下标来访问其中的元素。
以上可以再敲一遍试试。
输入:
输出:
a.b.c.d
a|b|c
a.c.b.d
os.path.join(path1[,path2[,......]])
os.path.join(path1[, path2[, ...]]) #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略。 >>> os.path.join('c:\\', 'csv', 'test.csv') 'c:\\csv\\test.csv' >>> os.path.join('windows\temp', 'c:\\', 'csv', 'test.csv') 'c:\\csv\\test.csv' >>> os.path.join('/home/aa','/home/aa/bb','/home/aa/bb/c') '/home/aa/bb/c'

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.
