语法列表加入Python

时间:2013-11-07 14:52:22

标签: python list

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

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

6 个答案:

答案 0 :(得分:27)

这个表达式是这样的:

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

答案 1 :(得分: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"

答案 2 :(得分: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'

答案 3 :(得分: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

答案 4 :(得分:-1)

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

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

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

这也将在零情况下失败。

答案 5 :(得分:-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'])