For given input lists: a,b,c and 1,2,3 Create a dictionary from two input lists
4 phương ánCó giải thích
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "def make_dict(lst1,lst2): res = {} for key,value in zip(lst1,lst2): res[key]= value return res". Khi viết thành nhiều dòng, thân hàm tạo dict rỗng res = {}, dòng for thụt vào một mức duyệt các cặp do zip sinh ra, câu gán res[key]= value thụt thêm một mức nằm trong vòng lặp, còn return res thẳng hàng với for để trả kết quả sau khi lặp xong; zip(lst1,lst2) ghép phần tử cùng vị trí thành các cặp (a, 1), (b, 2), (c, 3) nên kết quả là dict ánh xạ a sang 1, b sang 2, c sang 3. "def make_dict(lst1,lst2): res = {} res[lst1]= lst2 return res" sai vì list là kiểu không băm được (unhashable) nên dùng cả danh sách làm khoá gây TypeError, và kể cả chạy được cũng chỉ tạo một cặp chứ không ghép từng phần tử. "def make_dict(lst1,lst2): res = {} for key,value in (lst1,lst2): res[key]= value return res" sai vì thiếu zip: vòng lặp duyệt một tuple gồm hai danh sách, mỗi lượt cố tách một danh sách ba phần tử vào hai biến key, value nên báo ValueError too many values to unpack. "def make_dict(lst1,lst2): res = [] for key,value in zip(lst1,lst2): res[key]= value return res" sai vì res là list rỗng, gán res[key] với key là chuỗi gây TypeError do chỉ số của list phải là số nguyên.
Cùng phần 1
Thinking with the lens of the scientific process, what would your next steps be if you wanted to decide where to open the next store for your sled business?Có lời giảiCâu hỏiWhen you compiled the JSON files into a single DataFrame or NumPy array, about how many days did the entire range of dates span?Có lời giảiCâu hỏiWhich library in Python is commonly used for reading and writing CSV files?Có lời giảiCâu hỏiCả 31 câu của chủ đề31 câu