Hướng dẫn list in quotes python - danh sách trong dấu ngoặc kép python

Bạn cũng có thể thực hiện một cuộc gọi format

Nội phân chính

  • Tham gia một danh sách các chuỗi gói mỗi chuỗi trong trích dẫn trong Python #
  • Làm thế nào để bạn giữ báo giá trong một chuỗi python?
  • Làm thế nào để bạn bọc một chuỗi trong một báo giá trong Python?
  • Làm thế nào để bạn giữ một báo giá kép trong một chuỗi trong Python?
  • Làm cách nào để biến một danh sách thành một chuỗi trong Python?

>>> words = ['hello', 'world', 'you', 'look', 'nice']
>>> '"{0}"'.format('", "'.join(words))
'"hello", "world", "you", "look", "nice"'

Cập nhật: Một số điểm chuẩn (được thực hiện trên MBP 2009):

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195

Vì vậy, có vẻ như format thực sự khá đắt

CẬP NHẬT 2: Theo bình luận của @jcode, thêm

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
1 để đảm bảo rằng
>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
2 sẽ hoạt động, Python 2.7.12

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.08646488189697266

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(map(str, words)))""").timeit(1000)
0.04855608940124512

>>> timeit.Timer("""words = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.17348504066467285

>>> timeit.Timer("""words = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] * 100; '"{}"'.format('", "'.join(map(str, words)))""").timeit(1000)
0.06372308731079102

Tham gia một danh sách các chuỗi gói mỗi chuỗi trong trích dẫn trong Python #

Làm thế nào để bạn giữ báo giá trong một chuỗi python?

  1. Làm thế nào để bạn bọc một chuỗi trong một báo giá trong Python?
  2. Làm thế nào để bạn giữ một báo giá kép trong một chuỗi trong Python?
  3. Làm cách nào để biến một danh sách thành một chuỗi trong Python?

Copied!

my_list = ['one', 'two', 'three'] my_str = ', '.join(f'"{item}"' for item in my_list) print(my_str) # 👉️ '"one", "two", "three"'

Cập nhật: Một số điểm chuẩn (được thực hiện trên MBP 2009):

Copied!

my_list = ['one', 'two', 'three'] print(', '.join(my_list)) # 👉️ 'one, two, three'

Vì vậy, có vẻ như format thực sự khá đắt

Copied!

my_list = ['one', 'two', 'three'] my_str = ' '.join(f'"{item}"' for item in my_list) print(my_str) # 👉️ '"one" "two" "three"'

CẬP NHẬT 2: Theo bình luận của @jcode, thêm

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
1 để đảm bảo rằng
>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
2 sẽ hoạt động, Python 2.7.12

Để tham gia một danh sách các chuỗi gói mỗi chuỗi trong trích dẫn:

Gọi phương thức

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
3 trên dấu phân cách chuỗi.

Copied!

my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # 👉️ is subscribed: True

Chuyển biểu thức máy phát cho phương thức

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
3.

Trên mỗi lần lặp, sử dụng một chuỗi được định dạng để bọc mục trong trích dẫn.

Copied!

my_list = ['one', 'two', 'three'] my_str = ', '.join(f'"{item}"' for item in my_list) print(my_str) # 👉️ '"one", "two", "three"'

Phương thức str.join lấy một điều đáng tin cậy như một đối số và trả về một chuỗi là sự kết hợp của các chuỗi trong điều kiện có thể sử dụng được.

Chuỗi phương thức được gọi là bật được sử dụng làm phân tách giữa các phần tử.

Nếu bạn không cần một dấu phân cách và chỉ muốn tham gia các phần tử của ITerable vào một chuỗi, hãy gọi phương thức

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
3 trên một chuỗi trống.

Copied!

my_list = ['one', 'two', 'three', 4, 5] my_str = ', '.join(f'"{item}"' for item in my_list) print(my_str) # 👉️ '"one", "two", "three", "4", "5"'

Chúng tôi đã sử dụng một chuỗi được định dạng theo nghĩa đen để bọc từng mục danh sách trong các trích dẫn.

Làm thế nào để bạn giữ báo giá trong một chuỗi python?

Làm thế nào để bạn bọc một chuỗi trong một báo giá trong Python?.

Làm thế nào để bạn giữ một báo giá kép trong một chuỗi trong Python?

Làm cách nào để biến một danh sách thành một chuỗi trong Python?

Làm thế nào để bạn bọc một chuỗi trong một báo giá trong Python?

Làm thế nào để bạn giữ một báo giá kép trong một chuỗi trong Python?use single quotation marks inside of double quotation marks or vice versa. For instance: example1 = "He said 'See ya' and closed the door." example2 = 'They said "We will miss you" as he left.

Làm thế nào để bạn giữ một báo giá kép trong một chuỗi trong Python?

Làm cách nào để biến một danh sách thành một chuỗi trong Python?.

Cập nhật: Một số điểm chuẩn (được thực hiện trên MBP 2009):

Vì vậy, có vẻ như format thực sự khá đắt

CẬP NHẬT 2: Theo bình luận của @jcode, thêm

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
1 để đảm bảo rằng
>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; ', '.join('"{0}"'.format(w) for w in words)""").timeit(1000)
0.32559704780578613

>>> timeit.Timer("""words = ['hello', 'world', 'you', 'look', 'nice'] * 100; '"{}"'.format('", "'.join(words))""").timeit(1000)
0.018904924392700195
2 sẽ hoạt động, Python 2.7.12

Để tham gia một danh sách các chuỗi gói mỗi chuỗi trong trích dẫn:

Làm cách nào để biến một danh sách thành một chuỗi trong Python?

Cập nhật: Một số điểm chuẩn (được thực hiện trên MBP 2009):use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.