【问题标题】:Get the longest string from the list using python使用python从列表中获取最长的字符串
【发布时间】:2020-04-29 18:53:40
【问题描述】:

我试图在我的列表中打印“最长的字符串”。 我不想使用 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<=p:
        k=p
        r=b_list.index(p)
        print("Shortest string : " ,a_list[r])
        break

这是最短的字符串输出,当然,除了'abc'还有一个长度为3的项目,但我首先输入了break,以便列表索引只打印最小值。

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:
        k=p
    r=b_list.index(p)
    print("longest string : " ,a_list[r])
    break

这是打印最长字符串的结果。有什么问题? 再一次,我想在不使用 min(),max(),sort() 方法的情况下解决它。

【问题讨论】:

  • 为什么不使用max?这是作业吗?

标签: python


【解决方案1】:

您可以通过以下方式找出列表中最长和最短的字符串:

a_list = ['abc', 'bcd', 'bcdefg', 'abba', 'cddc', 'opq']

# we give the both variable the value of the first element in the list 
shortest = a_list[0]  
longest = a_list[0]  

for i in a_list[1:]:
    if len(shortest) >= len(i):  # check if current string in shorter 
        shortest = i # if yes change variable value

    if len(longest) <= len(i):  # check if current string in longer
        longest = i # if yes change variable value

# print results:
print(shortest)
print(longest)

【讨论】:

    【解决方案2】:

    您分配给 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
    

    P.S: 你最短的字符串代码也有同样的问题。它恰好适用于该特定输入

    【讨论】:

      【解决方案3】:
      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_element 是您的最大长度元素

      【讨论】:

        【解决方案4】:

        这是一种非常复杂的方法。 @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)
        

        【讨论】:

          【解决方案5】:

          您可以将第一个元素定义为最大值并与其余元素进行比较,如果找到新的最大值则替换最大值。

          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)
          

          【讨论】:

            【解决方案6】:

            我认为一个 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))
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多