Python题库

Posted ---wunian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python题库相关的知识,希望对你有一定的参考价值。

Date:2018-05-08

1、Given: an array containing hashes of names

Return: a string formatted as a list of names separated by commas except for the last two names, which should be separated by an ampersand.

Example:

namelist([ {‘name‘: ‘Bart‘}, {‘name‘: ‘Lisa‘}, {‘name‘: ‘Maggie‘} ])
# returns ‘Bart, Lisa & Maggie‘

namelist([ {‘name‘: ‘Bart‘}, {‘name‘: ‘Lisa‘} ])
# returns ‘Bart & Lisa‘

namelist([ {‘name‘: ‘Bart‘} ])
# returns ‘Bart‘

namelist([])
# returns ‘‘

Best Practices:

def namelist(names):
    if len(names) > 1:
        return ‘{} & {}‘.format(‘, ‘.join(name[‘name‘] for name in names[:-1]), 
                                names[-1][‘name‘])
    elif names:
        return names[0][‘name‘]
    else:
        return ‘‘

My solutions:

def namelist(names):
    #your code here
    if len(names) > 1:
        first_name = ‘, ‘.join(tmp_name[‘name‘] for tmp_name in names[:-1])
        last_name = names[-1][‘name‘]
        print(first_name)
        print(last_name)
        return first_name + ‘ & ‘ + last_name
    elif names:
        return names[0][‘name‘]
    else:
        return ‘‘

以上是关于Python题库的主要内容,如果未能解决你的问题,请参考以下文章

常用python日期日志获取内容循环的代码片段

Python题库

python 有用的Python代码片段

Python 向 Postman 请求代码片段

python [代码片段]一些有趣的代码#sort

使用 Python 代码片段编写 LaTeX 文档