Hướng dẫn write a python program to extract a list of values from a given list of dictionaries - viết chương trình python để trích xuất danh sách các giá trị từ danh sách từ điển đã cho

Cập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21:50:49 [UTC/GMT +8 giờ]

Từ điển Python: Bài tập-52 với giải pháp

Viết một chương trình Python để trích xuất danh sách các giá trị từ một danh sách từ điển nhất định.

Giải pháp mẫu::

Mã Python:

def test[lst, marks]:
    result = [d[marks] for d in lst if marks in d]
 
    return result

marks = [{'Math': 90, 'Science': 92}, 
         {'Math': 89, 'Science': 94}, 
         {'Math': 92, 'Science': 88}]

print["\nOriginal Dictionary:"]
print[marks]
subj = "Science"
print["\nExtract a list of values from said list of dictionaries where subject =",subj]
print[test[marks, subj]]

print["\nOriginal Dictionary:"]
print[marks]
subj = "Math"
print["\nExtract a list of values from said list of dictionaries where subject =",subj]
print[test[marks, subj]]

Đầu ra mẫu:

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]

Trình bày bằng hình ảnh:

Flowchart:

Trực quan hóa thực thi mã Python:

Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:

Trình chỉnh sửa mã Python:

Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn [và nhận xét] thông qua Disqus.

Trước đây: Từ điển Python chứa danh sách là giá trị. Viết một chương trình Python để cập nhật các giá trị danh sách trong từ điển đã nói. A Python Dictionary contains List as value. Write a Python program to update the list values in the said dictionary.
Next: Write a Python program to find the length of a given dictionary values.

Python: Lời khuyên trong ngày

Sử dụng toàn bộ danh sách để rút ngắn cho các vòng lặp:

x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result

Output:

[2, 4, 6, 8, 10]
[[element * 2] for element in x]

Output:

[2, 4, 6, 8, 10]

Phương pháp 2: Sử dụng Filter [] và Lambda

Trong đó, chúng tôi thực hiện nhiệm vụ lọc bằng Filter [] và hàm LambDA được sử dụng để đưa logic vào lọc. Toán tử được sử dụng để kiểm tra sự hiện diện của một khóa cụ thể. : test_list = [{‘gfg’ : 2, ‘is’ : 8, ‘good’ : 3}, {‘gfg’ : 1, ‘for’ : 10, ‘geeks’ : 9}, {‘love’ : 3}], key= “gfg”
Output : [{‘gfg’: 2, ‘is’: 8, ‘good’: 3}, {‘gfg’ : 1, ‘for’ : 10, ‘geeks’ : 9}] 
Explanation : gfg is present in first two dictionaries, hence extracted.

Đầu vào: test_list = [{'gfg': 2, 'là': 8, 'tốt': 3}, {'gfg': 1, 'cho': 10, 'geek': 9}, {'tình yêu': 3, 'gfgs': 4}], key = đầu ra tốt: [{'gfg': 2, 'là': 8, 'tốt': 3}] & nbsp; trích xuất. & nbsp; : test_list = [{‘gfg’ : 2, ‘is’ : 8, ‘good’ : 3}, {‘gfg’ : 1, ‘for’ : 10, ‘geeks’ : 9}, {‘love’ : 3, ‘gfgs’ : 4}], key = “good”
Output : [{‘gfg’: 2, ‘is’: 8, ‘good’: 3}] 
Explanation : good is present in 1st dictionary, hence extracted. 

Phương pháp 1: Sử dụng danh sách hiểu và khóa []Using list comprehension and keys[]

Trong đó, chúng tôi kiểm tra sự hiện diện của khóa sử dụng trong toán tử, các phím được trích xuất bằng phím []. Danh sách hiểu được sử dụng để lặp lại trên các từ điển khác nhau.in operator, keys are extracted using the key[]. List comprehension is used to iterate over different dictionaries.

Example:

Python3

test_list =

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
0
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
1
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
3
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
4
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
5__12

x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
3
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
4
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
1
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
7
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
4
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
9
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
[2, 4, 6, 8, 10]
1
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
4
[2, 4, 6, 8, 10]
3
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
[2, 4, 6, 8, 10]
5
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
2

x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
3
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
4
[2, 4, 6, 8, 10]
9
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
1
[[element * 2] for element in x]
2

[[element * 2] for element in x]
3
[[element * 2] for element in x]
4
[[element * 2] for element in x]
5
[[element * 2] for element in x]
6
[[element * 2] for element in x]
7
[[element * 2] for element in x]
8

[[element * 2] for element in x]
9____9
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
1

[2, 4, 6, 8, 10]
2=
[2, 4, 6, 8, 10]
4
[2, 4, 6, 8, 10]
5
[2, 4, 6, 8, 10]
6
[2, 4, 6, 8, 10]
7 test_list __

[[element * 2] for element in x]
3
[[element * 2] for element in x]
4
The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}]
6
[[element * 2] for element in x]
6
[[element * 2] for element in x]
7
The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}]
9

Đầu ra

The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}]

Độ phức tạp về thời gian: O [n], trong đó n là chiều dài của không gian listauxiliary đã cho: O [n]O[n], where n is the length of the given list
Auxiliary Space: O[n]

Phương pháp 2: Sử dụng Filter [] và LambdaUsing filter[] and lambda

Trong đó, chúng tôi thực hiện nhiệm vụ lọc bằng Filter [] và hàm LambDA được sử dụng để đưa logic vào lọc. Toán tử được sử dụng để kiểm tra sự hiện diện của một khóa cụ thể.in operator is used to check the presence of a specific key.

Example:

Python3

test_list =

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
0
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
1
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
3
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
4
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
5__12

x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
3
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
4
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
1
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
7
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
4
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
9
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
[2, 4, 6, 8, 10]
1
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
4
[2, 4, 6, 8, 10]
3
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
[2, 4, 6, 8, 10]
5
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
2

x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
3
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
4
[2, 4, 6, 8, 10]
9
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
x = [1, 2, 3, 4, 5]
result = []
for idx in range[len[x]]:
    result.append[x[idx] * 2]
result
1
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
4
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
1
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
2
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
07
[[element * 2] for element in x]
2

[[element * 2] for element in x]
3
[[element * 2] for element in x]
4
[[element * 2] for element in x]
5
[[element * 2] for element in x]
6
[[element * 2] for element in x]
7
[[element * 2] for element in x]
8

[[element * 2] for element in x]
9____9
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
9

[2, 4, 6, 8, 10]
2=
The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}]
2
[[element * 2] for element in x]
4
Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Science
[92, 94, 88]

Original Dictionary:
[{'Math': 90, 'Science': 92}, {'Math': 89, 'Science': 94}, {'Math': 92, 'Science': 88}]

Extract a list of values from said list of dictionaries where subject = Math
[90, 89, 92]
222

[[element * 2] for element in x]
3
[[element * 2] for element in x]
4
The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}]
6
[[element * 2] for element in x]
6
[[element * 2] for element in x]
7
The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}]
9

Đầu ra

The original list is : [{'gfg': 2, 'is': 8, 'good': 3}, {'gfg': 1, 'for': 10, 'geeks': 9}, {'love': 3, 'gfg': 4}]
The filtered Dictionaries : [{'gfg': 2, 'is': 8, 'good': 3}]

Độ phức tạp về thời gian: O [n], trong đó n là chiều dài của không gian listauxiliary đã cho: O [n]O[n], where n is the length of the given list
Auxiliary Space: O[n]


Bài Viết Liên Quan

Chủ Đề