跳转至

join详解

在 Python 中,join 的作用有两种主要场景:


1. 字符串的 join() 方法

作用:

将一个可迭代对象(如列表、元组等)中的元素,用指定的字符串连接起来,返回一个新的字符串。

语法:

"分隔符".join(可迭代对象)

参数说明:

  • 分隔符:用于连接的字符串。
  • 可迭代对象:包含要连接的字符串元素的对象(如列表、元组、生成器等)。

返回值:

返回一个用分隔符连接的字符串。


示例:

  1. 列表连接为字符串:

    words = ["Python", "is", "fun"]
    result = " ".join(words)  # 用空格连接
    print(result)  # 输出:Python is fun

  2. 用逗号分隔:

    items = ["apple", "banana", "cherry"]
    result = ", ".join(items)
    print(result)  # 输出:apple, banana, cherry

  3. 连接元组:

    items = ("hello", "world")
    result = "-".join(items)  # 用 "-" 连接
    print(result)  # 输出:hello-world

  4. 生成器连接:

    result = ", ".join(str(i) for i in range(5))
    print(result)  # 输出:0, 1, 2, 3, 4

  5. 注意点(连接的元素必须是字符串): 如果可迭代对象中包含非字符串类型,会抛出 TypeError

    nums = [1, 2, 3]
    result = "-".join(map(str, nums))  # 需要先将数字转为字符串
    print(result)  # 输出:1-2-3


2. 线程的 join() 方法

作用:

在线程编程中,join() 用于阻塞主线程,直到目标线程执行完毕。

所属模块:

threading.Thread 类。

语法:

线程对象.join(timeout=None)

参数说明:

  • timeout(可选): 阻塞的最长时间(单位:秒),如果省略或为 None,主线程将等待直到目标线程结束。

返回值:

无返回值。


示例:

  1. 等待线程完成:
    import threading
    import time
    
    def worker():
        print("Worker thread is starting...")
        time.sleep(2)
        print("Worker thread is finished.")
    
    thread = threading.Thread(target=worker)
    thread.start()
    print("Main thread is waiting for the worker thread...")
    thread.join()  # 等待子线程完成
    print("Main thread is resuming.")

输出:

Worker thread is starting...
Main thread is waiting for the worker thread...
Worker thread is finished.
Main thread is resuming.

  1. 使用超时参数:
    def task():
        time.sleep(5)
        print("Task is done.")
    
    thread = threading.Thread(target=task)
    thread.start()
    
    print("Main thread is waiting for 2 seconds...")
    thread.join(timeout=2)  # 主线程等待2秒后继续执行
    print("Main thread is resuming (even if task is not finished).")

输出:

Main thread is waiting for 2 seconds...
Main thread is resuming (even if task is not finished).
Task is done.


对比 join() 的两种用途

功能类型 描述
字符串 join 将多个字符串连接为一个新字符串,便于格式化输出或构建长字符串。
线程 join 阻塞主线程,直到目标线程完成或超时,用于确保多线程程序的执行顺序和同步。

关键区别:

  • str.join() 是字符串的方法,用于操作字符串。
  • threading.Thread.join() 是线程的方法,用于线程同步。