小编use*_*101的帖子

重复列表的元素n次

如何重复列表的每个元素n次数并形成新列表?例如:

x=[1,2,3,4]
n=3

x1=[1,1,1,2,2,2,3,3,3,4,4,4]
Run Code Online (Sandbox Code Playgroud)

x*n 不起作用

for i in x[i]
    x1=n*x[i]
Run Code Online (Sandbox Code Playgroud)

必须有一个简单而聪明的方式.

python

39
推荐指数
5
解决办法
5万
查看次数

关闭对话框时的错误是什么

我只是学习PyQt而且我有一个小应用程序,似乎工作正常单位我点击对话框右上角的X关闭它.当我这样做并返回控制台时,我发现异常已经引发如下:

To exit: use 'exit', 'quit', or Ctrl-D.
An exception has occurred, use %tb to see the full traceback.

SystemExit: 0


In [2]: %tb
Traceback (most recent call last):

  File "<ipython-input-1-4524246fa84a>", line 1, in <module>
    runfile('C:/Users/21025/simpleAdder.pyw', wdir='C:/Users/21025')

  File "C:\Users\21035\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 586, in runfile
    execfile(filename, namespace)

  File "C:/Users/21035/simpleAdder.pyw", line 81, in <module>
    sys.exit(app.exec_())

SystemExit: 0
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么 ?该应用程序的代码如下所示:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return …
Run Code Online (Sandbox Code Playgroud)

python pyqt

3
推荐指数
1
解决办法
6626
查看次数

为什么同一语句打印两个不同的值?

当我想要理解python自我概念时,我遇到了这个我认为有用的例子.但是有一部分让我感到困惑.为什么print a.i 输出两个不同的值?在第一种情况下,输出5对我来说是有意义的.但后来几行同样的print a.i声明输出123.

def say_hi():
    return 'hi!'

i = 789

class MyClass(object):

    i = 5

    def prepare(self):
        i = 10
        self.i = 123
        print i

    def say_hi(self):
        return 'Hi there!'

    def say_something(self):
        print say_hi()

    def say_something_else(self):
        print self.say_hi()
Run Code Online (Sandbox Code Playgroud)

产量

>>> print say_hi()
hi!
>>> print i
789
>>> a = MyClass()
>>> a.say_something()
hi!
>>> a.say_something_else()
Hi there!
>>> print a.i
5
>>> a.prepare()
10
>>> print i
789
>>> print a.i …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
95
查看次数

标签 统计

python ×3

pyqt ×1