擅长:python、mysql、java
<p>我用了不同的方法。我是个初学者,所以我不知道这是不是最干净的方法。对我来说,这似乎是最简单的方式:</p>
<pre><code>spam = ['apples', 'pizza', 'dogs', 'cats']
def comma(items):
for i in range(len(items) -2):
print(items[i], end=", ")# minor adjustment from one beginner to another: to make it cleaner, simply move the ', ' to equal 'end'. the print statement should finish like this --> end=', '
print(items[-2] + 'and ' + items[-1])
comma(spam)
</code></pre>
<p>它将给出输出:</p>
<pre><code>apples, pizza, dogs and cats
</code></pre>