Tuple adalah salah satu dari 4 tipe data bawaan di Python yang digunakan untuk menyimpan kumpulan data, 3 lainnya adalah Daftar, Set, dan Kamus, semuanya dengan kualitas dan penggunaan yang berbeda
Tuple adalah koleksi yang dipesan dan tidak dapat diubah
Tuple ditulis dengan tanda kurung bulat
Contoh
Buat Tupel
thistuple = ("apel", "pisang", "ceri")
cetak (initupel)
Item tupel
Item Tuple dipesan, tidak dapat diubah, dan memungkinkan nilai duplikat
Item Tuple diindeks, item pertama memiliki indeks [0], item kedua memiliki indeks [1] dll
Dipesan
Ketika kami mengatakan bahwa tupel dipesan, itu berarti item memiliki pesanan yang ditentukan, dan pesanan itu tidak akan berubah
Tidak dapat diubah
Tuple tidak dapat diubah, artinya kita tidak dapat mengubah, menambah atau menghapus item setelah tupel dibuat
Izinkan Duplikat
Karena tupel diindeks, mereka dapat memiliki item dengan nilai yang sama
Contoh
Tuple memungkinkan nilai duplikat
thistuple = ("apel", "pisang", "ceri", "apel", "ceri")
cetak (initupel)
Belajar Memfilter Data dengan Python Seperti Analis Data
Cobalah sesi pelatihan langsung dengan panduan langkah demi langkah dari seorang ahli. Coba proyek terpandu yang dibuat bekerja sama dengan Coursera sekarang
Get startedPanjang Tupel
Untuk menentukan berapa banyak item yang dimiliki sebuah tuple, gunakan fungsi len()
Contoh
Cetak jumlah item dalam tuple
thistuple = ("apel", "pisang", "ceri")
cetak(len(inituple))
Buat Tuple Dengan Satu Item
Untuk membuat tuple dengan hanya satu item, Anda harus menambahkan koma setelah item tersebut, jika tidak, Python tidak akan mengenalinya sebagai tuple
Contoh
Satu item tuple, ingat koma
thistuple = ("apel",)
cetak(ketik(initupel))
#BUKAN tupel
thistuple = ("apel")
cetak(ketik(initupel))
Item Tuple - Tipe Data
Item Tuple dapat berupa tipe data apa pun
Contoh
Tipe data string, int dan boolean
tuple1 = ("apel", "pisang", "ceri")
tupel2 = (1, 5, 7, 9, 3)
tuple3 = (Benar, Salah, Salah)
Tuple dapat berisi tipe data yang berbeda
Contoh
Tuple dengan string, bilangan bulat, dan nilai boolean
tuple1 = ("abc", 34, Benar, 40, "laki-laki")
Cobalah sendiri "jenis()
Dari perspektif Python, tuple didefinisikan sebagai objek dengan tipe data 'tuple'
Contoh
Apa tipe data tuple?
mytuple = ("apel", "pisang", "ceri")
cetak(ketik(mytuple))
Tuple() Konstruktor
Dimungkinkan juga untuk menggunakan konstruktor tuple() untuk membuat tuple
Contoh
Menggunakan metode tuple() untuk membuat tuple
thistuple = tuple(("apple", "banana", "cherry")) # perhatikan tanda kurung bulat ganda
cetak (initupel)
Koleksi Python (Array)
Ada empat tipe data koleksi dalam bahasa pemrograman Python
- Daftar adalah koleksi yang dipesan dan diubah. Memungkinkan duplikat anggota
- Tuple adalah koleksi yang dipesan dan tidak dapat diubah. Memungkinkan duplikat anggota
- Set adalah koleksi yang tidak diurutkan, tidak dapat diubah*, dan tidak diindeks. Tidak ada anggota rangkap
- Kamus adalah kumpulan yang dipesan** dan dapat diubah. Tidak ada anggota rangkap
*Set item tidak dapat diubah, tetapi Anda dapat menghapus dan/atau menambahkan item kapan pun Anda mau
**Pada Python versi 3. 7, kamus dipesan. Dalam Python 3. 6 dan sebelumnya, kamus tidak diurutkan
Saat memilih tipe koleksi, akan sangat berguna untuk memahami properti dari tipe tersebut. Memilih jenis yang tepat untuk kumpulan data tertentu dapat berarti retensi makna, dan dapat berarti peningkatan efisiensi atau keamanan
#Ini menemukan item berulang dalam tupel. tup= (1,2,3,4,4,5,5,12,12,12,12,1,3,2,4, "saya", "saya", "hai", "hai", " . jika tup. hitung(i)>1. reputasi. tambahkan (i) lain. lanjutkan untuk x dalam pengulangan. sementara perwakilan. hitung(x)>1. reputasi. hapus(x) print("Item berulang. ",tupel(perwakilan))Sering kali, saat bekerja dengan tupel Python, kami mengalami masalah saat menghapus duplikat. Ini adalah masalah yang sangat umum dan dapat terjadi dalam segala bentuk penyiapan pemrograman, baik itu pemrograman biasa atau pengembangan web. Mari kita bahas cara-cara tertentu di mana tugas ini dapat dilakukan.
Metode #1. Menggunakan set() + tuple() Ini adalah cara paling mudah untuk menghapus duplikat. Dalam hal ini, kami mengonversi tuple menjadi satu set, menghapus duplikat, lalu mengubahnya kembali menggunakan tuple().
Python3
# Python3 code to demonstrate working of
# Removing duplicates from tuple
# using tuple() + set()
# initialize tuple
test_tup= The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)0The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)1The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)3The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)5The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)7The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)3The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)5The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)1The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)1The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)3The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)8
The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)_9
The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)0The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)1The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2 The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)3 The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)4The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)5 The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)6The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)7
# Removing duplicates from tuple
# using tuple() + set()
# Python3 code to demonstrate working of0= The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)2The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)00The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)41 The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)02The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)43 The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3) The tuple after removing duplicates : (1, 3, 5, 2)04____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ E jelas tikeranya