Hướng dẫn how do i remove all square brackets from a list in python? - làm cách nào để xóa tất cả dấu ngoặc vuông khỏi danh sách trong python?

LIST = ['Python','problem','whatever']
print[LIST]

Khi tôi chạy chương trình này, tôi nhận được

[Python, problem, whatever]

Có thể loại bỏ dấu ngoặc vuông đó khỏi đầu ra không?

Sparkandshine

16.2K21 Huy hiệu vàng87 Huy hiệu bạc130 Huy hiệu đồng21 gold badges87 silver badges130 bronze badges

Đã hỏi ngày 3 tháng 11 năm 2012 lúc 9:09Nov 3, 2012 at 9:09

2

Bạn có thể chuyển đổi nó thành một chuỗi thay vì in trực tiếp danh sách:

print[", ".join[LIST]]

Nếu các phần tử trong danh sách không phải là chuỗi, bạn có thể chuyển đổi chúng thành chuỗi bằng cách sử dụng

[Python, problem, whatever]
0 [nếu bạn muốn báo giá xung quanh chuỗi] hoặc
[Python, problem, whatever]
1 [nếu bạn không], như vậy:

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]

Cho đầu ra:

1, 'foo', 3.5, {'hello': 'bye'}

Đã trả lời ngày 3 tháng 11 năm 2012 lúc 9:11Nov 3, 2012 at 9:11

4

Vâng, có một số cách để làm điều đó. Chẳng hạn, bạn có thể chuyển đổi danh sách thành một chuỗi và sau đó xóa các ký tự đầu tiên và cuối cùng:

l = ['a', 2, 'c']
print str[l][1:-1]
'a', 2, 'c'

Nếu danh sách của bạn chỉ chứa các chuỗi và bạn cũng muốn xóa các trích dẫn thì bạn có thể sử dụng phương thức

[Python, problem, whatever]
2 như đã được nói.

Đã trả lời ngày 3 tháng 11 năm 2012 lúc 9:13Nov 3, 2012 at 9:13

VicentVicentVicent

5.2522 Huy hiệu vàng27 Huy hiệu bạc36 Huy hiệu đồng2 gold badges27 silver badges36 bronze badges

Nếu bạn có số trong danh sách, bạn có thể sử dụng

[Python, problem, whatever]
3 để áp dụng
[Python, problem, whatever]
1 cho mỗi phần tử:

print ', '.join[map[str, LIST]]

^

[Python, problem, whatever]
3 là mã C nên nó nhanh hơn
[Python, problem, whatever]
6

Đã trả lời ngày 3 tháng 11 năm 2012 lúc 9:24Nov 3, 2012 at 9:24

yedpodtrzitkoyedpodtrzitkoyedpodtrzitko

8,5812 Huy hiệu vàng37 Huy hiệu bạc39 Huy hiệu đồng2 gold badges37 silver badges39 bronze badges

def listToStringWithoutBrackets[list1]:
    return str[list1].replace['[',''].replace[']','']

Đã trả lời ngày 7 tháng 2 năm 2014 lúc 11:35Feb 7, 2014 at 11:35

lahjaton_jlahjaton_jlahjaton_j

7956 Huy hiệu bạc13 Huy hiệu Đồng6 silver badges13 bronze badges

Đầu ra: & nbsp;

Phương pháp số 2: Sử dụng các phương thức str [] và thay thế []

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
2
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
3
1, 'foo', 3.5, {'hello': 'bye'}
9
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
5
The original list is : [5, 6, 8, 9, 10, 21]
List after removing square brackets : 5, 6, 8, 9, 10, 21
1

Phương pháp 1: Sử dụng str [] + Danh sách cắt & nbsp;

Nhập giá có thể được áp dụng, mà không cần phải truy cập từng yếu tố của danh sách là chuyển đổi toàn bộ danh sách thành một chuỗi và sau đó loại bỏ ký tự ban đầu và cuối cùng của danh sách bằng cách cắt danh sách. Điều này đã giành được công việc nếu danh sách chứa một chuỗi. Trong trường hợp đó, mỗi phần tử có thể được tham gia bằng tham gia [], như được thảo luận trong nhiều bài viết khác. & NBSP;

Python3

[Python, problem, whatever]
7
[Python, problem, whatever]
8
[Python, problem, whatever]
9
print[", ".join[LIST]]
0
print[", ".join[LIST]]
1
print[", ".join[LIST]]
2221
print[", ".join[LIST]]
4
print[", ".join[LIST]]
1
print[", ".join[LIST]]
6
print[", ".join[LIST]]
1
print[", ".join[LIST]]
8____21
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
03031

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
2
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
3
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
4
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
5
[Python, problem, whatever]
1
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
7

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
8
[Python, problem, whatever]
8
[Python, problem, whatever]
1
1, 'foo', 3.5, {'hello': 'bye'}
1
1, 'foo', 3.5, {'hello': 'bye'}
2
1, 'foo', 3.5, {'hello': 'bye'}
3
1, 'foo', 3.5, {'hello': 'bye'}
4
1, 'foo', 3.5, {'hello': 'bye'}
2
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
1

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
2
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
3
1, 'foo', 3.5, {'hello': 'bye'}
9
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
5
l = ['a', 2, 'c']
print str[l][1:-1]
'a', 2, 'c'
1

Đầu ra: & nbsp;

The original list is : [5, 6, 8, 9, 10, 21]
List after removing square brackets : 5, 6, 8, 9, 10, 21

Phương pháp số 2: Sử dụng các phương thức str [] và thay thế []

Python3

[Python, problem, whatever]
7
[Python, problem, whatever]
8
[Python, problem, whatever]
9
print[", ".join[LIST]]
0
print[", ".join[LIST]]
1
print[", ".join[LIST]]
2221
print[", ".join[LIST]]
4
print[", ".join[LIST]]
1
print[", ".join[LIST]]
6
print[", ".join[LIST]]
1
print[", ".join[LIST]]
8____21
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
03031

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
2
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
3
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
4
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
5
[Python, problem, whatever]
1
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
7

def listToStringWithoutBrackets[list1]:
    return str[list1].replace['[',''].replace[']','']
3
[Python, problem, whatever]
8
[Python, problem, whatever]
1
def listToStringWithoutBrackets[list1]:
    return str[list1].replace['[',''].replace[']','']
6

def listToStringWithoutBrackets[list1]:
    return str[list1].replace['[',''].replace[']','']
3
[Python, problem, whatever]
8
def listToStringWithoutBrackets[list1]:
    return str[list1].replace['[',''].replace[']','']
9
The original list is : [5, 6, 8, 9, 10, 21]
List after removing square brackets : 5, 6, 8, 9, 10, 21
0
The original list is : [5, 6, 8, 9, 10, 21]
List after removing square brackets : 5, 6, 8, 9, 10, 21
1

def listToStringWithoutBrackets[list1]:
    return str[list1].replace['[',''].replace[']','']
3
[Python, problem, whatever]
8
def listToStringWithoutBrackets[list1]:
    return str[list1].replace['[',''].replace[']','']
9
The original list is : [5, 6, 8, 9, 10, 21]
List after removing square brackets : 5, 6, 8, 9, 10, 21
5
The original list is : [5, 6, 8, 9, 10, 21]
List after removing square brackets : 5, 6, 8, 9, 10, 21
1

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
8
[Python, problem, whatever]
8
[Python, problem, whatever]
1
1, 'foo', 3.5, {'hello': 'bye'}
1
1, 'foo', 3.5, {'hello': 'bye'}
2
1, 'foo', 3.5, {'hello': 'bye'}
3
1, 'foo', 3.5, {'hello': 'bye'}
4
1, 'foo', 3.5, {'hello': 'bye'}
2
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
1

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
2
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
3
1, 'foo', 3.5, {'hello': 'bye'}
9
LIST = [1, "foo", 3.5, { "hello": "bye" }]
print[ ", ".join[ repr[e] for e in LIST ] ]
5
l = ['a', 2, 'c']
print str[l][1:-1]
'a', 2, 'c'
1

The original list is : [5, 6, 8, 9, 10, 21]
List after removing square brackets : 5, 6, 8, 9, 10, 21


Bài Viết Liên Quan

Chủ Đề