语法列表加入 Python 中


加入

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

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

这个表达式是这样做的:

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

试试这个,它考虑了边缘情况并使用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"

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

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'

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

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

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

'%s and %s'%(', '.join(mylist[:-1]),mylist[-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'])

最新更新