【问题标题】:Print a list of items separated by commas but with the word "and" before the last item打印以逗号分隔但在最后一项之前带有单词“and”的项目列表
【发布时间】:2017-02-08 12:15:22
【问题描述】:

我已经为此工作了两天。这是作业的内容:

假设你有一个这样的列表值:listToPrint = ['apples', 'bananas', 'tofu', 'cats'] 编写一个程序,打印一个列表,其中所有项目用逗号和空格分隔,在最后一个项目之前插入“and”。例如,上面的列表将打印'apples, bananas, tofu, and cats'。但是您的程序应该能够使用任何列表,而不仅仅是上面显示的列表。因此,如果要打印的列表比上述列表短或长,您将需要使用循环。

这是我目前所拥有的:

listToPrint = []
while True:
    newWord = input("a, b, and c ")
    if newWord == "":
        break
    else:
        listToPrint.append(newWord)

【问题讨论】:

  • 您遇到的具体问题是什么?您在上面使用的while 循环应该按照您的描述进行。
  • 感谢您的回复瑞克。这就是我的想法,但这似乎太简单了。因此,如果我要添加到列表中,我将在哪里插入它,以及如何插入?我会简单地继续从输入中添加或减去吗?该列表不应该保留在某个全局列表中吗?我对此很陌生,所以我有点困惑。
  • 我知道作为初学者很难提出正确的问题,但我也很难理解您遇到的问题。每次循环发生时,输入行和listToPrint.append(newWord) 行都会执行。它们位于 while 循环的主体内。每次都会执行该主体,直到遇到 break。
  • 根据您编写的代码,listToPrint 已经是list 类型的全局变量。您可以通过添加print(type(listToPrint)) 行来确认这一点。
  • 啊哈。知道了。你为什么不更新你的问题,其中一些更明确一点?提供所需的输入、所需的输出和您尝试过的代码(您已经提供了)。

标签: python list python-3.x loops for-loop


【解决方案1】:

我会这样做,但如果这是为了学校,请小心。如果我在下面所做的任何事情都使用了尚未涵盖的功能或技术,你的导师会不屑一顾。

listToPrint = ['a', 'b', 'c']

def list_to_string(L, sep = '', last_sep = None):
    if last _sep is None:
        return sep.join(L)
    else:
        return sep.join(L[:-1]) + last_sep + L[-1]

print(list_to_string(listToPrint, sep = ', ', last_sep = ', and '))

这里有更多的初学者版本:

listToPrint = ['a', 'b', 'c']
list_length = len(listToPrint)

result = ""
count = 0
for item in listToPrint:
    count = count + 1
    if count == list_length:
        result = result + "and " + item
    else:
        result = result + item + ", "

这一项不适用于列表中的一项。

【讨论】:

    【解决方案2】:

    您显示的代码似乎解决了与您的作业要求您做的不同的问题。该任务的重点是printing 来自提供的list 的值,而您的代码完全是关于inputing 来自用户的项目并将它们放入列表中。做一个然后另一个可能是有意义的,但是对于您在 cmets 中给出的作业,input 代码完全无关紧要。

    这是我解决该作业的方法(可能使用您还不理解的代码):

    print("{}, and {}".format(", ".join(list_to_print[:-1]), list_to_print[-1]))
    

    更“对新手友好”的方法看起来更像这样:

    for item in list_to_print[:-1]:
        print(item, end=', ')
    print('and', list_to_print[-1])
    

    【讨论】:

    • 非常感谢,这个成功了。对于 list_to_print[:-1] 中的项目: print(item, end=', ') print('and', list_to_print[-1])
    【解决方案3】:

    初级版:

    x = ['apples', 'bananas', 'tofu', 'cats']
    
    print("'", end='')
    
    for i in range(len(x)-2):
    
        print(x[i], end=', ')
    
    print(str(x[-2])+' and '+str(x[-1]),end='')
    
    print("'")
    

    输出:'apples, bananas, tofu and cats'

    【讨论】:

      【解决方案4】:
      #printing the first element sep. so the list works     
      print(listToPrint[0], end="") 
      
      for i in range(1, len(listToPrint)-1):
      
          print("," + listToPrint[i], end="") #this prints the middle elements
      
      if(len(listToPrint) > 1):
      
          print( " and " + listToPrint[-1], end="")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-16
        • 2013-08-30
        • 2015-01-11
        • 1970-01-01
        • 1970-01-01
        • 2015-07-17
        • 1970-01-01
        相关资源
        最近更新 更多