[英]Finding documentation on python “native” types, e.g. set
I'm trying to learn Python, and, while I managed to stumble on the answer to my current problem, I'd like to know how I can better find answers in the future. 我正在尝试学习Python,尽管我设法迷失了对当前问题的答案,但我想知道将来如何更好地找到答案。
My goal was to take a list of strings as input, and return a string whose characters were the union of the characters in the strings, eg 我的目标是将字符串列表作为输入,并返回一个字符串,该字符串的字符是字符串中字符的并集,例如
unionStrings( ("ab", "bc"))
would return "abc". 将返回“ abc”。
I implemented it like this: 我是这样实现的:
def unionStrings( strings ):
# Input: A list of strings
# Output: A string that is the (set) union of input strings
all = set()
for s in strings:
all = all.union(set(s))
return "".join(sorted(list(all)))
I felt the for loop was unnecessary, and searched for more neater, more pythonic(?), improvements . 我觉得for循环是不必要的,并寻求更整洁,更多的pythonic(?)改进。
First question : I stumbled on using the class method set.union()
, instead of set1.union(set2)
. 第一个问题 :我偶然发现使用类方法set.union()
而不是set1.union(set2)
。 Should I have been able to find this in the standard Python docs? 我应该能够在标准Python文档中找到它吗? I've not been able to find it there. 我无法在那找到它。
So I tried using set.union() like this: 所以我尝试像这样使用set.union():
>>> set.union( [set(x) for x in ("ab","bc")] )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'union' requires a 'set' object but received a 'list'
Again, I stumbled around and finally found that I should be calling it like this: 我再次跌跌撞撞,终于发现我应该这样称呼它:
>>> set.union( *[set(x) for x in ("ab","bc")] )
set(['a', 'c', 'b'])
Second question : I think this means that set.union
is (effectively) declared as 第二个问题 :我认为这意味着set.union
被(有效地)声明为
set.union( *sets)
and not 并不是
set.union( setsList )
Is that correct? 那是对的吗? (I'm still learning how to use splat '*'.) (我仍在学习如何使用splat'*'。)
Third question : Where could I find documentation on the signature of set.union()
? 第三个问题 :在哪里可以找到有关set.union()
签名的文档? I didn't see it in the set/freezeset doc's, and I couldn't get the inspect
module to give me anything. 我在set / freezeset文档中没有看到它,也无法获取inspect
模块给我任何东西。 I'm not even sure set
is a module, it seems to be a type. 我什至不确定set
是一个模块,它似乎是一种类型。 Is it defined in a module, or what? 是在模块中定义的还是什么?
Thanks for reading my complicated question. 感谢您阅读我的复杂问题。 It's more "How do I navigate Python documentation?" 更多的是“如何浏览Python文档?” than "How do I do this in Python code?". 而不是“如何在Python代码中执行此操作?”。
Responding to jonrsharpe's comment: 回应jonrsharpe的评论:
Ohhhhh! 哦! I'm so used to C++ where you define separate static and instance methods. 我很习惯在C ++中定义单独的静态方法和实例方法。 Now that you explain it I can really see what's happening. 现在,您已经解释了,我真的可以看到发生了什么。
The only thing I might do different is write it as 我唯一可能做的不同的是将其写为
t = set.union( *[set(x) for x strings] )
return "".join(sorted(t))
because it bugs me to treat strings[0]
differently from the strings in strings[1:]
when, functionally, they don't play different roles. 因为在功能上它们没有扮演不同的角色时,我觉得把strings[0]
与strings[1:]
中的strings[1:]
区别开来会困扰我。 If I have to call set()
on one of them, I'd rather call it on all of them, since union()
is going to do it anyways. 如果我必须在其中之一上调用set()
,我宁愿在所有它们上调用它,因为union()
无论如何都会这样做。 But that's just style, right? 但这只是样式,对不对?
There are several questions here. 这里有几个问题。 Firstly, you should know that: 首先,您应该知道:
Class.method(instance, arg)
is equivalent to: 等效于:
instance.method(arg)
for instance methods . 例如方法 。 You can call the method on the class and explicitly provide the instance, or just call it on the instance. 您可以在类上调用方法并显式提供实例,也可以仅在实例上调用它。
For historical reasons, many of the standard library and built-in types don't follow the UppercaseWords
convention for class names, but they are classes . 由于历史原因,许多标准库和内置类型都不遵循类名的UppercaseWords
约定,但它们是class 。 Therefore 因此
set.union(aset, anotherset)
is the same as 是相同的
aset.union(anotherset)
set
methods specifically can be tricky, because of the way they're often used. set
方法特别容易,因为它们经常被使用。 set.method(arg1, arg2, ...)
requires arg1
to already be a set
, the instance for the method, but all the other arguments will be converted (from 2.6 on). set.method(arg1, arg2, ...)
要求arg1
已经是set
,方法的实例,但是所有其他参数都将被转换(从2.6开始)。
This isn't directly covered in the set
docs, because it's true for everything; set
文档中没有直接涉及到这一点,因为对于所有情况都是如此。 Python is pretty consistent. Python非常一致。
In terms of needing to "splat", note that the docs say: 在需要“啪啪”方面,请注意文档说:
union(other, ...)
rather than 而不是
union(others)
ie each iterable is a separate argument, hence you need to unpack your list of iterables. 也就是说,每个可迭代对象都是一个单独的参数,因此您需要解压缩可迭代对象列表。
Your function could therefore be: 因此,您的功能可能是:
def union_strings(strings):
if not strings:
return ""
return "".join(sorted(set(strings[0]).union(*strings[1:])))
or, avoiding the special-casing of strings[0]
: 或者,避免使用特殊的strings[0]
:
def union_strings(strings):
if not strings:
return ""
return "".join(sorted(set.union(*map(set, strings))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.