26

加入列表的最pythonic方法是什么,以便每个项目之间有逗号,除了最后一个使用“and”的项目?

["foo"] --> "foo"
["foo","bar"] --> "foo and bar"
["foo","bar","baz"] --> "foo, bar and baz"
["foo","bar","baz","bah"] --> "foo, bar, baz and bah"
4

6 回答 6

30

这个表达式做到了:

print ", ".join(data[:-2] + [" and ".join(data[-2:])])

如此处所示:

>>> data
    ['foo', 'bar', 'baaz', 'bah']
>>> while data:
...     print ", ".join(data[:-2] + [" and ".join(data[-2:])])
...     data.pop()
...
foo, bar, baaz and bah
foo, bar and baaz
foo and bar
foo
于 2013-11-07T15:06:45.613 回答
14

试试这个,它考虑了边缘情况和用途format(),以展示另一种可能的解决方案:

def my_join(lst):
    if not lst:
        return ""
    elif len(lst) == 1:
        return str(lst[0])
    return "{} and {}".format(", ".join(lst[:-1]), lst[-1])

按预期工作:

 my_join([])
=> ""
 my_join(["x"])
=> "x"
 my_join(["x", "y"])
=> "x and y"
 my_join(["x", "y", "z"])
=> "x, y and z"
于 2013-11-07T15:04:02.260 回答
5

基于评论的修复导致了这种有趣的方式。它假设要连接的列表的字符串条目中没有逗号(无论如何这都会有问题,所以这是一个合理的假设。)

def special_join(my_list):
    return ", ".join(my_list)[::-1].replace(",", "dna ", 1)[::-1]


In [50]: def special_join(my_list):
        return ", ".join(my_list)[::-1].replace(",", "dna ", 1)[::-1]
   ....:

In [51]: special_join(["foo", "bar", "baz", "bah"])
Out[51]: 'foo, bar, baz and bah'

In [52]: special_join(["foo"])
Out[52]: 'foo'

In [53]: special_join(["foo", "bar"])
Out[53]: 'foo and bar'
于 2013-11-07T14:56:37.950 回答
1

已经有了很好的答案。这个适用于所有测试用例,并且与其他一些测试用例略有不同。

def grammar_join(words):
    return reduce(lambda x, y: x and x + ' and ' + y or y,
                 (', '.join(words[:-1]), words[-1])) if words else ''

tests = ([], ['a'], ['a', 'b'], ['a', 'b', 'c'])
for test in tests:                                 
    print grammar_join(test)

a
a and b
a, b and c
于 2013-11-07T15:56:57.100 回答
-1

只是最后一个的特例。像这样的东西:

'%s and %s'%(', '.join(mylist[:-1]),mylist[-1])

可能不会有任何更简洁的方法。

这在零情况下也会失败。

于 2013-11-07T14:57:58.230 回答
-1

如果您需要不支持负索引的解决方案(即 Django QuerySet)

def oxford_join(string_list):
    if len(string_list) < 1:
        text = ''
    elif len(string_list) == 1:
        text = string_list[0]
    elif len(string_list) == 2:
        text = ' and '.join(string_list)
    else:
        text = ', '.join(string_list)
        text = '{parts[0]}, and {parts[2]}'.format(parts=text.rpartition(', '))  # oxford comma
    return text

oxford_join(['Apples', 'Oranges', 'Mangoes'])
于 2015-09-22T04:51:22.017 回答