使用python从列表中获取最长的字符串
我试图打印列表中的“最长字符串”。 我不想使用max()或sort()。我只是想毫无办法地把它打印出来。 我成功地得到了最短的字符串,但打印出最长的字符串时遇到了问题。它们的产量是一样的使用python从列表中获取最长的字符串,python,Python,我试图打印列表中的“最长字符串”。 我不想使用max()或sort()。我只是想毫无办法地把它打印出来。 我成功地得到了最短的字符串,但打印出最长的字符串时遇到了问题。它们的产量是一样的 a_list=['abc','bcd','bcdefg','abba','cddc','opq'] b_list=[] for i in a_list: a=len(i) b_list.append(a) p=b_list[0] for k in b_list: if k<
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
a=len(i)
b_list.append(a)
p=b_list[0]
for k in b_list:
if k<=p:
k=p
r=b_list.index(p)
print("Shortest string : " ,a_list[r])
break
这是打印字符串最长的字符串的结果。有什么问题吗?
再一次,我想在不使用min()、max()、sort()方法的情况下求解它。您正在分配给
k
-迭代变量,而不是p
最大值
此外,缩进需要稍微固定:
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
a=len(i)
b_list.append(a)
p=b_list[0]
for k in b_list[1:]:
if k>p:
p = k
r=b_list.index(p)
print("longest string : " ,a_list[r])
输出:
longest string : bcdefg
注:您的最短字符串代码也有同样的问题。它恰好与指定给
k
的特定输入一起工作-迭代变量,而不是p
最大值
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
a=len(i)
b_list.append(a)
max_len_element =a_list[0]
max = b_list[0]
for i in range(len(b_list)):
if b_list[i] > max:
max = b_list[i]
max_len_element = a_list[i]
print(max, max_len_element)
此外,缩进需要稍微固定:
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
a=len(i)
b_list.append(a)
p=b_list[0]
for k in b_list[1:]:
if k>p:
p = k
r=b_list.index(p)
print("longest string : " ,a_list[r])
输出:
longest string : bcdefg
注:您的最短字符串代码也有同样的问题。它只是碰巧与那个特定的输入一起工作
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
b_list=[]
for i in a_list:
a=len(i)
b_list.append(a)
max_len_element =a_list[0]
max = b_list[0]
for i in range(len(b_list)):
if b_list[i] > max:
max = b_list[i]
max_len_element = a_list[i]
print(max, max_len_element)
最大长度元素是您的最大长度元素
max_len_元素是最大长度元素这是一种非常复杂的方法@rdas已经告诉您关于
p=k
而不是k=p
。您只需在列表中循环一次:
a_list = ['abc','bcd','bcdefg','abba','cddc','opq']
shortest, *remaining = a_list # 'abc' and ['bcd','bcdefg','abba','cddc','opq']
ls = len(shortest)
for word in remaining:
if len(word) < ls:
shortest = word
ls = len(word)
print(shortest)
a_list=['abc'、'bcd'、'bcdefg'、'abba'、'cddc'、'opq']
最短,*剩余=a#u列表#'abc'和['bcd','bcdefg','abba','cddc','opq']
ls=长度(最短)
对于剩余的单词:
如果len(word)
这是一种非常复杂的方法@rdas已经告诉您关于p=k
而不是k=p
。您只需在列表中循环一次:
a_list = ['abc','bcd','bcdefg','abba','cddc','opq']
shortest, *remaining = a_list # 'abc' and ['bcd','bcdefg','abba','cddc','opq']
ls = len(shortest)
for word in remaining:
if len(word) < ls:
shortest = word
ls = len(word)
print(shortest)
a_list=['abc'、'bcd'、'bcdefg'、'abba'、'cddc'、'opq']
最短,*剩余=a#u列表#'abc'和['bcd','bcdefg','abba','cddc','opq']
ls=长度(最短)
对于剩余的单词:
如果len(word)
您可以通过以下方式在列表中查找最长和最短的字符串:
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
#我们为这两个变量提供列表中第一个元素的值
shortest=a_列表[0]
最长=a_列表[0]
对于a_列表中的i[1:]:
如果len(最短)>=len(i):#检查当前字符串是否较短
最短=i#如果是,则更改变量值
如果len(longest)您可以通过以下方式在列表中查找最长和最短的字符串:
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
#我们为这两个变量提供列表中第一个元素的值
shortest=a_列表[0]
最长=a_列表[0]
对于a_列表中的i[1:]:
如果len(最短)>=len(i):#检查当前字符串是否较短
最短=i#如果是,则更改变量值
如果len(longest)您可以将第一个元素定义为max,并与其余元素进行比较,如果找到新的max,则替换max
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
max = a_list[0]
for i in a_list[1:]:
if len(i)>len(max):
max = i
print(max)
您可以将第一个元素定义为max,并与rest进行比较,如果找到新的max,则替换max
a_list=['abc','bcd','bcdefg','abba','cddc','opq']
max = a_list[0]
for i in a_list[1:]:
if len(i)>len(max):
max = i
print(max)
我认为一个for循环就足够了:
a = ['abc', 'bcd', 'bcdefg', 'abba', 'cddc', 'opq']
longest_str_length = 0
longest_str = None
for i in a:
curr_length = len(i)
if curr_length > longest_str_length:
longest_str_length = curr_length
longest_str = i
print("Longest string is '{}'".format(longest_str))
我认为一个for循环就足够了:
a = ['abc', 'bcd', 'bcdefg', 'abba', 'cddc', 'opq']
longest_str_length = 0
longest_str = None
for i in a:
curr_length = len(i)
if curr_length > longest_str_length:
longest_str_length = curr_length
longest_str = i
print("Longest string is '{}'".format(longest_str))
为什么不使用max
?这是作业吗?为什么不使用max
?这是家庭作业吗?