I have written a subclass of defaultdict , and I gave it its own __repr__ method in order to customize its look in an interactive session. In a regular Python session this works as expected: Python 3.5.0 (default, Sep 20 2015, 11:28:25) [GCC 5.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from collections import defaultdict >>> clas
我写了一个defaultdict的子类,为了在交互式会话中定制它的外观,我给了它自己的__repr__方法。 在常规的Python会话中,按预期工作: Python 3.5.0 (default, Sep 20 2015, 11:28:25) [GCC 5.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from collections import defaultdict >>> class Foo(defaultdict): ... def __repr__(self): ... return 'foo_
I'm tasked to make a "Set" class that contains the variable self.list and be able to print and str() the object by writing the __repr__ and __str__ methods. A second file (driver1.py), a "driver file" creates a Set object and attempts to call print(str(set_object)) and print(set_object) but both calls only print a memory address, Set.Set instance at 0x1033d1488> or som
我的任务是创建一个包含变量self.list的“Set”类,并通过编写__repr__和__str__方法来打印和str()该对象。 第二个文件(driver1.py),一个“驱动程序文件”创建一个Set对象并尝试调用print(str(set_object))和print(set_object),但这两个调用都只打印一个内存地址, Set.Set instance at 0x1033d1488>或其他某个位置。 我该如何改变这一点? 我希望它以{1,2,3}格式打印出set_object的内容 更新缩进后,这是我的代
It was my understanding that python will print the repr of the output, but this is apparently not always the case. For example: In ipython: In [1]: type([]) Out[1]: list In [2]: set([3,1,2]) Out[2]: {1, 2, 3} In python: >>> type([]) <type 'list'> >>> set([3,1,2]) set([1, 2, 3]) What transformation does ipython apply on the output? Instead of repr or standard ppri
我的理解是python会打印输出的repr ,但显然并不总是这样。 例如: 在ipython中: In [1]: type([]) Out[1]: list In [2]: set([3,1,2]) Out[2]: {1, 2, 3} 在python中: >>> type([]) <type 'list'> >>> set([3,1,2]) set([1, 2, 3]) ipython在输出上应用了什么转换? 代替repr或标准pprint模块IPython使用IPython.lib.pretty.RepresentationPrinter.pretty方法来打印输出。 Module IPytho
I have been working on a large assignment and I'm almost finished except I need help writing the __str__ and __repr__ functions of a Set container. I have never done this and I have no clue what to do. Searching the internet, I'm still stuck. I've tried something like: '%s(%r)' % (self.__class__, self) I need to print out a representation like this: 'set([ELEMENT_1, ELEMENT_2,
我一直在做一个大型的任务,我几乎完成了,除了我需要帮助编写Set容器的__str__和__repr__函数。 我从来没有这样做,我不知道该怎么做。 在互联网上搜索,我仍然坚持。 我试过类似的东西: '%s(%r)' % (self.__class__, self) 我需要打印出如下所示的图像: 'set([ELEMENT_1, ELEMENT_2,..., ELEMENT_N])' 我的元素存储在我编写集合容器的数组类中。 我用自己for item in self或if item in self for item in self访问
Lately, I've had lots of trouble with __repr__() , format() , and encodings. Should the output of __repr__() be encoded or be a unicode string? Is there a best encoding for the result of __repr__() in Python? What I want to output does have non-ASCII characters. I use Python 2.x, and want to write code that can easily be adapted to Python 3. The program thus uses # -*- coding: utf-8 -*-
最近,我在__repr__() , format()和编码方面遇到了很多麻烦。 __repr__()的输出是否应该被编码或是一个unicode字符串? Python中的__repr__()的结果是否有最好的编码? 我想要输出的确有非ASCII字符。 我使用Python 2.x,并希望编写可轻松适应Python 3的代码。该程序因此使用 # -*- coding: utf-8 -*- from __future__ import unicode_literals, print_function # The 'Hello' literal represents a Unicode object 以
Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations? The following uses each method and has the same outcome, so what is the difference? #!/usr/bin/python sub1 = "python string!" sub2 = "an arg" a = "i am a %s" % sub1 b = "i am a {0}".format(sub1) c = "with %(kwarg)s!" % {'kwarg':sub2} d = "with
Python 2.6引入了str.format()方法,其语法与现有的%运算符略有不同。 哪个更好,哪些情况? 以下使用每种方法,并有相同的结果,所以有什么区别? #!/usr/bin/python sub1 = "python string!" sub2 = "an arg" a = "i am a %s" % sub1 b = "i am a {0}".format(sub1) c = "with %(kwarg)s!" % {'kwarg':sub2} d = "with {kwarg}!".format(kwarg=sub2) print a # "i am a python string!" print b # "i am a python
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers Use the index() function of lists: Sentence = ["she" , "sells" , "sea" , "shells" ,"on" , "the", "sea" , "shore"] print Sentence.index("sea") The index of lists is zero-based, therefore the result is 2 .
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 使用列表的index()函数: Sentence = ["she" , "sells" , "sea" , "shells" ,"on" , "the", "sea" , "shore"] print Sentence.index("sea") 列表的索引是基于零的,因此结果是2 。
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers I think your looking something like the below example my_list = [1, 3, 5] number = input("Choose a number from 1 to 5: ") if int(number) in my_list: print("error. . . . ") else: print(int(number)) And when you run it this is how it works Choose a number from 1 to
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 我认为你看起来像下面的例子 my_list = [1, 3, 5] number = input("Choose a number from 1 to 5: ") if int(number) in my_list: print("error. . . . ") else: print(int(number)) 当你运行它时,它就是这样工作的 从1到5:2选择一个数字 2 从1到5:1中选择一个数字 错误。 。 。 。 首先,您需要将用户输入视为int ,然
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers >>> a=[2,3,4] >>> a.index(3) 1 编辑 >>> a=[2,3,4] >>> def check(n): ... try: ... print a.index(n) ... except ValueError: ... print "Element not found in list a" ... >>> check(5) Element not found in
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 >>> a=[2,3,4] >>> a.index(3) 1 编辑 >>> a=[2,3,4] >>> def check(n): ... try: ... print a.index(n) ... except ValueError: ... print "Element not found in list a" ... >>> check(5) Element not found in list a >>> check(3) 1
This question already has an answer here: Finding the index of an item given a list containing it in Python 23 answers To get the "ordinal" positions of search string jake in the input string use the following approach: mystr = "there not what is jake can do for you ask what you play do for spare jake" search_str = 'jake' result = [i+1 for i,w in enumerate(mystr.split()) if w.lowe
这个问题在这里已经有了答案: 在Python中找到一个包含它的列表的索引23个答案 要在输入字符串中获得搜索字符串jake的“序号”位置,请使用以下方法: mystr = "there not what is jake can do for you ask what you play do for spare jake" search_str = 'jake' result = [i+1 for i,w in enumerate(mystr.split()) if w.lower() == search_str] print(result) 输出: [5, 17] enumerate(mystr.split()) - 获取枚举对