Tương tự như any[]
, một giải pháp cũng hỗ trợ ngắn mạch:
>>> from itertools import chain
>>> mylist = [['value1', 'value2', 'value3'], ['secval1', 'secval2', 'secval3']]
>>> 'value2' in chain[*mylist]
True
bằng chứng rằng nó ngắn mạch như any[]
:
>>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
Kiểm tra xem giá trị có tồn tại trong danh sách hai chiều trong Python # không
Sử dụng hàm any[]
để kiểm tra xem giá trị có tồn tại trong danh sách hai chiều hay không, ví dụ: if any['value' in nested_list for nested_list in my_2d_list]:
. Hàm any[]
sẽ trả về
1 nếu giá trị tồn tại trong danh sách và >>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
2 khác.
>>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
Copied!
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any['three' in nested_list for nested_list in my_2d_list]: # 👇️ this runs print['three is contained in the two-dimensional list'] else: print['three is NOT contained in the two-dimensional list'] # ------------------------------------------------------------------ # ✅ if you need to get the sublist that contains the value if any['three' in [match := nested_list] for nested_list in my_2d_list]: # 👇️ this runs print['three is contained in the two-dimensional list'] print[match] # 👉️ ['three', 'four'] print[match.index['three']] # 👉️ 0 else: print['three is NOT contained in the two-dimensional list']
Chúng tôi đã sử dụng một biểu thức máy phát để lặp qua danh sách hai chiều.
Biểu thức của máy phát được sử dụng để thực hiện một số hoạt động cho mọi phần tử hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.
Trên mỗi lần lặp, chúng tôi kiểm tra xem một giá trị cụ thể có trong danh sách lồng nhau không.
Copied!
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any['three' in nested_list for nested_list in my_2d_list]: # 👇️ this runs print['three is contained in the two-dimensional list'] else: print['three is NOT contained in the two-dimensional list']
Bất kỳ chức năng nào lấy một sự khác biệt như một đối số và trả về
>>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
1 nếu bất kỳ yếu tố nào trong điều đó là sự thật.Nếu giá trị được chỉ định được tìm thấy trong bất kỳ danh sách lồng nhau nào, thì hàm ngắn của hàm any[]
trả về
1.>>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
Bạn có thể sử dụng cú pháp biểu thức gán nếu bạn cần lấy danh sách lồng nhau có chứa giá trị được chỉ định.
Copied!
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any['three' in [match := nested_list] for nested_list in my_2d_list]: # 👇️ this runs print['three is contained in the two-dimensional list'] print[match] # 👉️ ['three', 'four'] print[match.index['three']] # 👉️ 0 else: print['three is NOT contained in the two-dimensional list']
Biểu thức gán cho phép chúng tôi gán cho các biến trong một biểu thức bằng cú pháp
>>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
6.Nếu có thể trống rỗng hoặc không có yếu tố nào trong sự thật là sự thật, hàm
>>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
7 trả về >>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
2.Copied!
my_2d_list = [['one', 'two'], ['three', 'four'], ['five', 'six']] if any['HELLO' in nested_list for nested_list in my_2d_list]: print['HELLO is contained in the two-dimensional list'] else: # 👇️ this runs print['HELLO is NOT contained in the two-dimensional list']
Không có danh sách nào lồng nhau chứa một mục có giá trị được chỉ định, do đó điều kiện không bao giờ được đáp ứng và hàm any[]
trả về
>>> it=chain[*mylist]
>>> 'value2' in it
True
>>> list[it] #part of iterable still not traversed
['value3', 'secval1', 'secval2', 'secval3']
2.