Trong bài viết này, chúng tôi sẽ điều chỉnh các cách khác nhau để kiểm tra xem tất cả các yếu tố trong một danh sách nhất định là giống nhau hoặc khớp với một điều kiện.
Giả sử chúng ta có một danh sách chuỗi, tức là.
# List of string listOfStrings = ['Hello'] * 10
Bây giờ, hãy để sử dụng hàm python tất cả [] để kiểm tra xem tất cả các phần tử trong danh sách đã cho có giống nhau không.
Python: tất cả [] hàm
Python tất cả [] chức năng kiểm tra xem tất cả các yếu tố của IT có thể được cho là đúng.
Kiểm tra xem phần tử có giống nhau bằng tất cả []
Hãy để chuyển đổi danh sách thành ITable và kiểm tra xem mỗi mục nhập có thể bằng với phần tử đầu tiên của danh sách bằng tất cả [], tức là không.
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]
Quảng cáo
Đếm [] Trả về số lượng xuất hiện của phần tử đã cho trong danh sách.
Hãy gọi cho chức năng Count [] của danh sách với phần tử Firts của Danh sách là đối số. & NBSP; Nếu số lần xuất hiện của nó bằng độ dài của danh sách, thì điều đó có nghĩa là tất cả các phần tử trong danh sách là giống nhau.
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]
Hãy cùng làm điều tương tự trong một dòng, tức là.
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]
Kiểm tra xem tất cả các phần tử có giống nhau bằng cách sử dụng tập hợp không
Vì tập hợp chỉ chứa các phần tử duy nhất, do đó, hãy chuyển đổi danh sách thành SET. & NBSP; Nếu kích thước bộ là 1 thì điều đó có nghĩa là tất cả các phần tử trong danh sách đã cho là giống nhau.
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 1
Ví dụ hoàn chỉnh như sau,
def main[]: # List of string listOfStrings = ['Hello'] * 10 # Print the List print[listOfStrings] ''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"] ''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"] # Do the above logic in single line result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"] ''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 1 if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"] if __name__ == '__main__': main[]
Output:
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal& nbsp;
Đưa ra một danh sách chỉ chứa các phần tử chuỗi Nhiệm vụ ở đây là viết một chương trình Python để kiểm tra xem tất cả chúng có phải là số hay không. Nếu tất cả đều trả về số đúng khác, hãy trả về sai.
Input : test_list = ["434", "823", "98", "74"] Output : True Explanation : All Strings are digits.
Input : test_list = ["434", "82e", "98", "74"] Output : False Explanation : e is not digit, hence verdict is False.
Phương pháp 1: Sử dụng tất cả [], isDigit [] và biểu thức máy phátUsing all[], isdigit[] and generator expression
Trong đó, chúng tôi kiểm tra các số từ isDigit []. Tất cả [] được sử dụng để kiểm tra tất cả các chuỗi là số và lần lặp cho mỗi chuỗi được thực hiện bằng cách sử dụng biểu thức máy phát.
Example:
Python3
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]3
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]5
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]6
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]8
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]223
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]4
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]6
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]9
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]2
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]3
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]4
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]5
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]6
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]4
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 10
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 13
Đầu ra
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True
Độ phức tạp về thời gian: O [n] O[n]
Không gian phụ trợ: O [n]O[n]
Phương pháp 2: Sử dụng & nbsp; all [], isDigit [] và map [] Using all[], isdigit[] and map[]
Trong đó, chúng tôi mở rộng logic kiểm tra cho từng chuỗi bằng bản đồ [], thay vì biểu thức của trình tạo. Nghỉ ngơi tất cả các chức năng được thực hiện tương tự như phương thức trên.
Example:
Python3
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]3
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]5
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]6
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]8
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]223
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]4
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]6
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]9
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]2
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal8
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]4
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 10
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 13
Đầu ra
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True
Độ phức tạp về thời gian: O [n]O[n]
Không gian phụ trợ: O [n]O[n]
Phương pháp 2: Sử dụng & nbsp; all [], isDigit [] và map []Using isnumeric[] and len[] methods
Python3
Trong đó, chúng tôi mở rộng logic kiểm tra cho từng chuỗi bằng bản đồ [], thay vì biểu thức của trình tạo. Nghỉ ngơi tất cả các chức năng được thực hiện tương tự như phương thức trên.
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]4
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]6
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]9
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True2
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]3
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]5
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]6
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]8
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]223
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]2
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal8
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]02
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True2
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]06
Phương pháp 3: Sử dụng phương thức isnumeric [] và len []
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]00
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]11
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]14
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]15
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True9
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]07
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]19
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]4
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 10
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 13
Đầu ra
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]1
Độ phức tạp về thời gian: O [n]O[n]
Không gian phụ trợ: O [n]O[n]
Phương pháp 2: Sử dụng & nbsp; all [], isDigit [] và map []
Python3
Trong đó, chúng tôi mở rộng logic kiểm tra cho từng chuỗi bằng bản đồ [], thay vì biểu thức của trình tạo. Nghỉ ngơi tất cả các chức năng được thực hiện tương tự như phương thức trên.
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]4
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]6
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]9
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]3
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]5
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]6
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]8
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]223
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]2
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal8
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]3
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]5
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]6
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]8
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]223
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]2
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal8
Phương pháp 3: Sử dụng phương thức isnumeric [] và len []
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]3
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]5
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]6
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
Input : test_list = ["434", "82e", "98", "74"] Output : False Explanation : e is not digit, hence verdict is False.0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]0
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]223
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]4
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True6
result = len[listOfStrings] > 0 and all[elem == listOfStrings[0] for elem in listOfStrings]6
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True8
The original list is : ['434', '823', '98', '74'] Are all strings digits ? : True9
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]00
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]01
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]07
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]4
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]09
Phương pháp 4: Sử dụng các phương thức thay thế [] và Len []
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]4
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]5
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 10
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]7
''' check if element are same using list.count[] If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len[listOfStrings] > 0 : result = listOfStrings.count[listOfStrings[0]] == len[listOfStrings]8
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len[set[listOfStrings]] == 13
Đầu ra
''' check if element are same using all[] It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len[listOfStrings] > 0 : result = all[elem == listOfStrings[0] for elem in listOfStrings] if result : print["All Elements in List are Equal"] else: print["All Elements in List are Not Equal"]1