Sự ảo ma của * trong Python

Trong bài viết này, mình sẽ chém gió về một số cách sử dụng khác nhau của a.k.a “*” trong Python Ảnh của Paints with Diamonds Tạo collections với các phần tử lặp lại: collections =[1,2,3]*2print(collections)# output: [1, 2, 3, 1, 2, 3] Chúng ta hoàn toàn có thể áp dụng tương tự với tuple

Trong bài viết này, mình sẽ chém gió về một số cách sử dụng khác nhau của a.k.a “*” trong Python
image.png

Ảnh của Paints with Diamonds

Tạo collections với các phần tử lặp lại:

collections =[1,2,3]*2print(collections)# output: [1, 2, 3, 1, 2, 3]

Chúng ta hoàn toàn có thể áp dụng tương tự với tuple hay chuỗi:

collections =(1,2,3)*2print(collections)# output: (1, 2, 3, 1, 2, 3)
strings ="VN"*2print(strings)# output: VNVN

Unpacking tập hợp tuần tự thành các biến riêng biệt

collections =(4,5)
x, y = collections
print(x, y)# output: 4 5

Với các biến mà ta không sử dụng ta có thể sử dụng _ để biểu diễn:

data =['VietNam',84,100,'VN']
country, country_code, population, short_name = data
print(country, country_code, population, short_name)# output: VietNam 84 100 VN
country, country_code, population, _ = data
print(country, country_code, population)# output: VietNam 84 100

Các tiêu đề sau mình xin mạn phép để tiếng anh vì dịch ra thì nghe nó cũng khá thô và cũng không sát nghĩa lắm :V

Unpacking Elements from Iterables of Arbitrary Length
Lấy tất cả các phần tử ở giữa của list:

grades =[1,2,3,4,5]
first,*middle, last = grades
print(first, middle, last)# output: 1 [2, 3, 4] 5

Lấy tất cả các phần tử còn lại của list:

record =('VN','vn@example.com','0773555121','0847555212')
name, email,*phone_numbers = record
print(name, email, phone_numbers)# output: VN vn@example.com ['0773555121', '0847555212']

Dùng vài cái * một lúc nó sẽ như này:

record =('FUKADA',50,123.45,(12,18,2012))
name,*_,(*_, year)= record
print(name, year)# output: FUKADA 2012

args and kwargs
Hãy thử xem hàm dưới đây:

defcalculate(a,b,*args,**kwargs):print(a, b)print(arg)for key in kwargs:print(key, kwargs[key])
calculate(2,3,4,5,6, num1=8,num2=9)# output: 2 3# (4, 5, 6)# num1 8# num2 9

Merging Different Types Of Iterables

var1 =(1,2,3)
var2 ={4,5,6}
collections =[*var1,*var2]print(collections)#output: [1, 2, 3, 4, 5, 6]

Merging Two Dictionaries

dict1 ={'key1':1,'key2':2}
dict2 ={'key3':3,'key4':4}dict={**dict1,**dict2}print(dict)#output: {'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4}

Nguồn: viblo.asia

Bài viết liên quan

Thay đổi Package Name của Android Studio dể dàng với plugin APR

Nếu bạn đang gặp khó khăn hoặc bế tắc trong việc thay đổi package name trong And

Lỗi không Update Meta_Value Khi thay thế hình ảnh cũ bằng hình ảnh mới trong WordPress

Mã dưới đây hoạt động tốt có 1 lỗi không update được postmeta ” meta_key=

Bài 1 – React Native DevOps các khái niệm và các cài đặt căn bản

Hướng dẫn setup jenkins agent để bắt đầu build mobile bằng jenkins cho devloper an t

Chuyển đổi từ monolith sang microservices qua ví dụ

1. Why microservices? Microservices là kiến trúc hệ thống phần mềm hướng dịch vụ,