Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
This is the brute method in which we perform this task. In this, we run a loop to keep a memory of longest string length and return the string which has max length in list.
Đôi khi, trong khi làm việc với danh sách Python, chúng ta có thể gặp vấn đề trong đó chúng ta nhận được các chuỗi dưới dạng các yếu tố và muốn tính toán chuỗi có độ dài tối đa. Loại vấn đề này có thể có các ứng dụng trong nhiều lĩnh vực. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết.
Phương pháp số 1: Sử dụng loopthis là phương pháp vũ phu trong đó chúng tôi thực hiện nhiệm vụ này. Trong đó, chúng tôi chạy một vòng lặp để giữ bộ nhớ có độ dài chuỗi dài nhất và trả về chuỗi có độ dài tối đa trong danh sách.
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks1
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks2
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks3
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks4
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks5
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks6
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks5
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks8
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks5
mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
0The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks5
mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
223mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
4mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
5mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
6 mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
7 mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
8mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
9>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
0____12 >>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
2>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
3>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
4 >>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
5>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
6 >>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
7>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
8>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
9 def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
0def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
1def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
2>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
03012 def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
0def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
6
2def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
8def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Maximum length string is : geeks
2
0max[some_list , key = len]
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks
4mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
5mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
3 max[some_list , key = len]
7 mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
5max[some_list , key = len]
This method can also be used to solve this problem. In this, we use inbuilt max[] with “len” as key argument to extract the string with the maximum length.
Đôi khi, trong khi làm việc với danh sách Python, chúng ta có thể gặp vấn đề trong đó chúng ta nhận được các chuỗi dưới dạng các yếu tố và muốn tính toán chuỗi có độ dài tối đa. Loại vấn đề này có thể có các ứng dụng trong nhiều lĩnh vực. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết.
Phương pháp số 1: Sử dụng loopthis là phương pháp vũ phu trong đó chúng tôi thực hiện nhiệm vụ này. Trong đó, chúng tôi chạy một vòng lặp để giữ bộ nhớ có độ dài chuỗi dài nhất và trả về chuỗi có độ dài tối đa trong danh sách.
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks1
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks2
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks3
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks4
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks5
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks6
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks5
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks8
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks5
mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
0The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks5
mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
223def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
2>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
03012 def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
0def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
6
2def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
8def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
The original list : ['gfg', 'is', 'best', 'for', 'geeks']
Maximum length string is : geeks
2
0max[some_list , key = len]
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks
Tôi có một danh sách độ dài thay đổi và đang cố gắng tìm cách kiểm tra xem mục danh sách hiện đang được đánh giá là chuỗi dài nhất có trong danh sách. Và tôi đang sử dụng Python 2.6.1
Ví dụ:
mylist = ['abc','abcdef','abcd']
for each in mylist:
if condition1:
do_something[]
elif ___________________: #else if each is the longest string contained in mylist:
do_something_else[]
Chắc chắn có một danh sách đơn giản hiểu biết ngắn và thanh lịch mà tôi đang nhìn ra?
Tế bào thần kinh
4.6924 Huy hiệu vàng33 Huy hiệu bạc54 Huy hiệu đồng4 gold badges33 silver badges54 bronze badges
Đã hỏi ngày 16 tháng 5 năm 2009 lúc 21:15May 16, 2009 at 21:15
Từ chính tài liệu Python, bạn có thể sử dụng
for st in mylist:
if len[st]==max_length:...
8:>>> mylist = ['123','123456','1234']
>>> print max[mylist, key=len]
123456
Đã trả lời ngày 16 tháng 5 năm 2009 lúc 21:19May 16, 2009 at 21:19
Paolo Bergantinopaolo BergantinoPaolo Bergantino
473K79 Huy hiệu vàng518 Huy hiệu bạc436 Huy hiệu Đồng79 gold badges518 silver badges436 bronze badges
7
def longestWord[some_list]:
count = 0 #You set the count to 0
for i in some_list: # Go through the whole list
if len[i] > count: #Checking for the longest word[string]
count = len[i]
word = i
return ["the longest string is " + word]
hoặc dễ dàng hơn nhiều:
max[some_list , key = len]
Pableiros
13.9k11 Huy hiệu vàng92 Huy hiệu bạc99 Huy hiệu đồng11 gold badges92 silver badges99 bronze badges
Đã trả lời ngày 14 tháng 12 năm 2016 lúc 23:26Dec 14, 2016 at 23:26
0
Điều gì sẽ xảy ra nếu có nhiều hơn 1 chuỗi dài nhất [nghĩ '12' và '01']?
Hãy thử điều đó để có được yếu tố dài nhất
max_length,longest_element = max[[[len[x],x] for x in ['a','b','aa']]]
Và sau đó là foreach thường xuyên
for st in mylist:
if len[st]==max_length:...
Đã trả lời ngày 16 tháng 5 năm 2009 lúc 21:22May 16, 2009 at 21:22
Elazar Leibovichelazar LeibovichElazar Leibovich
32K30 Huy hiệu vàng120 Huy hiệu bạc167 Huy hiệu Đồng30 gold badges120 silver badges167 bronze badges
Để có được mục nhỏ nhất hoặc lớn nhất trong danh sách, hãy sử dụng các hàm tối đa và tối đa tích hợp:
lo = min[L]
hi = max[L]
Như với sắp xếp, bạn có thể chuyển trong một đối số "khóa" được sử dụng để ánh xạ các mục trong danh sách trước khi chúng được so sánh:
lo = min[L, key=int]
hi = max[L, key=int]
//effbot.org/zone/python-list.htm
Có vẻ như bạn có thể sử dụng chức năng tối đa nếu bạn ánh xạ chính xác cho các chuỗi và sử dụng nó làm so sánh. Tôi sẽ đề nghị chỉ tìm thấy tối đa một lần mặc dù tất nhiên, không phải cho mỗi yếu tố trong danh sách.
Đã trả lời ngày 16 tháng 5 năm 2009 lúc 21:20May 16, 2009 at 21:20
Gavin Hgavin hGavin H
10.2k2 Huy hiệu vàng34 Huy hiệu bạc41 Huy hiệu đồng2 gold badges34 silver badges41 bronze badges
lo = min[L]
hi = max[L]
9 hoặc chỉ lo = min[L, key=int]
hi = max[L, key=int]
0Đã trả lời ngày 16 tháng 5 năm 2009 lúc 21:20May 16, 2009 at 21:20
Gavin Hgavin hHarryM
10.2k2 Huy hiệu vàng34 Huy hiệu bạc41 Huy hiệu đồng13 silver badges7 bronze badges
1
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] Maximum length string is : geeks0
lo = min[L]
hi = max[L]
9 hoặc chỉ lo = min[L, key=int]
hi = max[L, key=int]
0Oct 21, 2011 at 23:21