String split python tetap pertama

Parameter n dapat digunakan untuk membatasi jumlah pemisahan pada pembatas. Output dari split dan rsplit berbeda

>>> s.str.split(n=2)
0                     [this, is, a regular sentence]
1    [https://docs.python.org/3/tutorial/index.html]
2                                                NaN
dtype: object

>>> s.str.rsplit(n=2)
0                     [this is a, regular, sentence]
1    [https://docs.python.org/3/tutorial/index.html]
2                                                NaN
dtype: object
_

Parameter pat dapat digunakan untuk memisahkan karakter lain

>>> s.str.split(pat="/")
0                         [this is a regular sentence]
1    [https:, , docs.python.org, 3, tutorial, index...
2                                                  NaN
dtype: object

Saat menggunakan

>>> s.str.rsplit()
0                   [this, is, a, regular, sentence]
1    [https://docs.python.org/3/tutorial/index.html]
2                                                NaN
dtype: object
_1, elemen pemisahan akan diperluas menjadi kolom terpisah. Jika NaN ada, itu disebarkan ke seluruh kolom selama pemisahan

>>> s.str.split(expand=True)
                                               0     1     2        3         4
0                                           this    is     a  regular  sentence
1  https://docs.python.org/3/tutorial/index.html  None  None     None      None
2                                            NaN   NaN   NaN      NaN       NaN
_

Untuk kasus penggunaan yang sedikit lebih rumit seperti memisahkan nama dokumen html dari url, kombinasi pengaturan parameter dapat digunakan

>>> s.str.rsplit("/", n=1, expand=True)
                                    0           1
0          this is a regular sentence        None
1  https://docs.python.org/3/tutorial  index.html
2                                 NaN         NaN

Ingatlah untuk menghindari karakter khusus saat menggunakan ekspresi reguler secara eksplisit

>>> s = pd.Series(["foo and bar plus baz"])
>>> s.str.split(r"and|plus", expand=True)
    0   1   2
0 foo bar baz
_

Ekspresi reguler dapat digunakan untuk menangani url atau nama file. Ketika pat adalah string dan

>>> s.str.split(n=2)
0                     [this, is, a regular sentence]
1    [https://docs.python.org/3/tutorial/index.html]
2                                                NaN
dtype: object
_0 (default), pat yang diberikan dikompilasi sebagai regex hanya jika
>>> s.str.split(n=2)
0                     [this, is, a regular sentence]
1    [https://docs.python.org/3/tutorial/index.html]
2                                                NaN
dtype: object
1

Pertama, saya akan memperkenalkan Anda pada sintaks dari metode .split(). Setelah itu, Anda akan melihat cara menggunakan metode .split() dengan dan tanpa argumen, menggunakan contoh kode di sepanjang jalan

Inilah yang akan kami bahas

Apa Metode .split() di Python?

Anda menggunakan metode .split() untuk memisahkan string menjadi daftar

Sintaks umum untuk metode .split() terlihat seperti berikut

string.split(separator, maxsplit)
_

Mari kita hancurkan

  • coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # <class 'list'>
    
    
    _9 adalah string yang ingin Anda pisahkan. Ini adalah string tempat Anda memanggil metode .split()
  • Metode .split() menerima dua argumen
  • Argumen opsional pertama adalah
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # <class 'list'>
    
    
    _2, yang menentukan jenis pemisah yang akan digunakan untuk memisahkan string. Jika argumen ini tidak diberikan, nilai defaultnya adalah sembarang spasi, yang berarti string akan terpisah setiap kali .split() bertemu dengan spasi apa pun
  • Argumen opsional kedua adalah
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # <class 'list'>
    
    
    4, yang menentukan jumlah maksimum pemisahan yang harus dilakukan oleh metode .split(). Jika argumen ini tidak diberikan, nilai defaultnya adalah
    coding_journey = "I love   coding"
    
    coding_journey_split = coding_journey.split()
    
    print(coding_journey_split)
    
    # output
    # ['I', 'love', 'coding']
    
    6, artinya tidak ada batasan jumlah pemisahan, dan .split() harus membagi string pada semua kejadian yang ditemuinya
    coding_journey = "I am learning to code for free with freeCodecamp!"
    
    # split string into a list and save result into a new variable
    coding_journey_split = coding_journey.split()
    
    print(coding_journey)
    print(coding_journey_split)
    
    # check the data type of coding_journey_split by using the type() function
    print(type(coding_journey_split))
    
    # output
    # I am learning to code for free with freeCodecamp!
    # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
    # <class 'list'>
    
    
    2

Metode .split()_ mengembalikan daftar substring baru, dan string asli tidak diubah dengan cara apa pun

Bagaimana Metode .split() Bekerja Tanpa Argumen Apa Pun?

Inilah cara Anda membagi string menjadi daftar menggunakan metode .split() tanpa argumen

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

Keluarannya menunjukkan bahwa setiap kata yang menyusun string sekarang menjadi item daftar, dan string asli dipertahankan

Ketika Anda tidak memberikan salah satu dari dua argumen yang diterima oleh metode .split(), maka secara default, itu akan membagi string setiap kali bertemu spasi hingga string berakhir

Apa yang terjadi jika Anda tidak meneruskan argumen apa pun ke metode .split() , dan ia menemukan spasi putih berturut-turut, bukan hanya satu?

coding_journey = "I love   coding"

coding_journey_split = coding_journey.split()

print(coding_journey_split)

# output
# ['I', 'love', 'coding']

Pada contoh di atas, saya menambahkan spasi putih berurutan antara kata

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(".")

print(fave_website_split)

# output
# ['www', 'freecodecamp', 'org']
4 dan kata
fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(".")

print(fave_website_split)

# output
# ['www', 'freecodecamp', 'org']
5. Jika demikian, metode .split() memperlakukan setiap spasi yang berurutan seolah-olah merupakan satu spasi tunggal

Bagaimana Metode .split() Bekerja Dengan Argumen coding_journey = "I am learning to code for free with freeCodecamp!" # split string into a list and save result into a new variable coding_journey_split = coding_journey.split() print(coding_journey) print(coding_journey_split) # check the data type of coding_journey_split by using the type() function print(type(coding_journey_split)) # output # I am learning to code for free with freeCodecamp! # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!'] # <class 'list'> 2?

Seperti yang Anda lihat sebelumnya, ketika tidak ada argumen

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

2, nilai default untuknya adalah spasi putih. Yang mengatakan, Anda dapat mengatur
coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

2 yang berbeda

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

_2 akan memecah dan membagi string setiap kali bertemu dengan karakter yang Anda tentukan dan akan mengembalikan daftar substring

Misalnya, Anda dapat membuatnya sehingga sebuah string terpisah setiap kali metode .split() menemukan titik,

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(". ")

print(fave_website_split)

# output
# ['www.freecodecamp.org']
3

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(".")

print(fave_website_split)

# output
# ['www', 'freecodecamp', 'org']

Dalam contoh di atas, string terpecah setiap kali .split() bertemu dengan

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(". ")

print(fave_website_split)

# output
# ['www.freecodecamp.org']
3

Perlu diingat bahwa saya tidak menentukan titik diikuti dengan spasi. Itu tidak akan berhasil karena string tidak berisi titik diikuti dengan spasi

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(". ")

print(fave_website_split)

# output
# ['www.freecodecamp.org']

Sekarang, mari kita lihat kembali contoh terakhir dari bagian sebelumnya

Ketika tidak ada

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

_2 argumen, spasi putih berurutan diperlakukan seolah-olah itu adalah spasi tunggal

Namun, saat Anda menetapkan satu spasi sebagai

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

2, maka string akan terpisah setiap kali bertemu dengan satu karakter spasi

coding_journey = "I love   coding"

coding_journey_split = coding_journey.split(" ")

print(coding_journey_split)

# output
# ['I', 'love', '', '', 'coding']

Dalam contoh di atas, setiap kali .split() menemui karakter spasi, karakter tersebut membagi kata dan menambahkan spasi kosong sebagai item daftar

Bagaimana Metode .split() Bekerja Dengan Argumen coding_journey = "I am learning to code for free with freeCodecamp!" # split string into a list and save result into a new variable coding_journey_split = coding_journey.split() print(coding_journey) print(coding_journey_split) # check the data type of coding_journey_split by using the type() function print(type(coding_journey_split)) # output # I am learning to code for free with freeCodecamp! # ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!'] # <class 'list'> 4?

Ketika tidak ada

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

_4 argumen, tidak ada batasan yang ditentukan kapan pemisahan harus berhenti

Pada contoh pertama dari bagian sebelumnya, .split() memisahkan string setiap kali bertemu dengan

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

2 hingga mencapai akhir string

Namun, Anda dapat menentukan kapan Anda ingin pemisahan berakhir

Misalnya, Anda dapat menentukan bahwa pemisahan berakhir setelah menemukan satu titik

fave_website = "www.freecodecamp.org"

fave_website_split = fave_website.split(".", 1)

print(fave_website_split)

# output
# ['www', 'freecodecamp.org']

Pada contoh di atas, saya menyetel

coding_journey = "I am learning to code for free with freeCodecamp!"

# split string into a list and save result into a new variable
coding_journey_split = coding_journey.split()

print(coding_journey)
print(coding_journey_split)

# check the data type of coding_journey_split by using the type() function
print(type(coding_journey_split))

# output
# I am learning to code for free with freeCodecamp!
# ['I', 'am', 'learning', 'to', 'code', 'for', 'free', 'with', 'freeCodecamp!']
# <class 'list'>

_4 menjadi
coding_journey = "I love   coding"

coding_journey_split = coding_journey.split(" ")

print(coding_journey_split)

# output
# ['I', 'love', '', '', 'coding']
5, dan daftar dibuat dengan dua item daftar

Saya menetapkan bahwa daftar harus dipisahkan ketika menemukan satu titik. Setelah menemukan satu titik, operasi akan berakhir, dan string lainnya akan menjadi item daftar dengan sendirinya

Kesimpulan

Dan begitulah. Anda sekarang tahu cara membagi string dengan Python menggunakan metode .split()

Saya harap Anda menemukan tutorial ini bermanfaat

Untuk mempelajari lebih lanjut tentang bahasa pemrograman Python, lihat sertifikasi Python freeCodeCamp

Anda akan mulai dari dasar dan belajar dengan cara yang interaktif dan ramah bagi pemula. Anda juga akan membangun lima proyek pada akhirnya untuk dipraktikkan dan membantu memperkuat apa yang telah Anda pelajari

Terima kasih telah membaca, dan selamat coding

IKLAN

IKLAN

IKLAN

IKLAN


String split python tetap pertama
Dionysia Lemonaki

Mempelajari sesuatu yang baru setiap hari dan menulis tentangnya


Jika artikel ini bermanfaat, tweetlah

Belajar kode secara gratis. Kurikulum open source freeCodeCamp telah membantu lebih dari 40.000 orang mendapatkan pekerjaan sebagai pengembang. Memulai

Apakah Python split menjaga ketertiban?

Ya,. split() selalu mempertahankan urutan karakter dalam string .

Bagaimana Anda membagi string dengan kemunculan pertama di Python?

Pendekatan. Karena kita harus memisahkan string hanya sekali pada kemunculan pertama dari pemisah yang diberikan, kita dapat menggunakan argumen maxsplit untuk menyelesaikan masalah yang diberikan dengan menyetel maxsplit = 1 . Therefore, the string will split along the specified separator only once from the left end.

Bagaimana Anda membagi string dan mengambil bagian terakhir dengan Python?

Misalnya, koma(,) . Python menyediakan metode yang membagi string dari ujung belakang string. Fungsi Python bawaan rsplit() yang membagi string pada kejadian terakhir dari pembatas. Dalam rsplit() fungsi 1 diteruskan dengan argumen sehingga string hanya diputus dengan mengambil satu pembatas dari yang terakhir.

Bagaimana Anda membagi string dan menyimpannya?

Metode split() membagi string menjadi array substring . Metode split() mengembalikan array baru. Metode split() tidak mengubah string asli. Jika (" ") digunakan sebagai pemisah, string dipisahkan di antara kata-kata.