Hướng dẫn how to get comma separated values in python - cách lấy các giá trị được phân tách bằng dấu phẩy trong python

Bạn có thể sử dụng phương pháp str.split.

>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split[","]
>>> print my_list
['A', 'B', 'C', 'D', 'E']

Nếu bạn muốn chuyển đổi nó thành một tuple, chỉ

>>> print tuple[my_list]
['A', 'B', 'C', 'D', 'E']

Nếu bạn đang muốn nối vào danh sách, hãy thử điều này:

>>> my_list.append['F']
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    • Bàn luận
    • Cho một chuỗi đầu vào được phân tách bằng dấu phẩy thay vì không gian. Nhiệm vụ là lưu trữ chuỗi đầu vào này trong một danh sách hoặc biến. Điều này có thể đạt được trong Python bằng hai cách:

    Sử dụng danh sách hiểu và

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2 Using List comprehension and
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2

    Sử dụng

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    3 và
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2

    Example:

    Phương pháp 1: Sử dụng danh sách hiểu và

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    2
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    4
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    5
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    6
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    7

    Hàm

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2 giúp nhận được nhiều đầu vào từ người dùng. Nó phá vỡ đầu vào đã cho bởi bộ phân cách được chỉ định. Nếu bộ phân cách không được cung cấp thì bất kỳ không gian trắng nào được sử dụng làm dấu phân cách. Nói chung, người dùng sử dụng phương thức
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2 để phân chia chuỗi Python nhưng người ta cũng có thể sử dụng nó để thực hiện nhiều đầu vào.

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    2
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    # split string s by comma
    s.split[","]
    4
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    5
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    6
    # split string s by comma
    s.split[","]
    7

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    8
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    9
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    0
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    1
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    2
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    3

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    2
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    ['Yes', ' they backed Steve', ' Emma', ' and Rohan in the meeting.']
    4
    ['Yes', ' they backed Steve', ' Emma', ' and Rohan in the meeting.']
    5

    Output:

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    8
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    9
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    0
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    1
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    2
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    3
    Using
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    3 and
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2

    # split string s by comma
    s.split[","]
    8______19

    Phương pháp 2: Sử dụng

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    3 và
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    2
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    4
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    5
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    6
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    7

    Hàm

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    3 Trả về danh sách các kết quả sau khi áp dụng chức năng đã cho cho từng mục của một thứ nhất định [danh sách, tuple, v.v.]

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    2
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    # split string s by comma
    s.split[","]
    4
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    5
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    6
    # split string s by comma
    s.split[","]
    7

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    8
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    9
    # string with commas
    s = "Yes, they backed Steve, Emma, and Rohan in the meeting."
    # split string by comma
    ls = s.split[",", maxsplit=1]
    print[ls]
    1
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    1
    # string with commas
    s = "Yes, they backed Steve, Emma, and Rohan in the meeting."
    # split string by comma
    ls = s.split[",", maxsplit=1]
    print[ls]
    4
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    6
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    8
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    9
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    030

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    8
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    9
    # string with commas
    s = "Yes, they backed Steve, Emma, and Rohan in the meeting."
    # split string by comma
    ls = s.split[",", maxsplit=1]
    print[ls]
    1
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    1
    # string with commas
    s = "Yes, they backed Steve, Emma, and Rohan in the meeting."
    # split string by comma
    ls = s.split[",", maxsplit=1]
    print[ls]
    4
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    6
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    7
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 2, 3, 4
    
    The values of input are [1, 2, 3, 4]
    
    8
    >>> my_list.append['F']
    >>> print my_list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    9
    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 22, 34, 6, 88, 2
    
    The values of input are [1, 22, 34, 6, 88, 2]
    
    030

    Output:

    Enter two values
    1, 2
    
    The value of a is 1 and b is 2
    Enter three values
    1, 2, 3
    
    The value of a is 1, b is 2 and c is 3
    Enter multiple values
    1, 2, 3, 4
    
    The values of input are [1, 2, 3, 4]
    


    Trong hướng dẫn này, chúng tôi sẽ xem xét cách chia một chuỗi thành một danh sách các chuỗi về sự xuất hiện của dấu phẩy trong Python với sự trợ giúp của các ví dụ.

    Làm thế nào để chia một chuỗi trong Python?

    Bạn có thể sử dụng hàm chuỗi Python

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2 để chia chuỗi [bằng dấu phân cách] thành danh sách các chuỗi. Để phân chia một chuỗi bằng dấu phẩy trong Python, hãy truyền ký tự dấu phẩy
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    34 như một dấu phân cách cho hàm
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2.

    Sau đây là cú pháp -

    # split string s by comma
    s.split[","]

    Nó trả về một danh sách các chuỗi do phân tách chuỗi ban đầu về các lần xuất hiện của

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    34.

    Hãy cùng nhìn vào một số ví dụ.

    Chia chuỗi chia theo dấu phẩy

    Tại đây, chúng tôi vượt qua dấu phẩy

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    34 làm dấu phân cách cho hàm
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2.

    # string with commas
    s = "Yes, they backed Steve, Emma, and Rohan in the meeting."
    # split string by comma
    ls = s.split[","]
    print[ls]

    Output:

    ['Yes', ' they backed Steve', ' Emma', ' and Rohan in the meeting.']

    Danh sách kết quả chứa các từ kết quả từ sự phân chia chuỗi ban đầu về sự xuất hiện của dấu phẩy.

    Sửa số lượng phân tách

    Bạn cũng có thể chỉ định số lượng phân tách tối đa được thực hiện bằng tham số

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    39. Theo mặc định, chức năng chuỗi
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    2 làm cho tất cả các chia tách có thể.

    Hãy để chỉ chia chuỗi ở trên thành hai phần khi xảy ra dấu phẩy,

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    34 khởi động từ bên trái. Để chia chuỗi thành hai phần,
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    39 phải là
    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    43, bởi vì chúng tôi chỉ tạo ra một phần phân chia dẫn đến hai chuỗi.

    # string with commas
    s = "Yes, they backed Steve, Emma, and Rohan in the meeting."
    # split string by comma
    ls = s.split[",", maxsplit=1]
    print[ls]

    Output:

    ['Yes', ' they backed Steve, Emma, and Rohan in the meeting.']

    Bạn có thể thấy rằng danh sách kết quả chỉ có hai chuỗi.

    Hãy cùng nhìn vào một ví dụ khác.

    Hãy để phân chia chuỗi ban đầu thành ba phần, ở đây chúng tôi vượt qua

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    44.

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    0

    Output:

    >>> print tuple[my_list]
    ['A', 'B', 'C', 'D', 'E']
    
    1

    Danh sách kết quả chỉ có ba chuỗi.

    Bạn cũng có thể quan tâm đến -

    1. Python - Chuỗi phân chia theo dấu gạch dưới
    2. Python - Xóa nhiều khoảng trống khỏi chuỗi
    3. Xóa các dòng khỏi chuỗi trong Python


    Đăng ký nhận bản tin của chúng tôi để biết thêm hướng dẫn và hướng dẫn thông tin. Chúng tôi không spam và bạn có thể từ chối bất cứ lúc nào.
    We do not spam and you can opt out any time.

    • Piyush là một nhà khoa học dữ liệu đam mê sử dụng dữ liệu để hiểu mọi thứ tốt hơn và đưa ra quyết định sáng suốt. Trong quá khứ, anh ta làm việc như một nhà khoa học dữ liệu cho ZS và có bằng kỹ sư từ IIT Roorkee. Sở thích của anh ấy bao gồm xem cricket, đọc sách và làm việc trên các dự án phụ.

      Xem tất cả các bài viết

    Làm thế nào để bạn trích xuất dấu phẩy các giá trị tách biệt trong Python?

    Sử dụng str.split [] để chuyển đổi chuỗi được phân tách bằng dấu phẩy thành danh sách.Gọi str.Chia [SEP] với "", như SEP để chuyển đổi chuỗi được phân tách bằng dấu phẩy thành một danh sách. split[] to convert a comma-separated string to a list. Call str. split[sep] with "," as sep to convert a comma-separated string into a list.

    Làm thế nào để bạn làm một dấu phẩy

    Làm thế nào để chuyển đổi danh sách Python thành một chuỗi được phân tách bằng dấu phẩy?Bạn có thể sử dụng phương thức chuỗi .Join để chuyển đổi danh sách thành một chuỗi.Vì vậy, một lần nữa, cú pháp là [SEPERATOR].use the . join string method to convert a list into a string. So again, the syntax is [seperator].

    Làm thế nào để bạn tách dấu phẩy

    Sử dụng phương thức str.Split [] để phân chia chuỗi trên mỗi lần xuất hiện của dấu phẩy. split[] method to split the string on each occurrence of a comma.

    Làm thế nào để bạn in dấu phẩy trong Python?

    Khái niệm: Định dạng dấu phẩy Python cung cấp một số phương thức định dạng chuỗi trong hàm in [] ngoài việc bổ sung chuỗi.Print [] cung cấp sử dụng dấu phẩy để kết hợp các chuỗi cho đầu ra.Bằng các chuỗi phân tách dấu phẩy, print [] sẽ xuất ra từng chuỗi được phân tách theo một không gian theo mặc định.print[] provides using commas to combine strings for output. By comma-separating strings, print[] will output each separated by a space by default.

    Bài Viết Liên Quan

    Chủ Đề