Hướng dẫn what is the need of collections in python? - nhu cầu của các bộ sưu tập trong python là gì?

Bộ sưu tập là một mô-đun Python tích hợp cung cấp các kiểu dữ liệu container hữu ích. Các kiểu dữ liệu container cho phép chúng tôi lưu trữ và truy cập các giá trị một cách thuận tiện. Nói chung, bạn sẽ có danh sách, bộ dữ liệu và từ điển. Nhưng, trong khi xử lý dữ liệu có cấu trúc, chúng tôi cần các đối tượng thông minh hơn.

Trong bài viết này, tôi sẽ hướng dẫn bạn qua các cấu trúc dữ liệu khác nhau được hỗ trợ bởi mô -đun


#> 250
9, hiểu khi nào nên sử dụng chúng với các ví dụ.

Nội dung

  1. Được đặt tên
    • Những gì được đặt tên
    • Một cách khác để tạo ra một danh tính
    • Tại sao sử dụng được đặt tên trên từ điển
    • Tạo một cái tên được đặt tên từ một từ điển Python
    • Cách thay thế một thuộc tính trong một tên gọi là
  2. Phản đối
  3. DefaultDict
  4. Đặt hàng
    • Điều gì xảy ra khi bạn xóa và kiểm soát lại các khóa để đặt hàng
    • Sắp xếp với OrderedDict
  5. Chainmap
    • Điều gì xảy ra khi chúng ta có các phím dự phòng trong một chuỗi
    • Cách thêm từ điển mới vào Chainmap
    • Cách đảo ngược thứ tự từ điển trong Chainmap
  6. Danh sách người dùng
  7. Người dùng
  8. Người dùng
# Import the collections module
import collections

Hãy để chúng tôi bắt đầu với

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0

Cái gì được đặt tênTuple ()

Bạn có thể nghĩ về

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0 theo hai cách:

Là một phiên bản nâng cao của tuple. Hoặc như một cách nhanh chóng để tạo một Python

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
2 với một số thuộc tính được đặt tên.

Một sự khác biệt chính giữa

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
3 và
# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0 là: Trong khi
# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
3 hãy để bạn truy cập dữ liệu thông qua các chỉ số, với
# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0, bạn có thể truy cập các phần tử bằng tên của họ.

Bạn thực sự có thể xác định tất cả các thuộc tính mà

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0 có thể giữ và tạo nhiều trường hợp của nó. Giống như cách bạn sẽ làm với các lớp học.

Vì vậy, về mặt chức năng, nó tương tự như

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
2, mặc dù nó có
# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
3 trong tên của nó.

Hãy để tạo ra một

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0 đại diện cho một ‘phim, với các thuộc tính‘ thể loại, ‘xếp hạng và‘ chì_actor.

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')

Bây giờ, bạn có thể truy cập bất kỳ chi tiết nào của một bộ phim bạn muốn sử dụng định danh. Nó khá thuận tiện và thân thiện với người dùng.

# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)

#> romance
#> Brad Pitt
#> 8.5

Một cách khác để tạo ra
# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0

Thay phiên, bạn có thể vượt qua một danh sách các tên trường thay vì các tên được nộp được phân tách bằng một không gian.

Hãy cho chúng tôi xem một ví dụ.

# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)

#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING

Các mục trong

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0 có thể được truy cập bởi cả hai chỉ mục cũng như một định danh.

print(tale[1])

#> 250

Tại sao sử dụng được đặt tên trên từ điển

Tạo một cái tên được đặt tên từ một từ điển Python

Cách thay thế một thuộc tính trong một tên gọi là

Phản đối

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))

#> Size or space occupied by dictionary 240
#> Size or space occupied by namedtuple 64

DefaultDict

Đặt hàng

Điều gì xảy ra khi bạn xóa và kiểm soát lại các khóa để đặt hàng

Sắp xếp với OrderedDict

Chainmap

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
0
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
1

Cách thay thế một thuộc tính trong một tên gọi là

Phản đối

DefaultDict

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
2
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
3

Phản đối

DefaultDict

Đặt hàng

Điều gì xảy ra khi bạn xóa và kiểm soát lại các khóa để đặt hàng

Sắp xếp với OrderedDict

Chainmap

Điều gì xảy ra khi chúng ta có các phím dự phòng trong một chuỗi

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
4
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
5

Cách thêm từ điển mới vào Chainmap

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
6
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
7

Cách đảo ngược thứ tự từ điển trong Chainmap

Danh sách người dùng

Người dùng

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
8
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
9

Người dùng

Hãy để chúng tôi bắt đầu với

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0

Cái gì được đặt tênTuple ()

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
08

Bạn có thể nghĩ về

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
0 theo hai cách:

# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
0
# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
1

Là một phiên bản nâng cao của tuple. Hoặc như một cách nhanh chóng để tạo một Python

# Create a dict and namedtuple with same data and compare the size
import random
import sys

# Create Dict
dicts = {'numbers_1': random.randint(0, 10000),'numbers_2':random.randint(5000,10000)} 
print('Size or space occupied by dictionary',sys.getsizeof(dicts))

# converting same dictionary to a namedtuple
data=namedtuple('data',['numbers_1','numbers_2'])
my_namedtuple= data(**dicts)
print('Size or space occupied by namedtuple',sys.getsizeof(my_namedtuple))
2 với một số thuộc tính được đặt tên.

# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
2
# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
3

Chúng ta có thể sử dụng cùng để tìm thấy ký tự lặp đi lặp lại nhất trong một chuỗi.

# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
4
# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
5

Điều gì xảy ra nếu bạn không chỉ định ’n, trong khi sử dụng

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
10?

Tất cả các yếu tố là số lượng của chúng sẽ được in theo thứ tự giảm dần số lượng của họ.

# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
6
# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
7

Phương thức

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
11 Trả về tất cả các yếu tố có số lượng lớn hơn 0.

# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
8
# Access the fields
print(titanic.genre)
print(seven.lead_actor)
print(ironman.rating)
9

DefaultDict

Từ điển là một bộ sưu tập các khóa và giá trị không được đặt hàng.

Trong khóa: các cặp giá trị, khóa phải khác biệt và không thể thay đổi. Đó là lý do tại sao trong một từ điển, một danh sách không thể là một chìa khóa, vì nó có thể thay đổi. Nhưng, một tuple có thể là một chìa khóa.


#> romance
#> Brad Pitt
#> 8.5
0

RefaultDict khác với Dictict như thế nào

Nếu bạn cố gắng truy cập một khóa không có trong từ điển, nó sẽ ném

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
12. Trong khi đó, trong một
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
13, nó không cho
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
12.

Nó không cho một KeyError. Nếu bạn truy cập khóa không có,

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
13 sẽ trả về giá trị mặc định.

Cú pháp:

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
16

Khi chúng tôi truy cập khóa không có mặt, hàm

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
17 sẽ trả về giá trị mặc định


#> romance
#> Brad Pitt
#> 8.5
1

#> romance
#> Brad Pitt
#> 8.5
2

Nếu bạn Excecute trên lệnh, nó không cung cấp cho bạn một

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
12. Trong trường hợp bạn muốn xuất ra giá trị đó cho khóa được yêu cầu không có mặt, bạn có thể xác định chức năng của riêng mình và chuyển nó cho DefaultDict. Xem ví dụ dưới đây
In case you want to output that the value for the requested key is not present, you can define your own function and pass it to the defaultdict.
See below example


#> romance
#> Brad Pitt
#> 8.5
3

#> romance
#> Brad Pitt
#> 8.5
4

Theo tất cả các cách khác, nó giống như một từ điển bình thường. Các lệnh Cú pháp tương tự cũng được sử dụng cho DefaultDict.

Trên thực tế, có thể khắc phục

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
12 trong từ điển bằng cách sử dụng phương pháp
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
20.


#> romance
#> Brad Pitt
#> 8.5
5

#> romance
#> Brad Pitt
#> 8.5
6

Đặt hàng

Dict là một bộ sưu tập không có thứ tự của các cặp giá trị chính. Nhưng, một

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
21 duy trì thứ tự mà các phím được chèn.

Nó là phân lớp của


#> Size or space occupied by dictionary 240
#> Size or space occupied by namedtuple 64
9.

Tôi sẽ tạo ra một


#> Size or space occupied by dictionary 240
#> Size or space occupied by namedtuple 64
9 bình thường và làm cho nó
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
21 để cho bạn thấy sự khác biệt


#> romance
#> Brad Pitt
#> 8.5
7

#> romance
#> Brad Pitt
#> 8.5
8

Trong một

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
21, ngay cả sau khi thay đổi giá trị của một số khóa nhất định, thứ tự vẫn giống hoặc không thay đổi.


#> romance
#> Brad Pitt
#> 8.5
9
# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
0

Điều gì xảy ra khi bạn xóa và kiểm soát lại các khóa để đặt hàng

Khi một khóa bị xóa, thông tin về thứ tự của nó cũng bị xóa. Khi bạn kiểm tra lại khóa, nó được coi là một mục nhập mới và thông tin đơn hàng tương ứng được lưu trữ.

# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
1
# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
2

Khi tái cấu trúc khóa, nó được coi là một mục mới.

# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
3
# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
4

Bạn có thể thấy chiếc xe đạp là cuối cùng, thứ tự đã thay đổi khi chúng tôi xóa chìa khóa.

Có một số lệnh hữu ích có thể được thực thi. Chúng tôi có thể thực hiện các chức năng sắp xếp theo nhu cầu

Sắp xếp với OrderedDict

Điều gì sẽ xảy ra nếu bạn muốn sắp xếp các mục theo thứ tự tăng giá trị của chúng? Điều này sẽ giúp bạn trong phân tích dữ liệu

Sắp xếp các mục theo khóa (theo thứ tự tăng dần)

# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
5
# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
6

Sắp xếp các cặp theo giá trị (theo thứ tự tăng dần)

# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
7
# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
6

Sắp xếp từ điển theo độ dài của chuỗi khóa (theo thứ tự tăng dần)

# Creating namedtuple by passing fieldnames as a list of strings
book = namedtuple('book',['price','no_of_pages','author'])

harry_potter = book('500','367','JK ROWLING')
pride_and_prejudice = book('300','200','jane_austen')
tale = book('199','250','christie')

print('Price of pride and prejudice is ',pride_and_prejudice.price)
print('author of harry potter is',harry_potter.author)
9

#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
0

Chainmap

Chainmap là một kiểu dữ liệu container lưu trữ nhiều từ điển. Trong nhiều trường hợp, bạn có thể có từ điển có liên quan hoặc tương tự, bạn có thể lưu trữ chúng tập thể trong một

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
26
In many cases, you might have relevant or similar dictionaries, you can store them collectively in a
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
26

Bạn có thể in tất cả các mục trong

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
26 bằng toán tử
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
28. Mã dưới đây thể hiện giống nhau


#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
1

#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
2

Bạn có thể in các khóa của tất cả các từ điển trong một chuỗi bằng cách sử dụng hàm

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
29


#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
3

#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
4

Bạn có thể in các giá trị của tất cả các từ điển trong một chuỗi bằng cách sử dụng

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
30function


#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
5

#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
6

Điều gì xảy ra khi chúng ta có các phím dự phòng trong một chuỗi

Có thể là 2 từ điển có thể có cùng một khóa. Xem một ví dụ dưới đây.


#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
7

#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
8

Quan sát rằng ’màu đỏ không được lặp lại, nó chỉ được in một lần

Cách thêm từ điển mới vào Chainmap

Bạn có thể thêm một từ điển mới khi bắt đầu một chuỗi bằng phương pháp

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
31. Nó được thể hiện trong mã dưới đây.


#> Price of pride and prejudice is  300
#> author of harry potter is JK ROWLING
9
print(tale[1])
0

Cách đảo ngược thứ tự từ điển trong Chainmap

Thứ tự trong đó các từ điển được lưu trữ trong một chuỗi có thể được đảo ngược bằng hàm

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
32.

print(tale[1])
1
print(tale[1])
2

Danh sách người dùng

Hy vọng bạn quen thuộc với Python

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
33s ?.

Một kiểu dữ liệu container giống như danh sách, là lớp trình bao bọc cho

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
33S.

Cú pháp:

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
36

Bạn vượt qua một danh sách bình thường như một đối số cho UserList. Danh sách này được lưu trữ trong thuộc tính dữ liệu và có thể được truy cập thông qua phương thức

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
37.

print(tale[1])
3
print(tale[1])
4

Việc sử dụng danh sách người dùng là gì

Giả sử bạn muốn tăng gấp đôi tất cả các yếu tố trong một số danh sách cụ thể là phần thưởng. Hoặc có thể bạn muốn đảm bảo rằng không có yếu tố nào có thể bị xóa khỏi một danh sách nhất định.

Trong những trường hợp như vậy, chúng tôi cần thêm một hành vi nhất định vào danh sách của chúng tôi, có thể được thực hiện bằng danh sách người dùng.

Ví dụ: hãy để tôi chỉ cho bạn cách

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
34 có thể được sử dụng để ghi đè chức năng của phương thức tích hợp. Mã dưới đây ngăn chặn việc bổ sung một giá trị mới (hoặc nối tiếp) vào danh sách.

print(tale[1])
5
print(tale[1])
6

Mã trên in thông báo

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
39 và không cho phép nối thêm. Điều này có thể hữu ích nếu bạn muốn đảm bảo không ai có thể chèn tên của họ sau thời hạn cụ thể. Vì vậy,
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
34 có hiệu quả thời gian rất thực.

Người dùng

Giống như

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
41 là lớp trình bao bọc cho
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
33S,
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
43 là một lớp trình bao bọc cho
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
44S.

Nó cho phép bạn thêm một số chức năng/hành vi nhất định vào chuỗi. Bạn có thể chuyển bất kỳ đối số chuyển đổi chuỗi nào cho lớp này và có thể truy cập chuỗi bằng thuộc tính dữ liệu của lớp.

print(tale[1])
7
print(tale[1])
8

Như bạn có thể thấy trong ví dụ trên, số 765 đã được chuyển đổi thành một chuỗi ‘765, và có thể được truy cập thông qua phương thức

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
45.

Làm thế nào và khi người dùng có thể được sử dụng

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
43 có thể được sử dụng để sửa đổi chuỗi hoặc thực hiện các chức năng nhất định.

Điều gì sẽ xảy ra nếu bạn muốn xóa một từ cụ thể khỏi tệp văn bản (bất cứ nơi nào hiện tại)?

Có thể, một số từ đã đặt sai chỗ và cần phải được loại bỏ.

Hãy để xem một ví dụ về cách `userstring` có thể được sử dụng để xóa một số từ lẻ ra khỏi chuỗi

print(tale[1])
9

#> 250
0

Bạn có thể thấy rằng ‘bút chì và’ eraser đã bị xóa bằng cách sử dụng lớp chức năng

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
47.

Hãy để chúng tôi xem xét một trường hợp khác. Điều gì sẽ xảy ra nếu bạn cần thay thế một từ bằng một số từ khác trong toàn bộ tệp?

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
48 giúp điều này dễ dàng hơn nhiều như được hiển thị bên dưới. Mã bên dưới thay thế một từ nhất định trong toàn bộ văn bản bằng cách sử dụng
# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
43

Tôi đã xác định một chức năng bên trong lớp để thay thế một số từ nhất định bằng ‘Chủ tịch trong suốt.


#> 250
1

#> 250
2

Như bạn có thể thấy, ‘Rajesh, được thay thế bằng‘ Chủ tịch ở khắp mọi nơi. Tương tự, người dùng giúp bạn đơn giản hóa tất cả các quy trình

Người dùng

Nó là một lớp trình bao bọc cho từ điển. Cú pháp, các chức năng tương tự như UserList và UserTring.

syntax:

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
50

Chúng tôi vượt qua một từ điển làm đối số được lưu trữ trong thuộc tính dữ liệu của UserDict.


#> 250
3

#> 250
4

Cách người dùng có thể được sử dụng

# Creating a namedtuple. 

# The field values are passed as a string seperated by ' '
from collections import namedtuple
movie = namedtuple('movie','genre rating lead_actor')

# Create instances of movie
ironman = movie(genre='action',rating=8.5,lead_actor='robert downey junior')
titanic = movie(genre='romance',rating=8,lead_actor='leonardo dicaprio')
seven   = movie(genre='crime',rating=9,lead_actor='Brad Pitt')
51 cho phép bạn tạo một từ điển được sửa đổi theo nhu cầu của bạn. Hãy cùng xem một ví dụ về cách người dùng có thể được sử dụng để ghi đè chức năng của phương thức tích hợp. Mã dưới đây ngăn một cặp giá trị khóa bị bỏ.


#> 250
5

#> 250
6

Bạn sẽ nhận được một tin nhắn RunTimeError. Điều này sẽ giúp nếu bạn không muốn mất dữ liệu.

Điều gì sẽ xảy ra nếu một số khóa có giá trị rác và bạn cần thay thế chúng bằng nil hoặc ‘0,? Xem các ví dụ dưới đây về cách sử dụng UserDict cho cùng.


#> 250
7

#> 250
8

Trường có giá trị rác đã được thay thế bằng 0. Đây chỉ là những ví dụ đơn giản về cách người dùng cho phép bạn tạo một từ điển với chức năng cần thiết

Đây là tất cả các kiểu dữ liệu container từ mô -đun bộ sưu tập. Chúng tăng hiệu quả lên một lượng lớn khi được sử dụng trên các bộ dữ liệu lớn.

Sự kết luận

Tôi hy vọng bạn đã hiểu khi nào và tại sao sử dụng các kiểu dữ liệu container trên. Nếu bạn có bất kỳ câu hỏi nào, xin vui lòng bỏ nó trong các bình luận

Bài viết đề xuất

Hướng dẫn Python JSON Python Regex Hướng dẫn Python Guide Hướng dẫn xử lý Paralel trong Python
Python RegEx Tutorial
Python Logging Guide
Paralel Processing in Python

Bài viết này được đóng góp bởi Shrivarsheni.

Các bộ sưu tập có quan trọng Python không?

Bộ sưu tập là một mô-đun Python tích hợp cung cấp các kiểu dữ liệu container hữu ích.Các kiểu dữ liệu container cho phép chúng tôi lưu trữ và truy cập các giá trị một cách thuận tiện.Nói chung, bạn sẽ có danh sách, bộ dữ liệu và từ điển.Nhưng, trong khi xử lý dữ liệu có cấu trúc, chúng tôi cần các đối tượng thông minh hơn.provides useful container datatypes. Container datatypes allow us to store and access values in a convenient way. Generally, you would have used lists, tuples, and dictionaries. But, while dealing with structured data we need smarter objects.

Những bộ sưu tập nào được sử dụng trong Python?

Ngôn ngữ lập trình Python có bốn loại dữ liệu thu thập- danh sách, bộ, bộ và từ điển.list, tuple, sets and dictionary.

Có bao nhiêu loại bộ sưu tập trong Python?

Có bốn loại dữ liệu thu thập trong ngôn ngữ lập trình Python: Danh sách là một bộ sưu tập được đặt hàng và thay đổi.four collection data types in the Python programming language: List is a collection which is ordered and changeable.

Tôi nên nhập vào bộ sưu tập gì trong Python?

Python - Mô -đun bộ sưu tập..
Ví dụ: Khai báo một tuple có tên.>>> Nhập bộ sưu tập >>> Sinh viên = Bộ sưu tập.Được đặt tên ('Sinh viên', [Tên, Tuổi, Dấu hiệu]) ....
Ví dụ: Tạo đối tượng của Tuple được đặt tên.>>> S1 = Sinh viên ("Imran", 21, 98) ....
Ví dụ: Truy cập có tên Tuple.>>> S1.....
Ví dụ: Truy cập có tên Tuple.>>> S1 [0] 'Imran'.