简体   繁体   English

请解释一下这个python代码的含义是什么?

[英]Please explain to me what this python code means?

I still learn python but this code seems beyond my level. 我仍然学习python,但这段代码似乎超出了我的水平。 what does it means? 这是什么意思?

 pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]

You can convert any list comprehension to an equivalent explicit loop like this: 您可以将任何列表推导转换为等效的显式循环,如下所示:

pairs = []
for s1 in qs.split('&'):
    for s2 in s1.split(';'):
        pairs.append(s2)

The rule is to take all of the for and if clauses, nest them in the order they appear, and then append(foo) for whatever foo comes before the first clause. 规则是取所有forif子句,按照它们出现的顺序嵌套它们,然后为第一个子句之前的foo append(foo)

The tutorial section on List Comprehension (and the subsection on Nested List Comprehensions) explains this… but it doesn't give you the simple rule for converting any comprehension into a nested block statement, which (in my opinion) makes it much easier to understand all but the trivial cases. 了解列表理解的教程部分(以及嵌套列表理解的子部分)解释了这一点......但是它并没有为您提供将任何理解转换为嵌套块语句的简单规则,这在我看来更容易理解除了琐碎的案件之外的所有案件。

It's also worth noting that urllib.parse.parse_qsl (or urlparse.parse_qsl in 2.x) is a better way to parse query strings. 值得注意的是, urllib.parse.parse_qsl (或2.x中的urlparse.parse_qsl )是解析查询字符串的更好方法。 Besides the fact that it doesn't involve a hard-to-read nested list comprehension, it also properly handles all kinds of things (like quoting) that you wouldn't think about in advance, and will end up debugging for one of your users who doesn't know how to submit useful bug reports. 除了它不涉及难以阅读的嵌套列表理解这一事实外,它还可以正确处理您不会事先考虑的各种事情(如引用),并最终调试其中一个不知道如何提交有用的错误报告的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM