Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在非此即彼的情况下提供一组值?_Python_Algorithm - Fatal编程技术网

Python 如何在非此即彼的情况下提供一组值?

Python 如何在非此即彼的情况下提供一组值?,python,algorithm,Python,Algorithm,我正在寻找一种更简洁的方法来编写lookup\u和\u url函数。我认为必须有一种更简洁的方式来声明和实现我的逻辑。我已尽最大努力准确描述代码本身的外部功能和内部功能: def render(opener='corp'): """render will render a carousel of videos, with the opener being corp or uk and the follower being the other""" def lookup_

我正在寻找一种更简洁的方法来编写
lookup\u和\u url
函数。我认为必须有一种更简洁的方式来声明和实现我的逻辑。我已尽最大努力准确描述代码本身的外部功能和内部功能:

def render(opener='corp'):
    """render will render a carousel of videos, with the opener being
    corp or uk and the follower being the other"""

    def lookup_and_url():
        """"lookup_and_url() returns the DOM id and url for the
        opening video (the opener) and the video to follow (the follower).

        It returns a dictionary that allows a loop to make a DOM
        lookup of the id of the HTML element and then replace its
        `src attribute with the provided URL
        """

        src = dict(
            corp='http://www.youtube.com/embed/0lrqEGlu0Fo',
            uk='http://www.youtube.com/embed/30MfCTLhdZ4'
        )

        if opener == 'corp':
            return dict(
                opener = dict(lookup='opener', src=src['corp']),
                follower = dict(lookup='follower', src=src['uk'])
            )
        else:
            return dict(
                opener = dict(lookup='opener', src=src['uk']),
                follower = dict(lookup='follower', src=src['corp'])
            )

    lookup_and_src = lookup_and_url()

    for replace in lookup_and_src:
        root.findmeld(lookup_and_src['lookup']).attributes(src=lookup_and_src['src'])

看起来你的内部函数所做的唯一一件事就是选择你的两个URL中的哪个是“开启者”,哪个是“跟随者”。构建嵌套字典所做的所有工作都是毫无意义的。我认为你可以让这件事简单得多:

def render(opener='corp'):
    corp = 'http://www.youtube.com/embed/0lrqEGlu0Fo',
    uk = 'http://www.youtube.com/embed/30MfCTLhdZ4'

    o, f = (corp, uk) if opener == "corp" else (uk, corp)

    root.findmeld('opener').attributes(src=o)
    root.findmeld('follower').attributes(src=f)

我认为,如果没有所有的字典,这会更容易理解。

似乎你的内部函数所做的唯一一件事就是选择两个URL中的哪个是“开启者”,哪个是“跟随者”。构建嵌套字典所做的所有工作都是毫无意义的。我认为你可以让这件事简单得多:

def render(opener='corp'):
    corp = 'http://www.youtube.com/embed/0lrqEGlu0Fo',
    uk = 'http://www.youtube.com/embed/30MfCTLhdZ4'

    o, f = (corp, uk) if opener == "corp" else (uk, corp)

    root.findmeld('opener').attributes(src=o)
    root.findmeld('follower').attributes(src=f)

如果没有所有的字典,我想这会更容易理解。

如果你将以下内容提取到另一本字典中(如果你有两个以上的条目),不是会更容易吗


如果你将以下内容提取到另一本词典中(如果你有两个以上的条目),不是更容易吗


它可以缩短,但请记住,即使你可以写得更简洁,它现在也非常清楚和明确。没有一个程序员,不管他们是不是新手,理解这段代码都会有困难,这一点非常重要。仅仅因为一段代码不“聪明”,并不意味着它不好。我可能会让代码保持原样——python喜欢显式和清晰。我不理解最后的循环。您不使用从循环中获得的
replace
值,因此每次都执行完全相同的操作。还有,是否有理由到处使用字典?看起来您可以简单地拥有两个变量,并根据函数的参数按一种顺序或另一种顺序分配它们。但是由于循环没有意义,我不知道那会做什么,确切地说。@Blckknght也许他的意思是
root.findmeld(replace['lookup']).attributes(src=replace['src'])
@groovy:我想可能是这样,但那不起作用,因为循环在字典的键上,而不是在值上(而且,它的顺序是任意的).@groovy,没错。非常好,Blackknghttit可以缩短,但请记住,即使你可以写得更简洁,它现在也非常清晰和明确。没有一个程序员,不管他们是不是新手,理解这段代码都会有困难,这一点非常重要。仅仅因为一段代码不“聪明”,并不意味着它不好。我可能会让代码保持原样——python喜欢显式和清晰。我不理解最后的循环。您不使用从循环中获得的
replace
值,因此每次都执行完全相同的操作。还有,是否有理由到处使用字典?看起来您可以简单地拥有两个变量,并根据函数的参数按一种顺序或另一种顺序分配它们。但是由于循环没有意义,我不知道那会做什么,确切地说。@Blckknght也许他的意思是
root.findmeld(replace['lookup']).attributes(src=replace['src'])
@groovy:我想可能是这样,但那不起作用,因为循环在字典的键上,而不是在值上(而且,它的顺序是任意的).@groovy,没错。非常好,抓住布莱克·恩赫特尼斯。我可以把事情弄得更紧一些,因为字典键与
lookup
的值相同,只需将
lookup
全部删除。return dict(opener=src[opener],follower=src[followers[opener]]很好。我可以把事情搞得更紧一些,因为字典键与
lookup
的值相同,只需将
lookup
一起删除即可。return dict(opener=src[opener],follower=src[followers[opener]]