在 pyqt 中,我有一个用户可以编辑的 qtableview。如果用户对表进行了更改,则在用户完成后将复制该表。如果未进行任何更改,则跳过该表。该表充满了空字符串,即:
table = [["","",""],["","",""]]
Run Code Online (Sandbox Code Playgroud)
我想检查该表是否仅包含"",如果包含,则忽略它。如果它没有,即包含 a "1",一些代码会在列表中运行。现在我有一个工作代码,但它不是很 Pythonic,我想知道如何改进它。我的代码如下:
tabell1 = [["","",""],["","",""]]
meh = 0
for item in self.tabell1:
for item1 in item:
if item1 == "":
pass
else:
meh = 1
if meh == 1:
do the thing
Run Code Online (Sandbox Code Playgroud) 如果有人能帮忙解决这个问题,我将非常感激,因为这几个小时让我有点疯狂!
我有一个 ndarray 如下:
array([[[0, 0],
[0, 2],
[0, 4]],
[[1, 0],
[1, 2],
[1, 4]]])
Run Code Online (Sandbox Code Playgroud)
我想将其转换为记录数组:
array([[(0, 0),
(0, 2),
(0, 4)],
[(1, 0),
(1, 2),
(1, 4)]],
dtype=[('x', '<i4'), ('y', '<i4')])
Run Code Online (Sandbox Code Playgroud) from Tkinter import *
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.bttnClicks = 0
self.createWidgets()
def createWidgets(self):
self.bttn = Button(self)
self.bttn["text"] = "number of clicks"
self.bttn["command"] = self.upadteClicks
self.bttn.grid()
def upadteClicks(self):
self.bttnClicks += 1
self.bttn["text"] = "number of clicks " + str(self.bttnClicks)
root = Tk()
root.title("button that do something")
root.geometry("400x200")
app = Application(root)
root.mainloop()`
Run Code Online (Sandbox Code Playgroud)
这就是错误:
super(Application, self).__init__(master)
TypeError: super() argument 1 must be type, not classobj
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?该代码在 python 3.XX 中运行良好,但在 python 2.XX 中则不然。
以下是y基于传感器(列x)的值计算的距离(列).
x y
----------
-51.61 ,1.5
-51.61 ,1.5
-51.7 ,1.53
-51.91 ,1.55
-52.28 ,1.62
-52.35 ,1.63
-52.49 ,1.66
-52.78 ,1.71
-52.84 ,1.73
-52.90 ,1.74
-53.21 ,1.8
-53.43 ,1.85
-53.55 ,1.87
-53.71 ,1.91
-53.99 ,1.97
-54.13 ,2
-54.26 ,2.03
-54.37 ,2.06
-54.46 ,2.08
-54.59 ,2.11
-54.89 ,2.19
-54.94 ,2.2
-55.05 ,2.23
-55.11 ,2.24
-55.17 ,2.26
Run Code Online (Sandbox Code Playgroud)
我想曲线拟合找到基于此函数的常量a和b数据test.txt:
Function y = 10^((a-x)/10*b)
Run Code Online (Sandbox Code Playgroud)
我使用以下代码:
import math
from numpy import genfromtxt
from …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,其中将方程式作为字符串输入,然后求值。到目前为止,我已经提出了:
test_24_string = str(input("Enter your answer: "))
test_24 = eval(test_24_string)
Run Code Online (Sandbox Code Playgroud)
我既需要此方程式的字符串版本,也需要评估的版本。但是,这eval是非常危险的功能。int()但是,使用不起作用,因为这是一个方程式。是否存在一个Python函数,该函数将评估字符串中的数学表达式,就像输入数字一样?
class my_class(object):
def __init__(self):
self.ref = 0
self.ask = 0
self.added = self.ref + self.ask
inst = my_class()
inst.ref = 5
inst.ask = 7
print(inst.ref + inst.ask)
print(inst.added)
Run Code Online (Sandbox Code Playgroud)
我想print(inst.added)返回12但它返回0
我知道如果列表中的元素没有特定的大小,如何删除它:
x = [[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2],[1,2,3],[1,2,3],[1,2,3,4]]
y = [s for s in x if len(s) == len(x[0])]
Run Code Online (Sandbox Code Playgroud)
x原始列表在哪里,y是新列表.正如你在第一个中看到的那样,有一个条目不像其他条目那么长,而一个条目比其他条目长.
我想删除一个元素,每次它与列表中的大多数元素的长度不同.只要列表中的第一个元素与大多数元素具有相同的长度,显示的方法就可以工作.
所以问题是如何获得最常见的元素长度?没有循环迭代的长度.平均值不起作用,因为平均值不代表大部分长度而是元素的平均长度(例如长度3,3,3,30将给出平均值~10,而长度的主要值为3).
我想要一个易于读取访问多维numpy数组的某些部分.对于任何访问第一个维度的数组都是easy(b[index]).另一方面,访问第六维是"硬"(特别是阅读).
b[:,:,:,:,:,index] #the next person to read the code will have to count the :
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?特别是有一种方法,在编写程序时轴是不知道的吗?
编辑:索引维度不一定是最后一个维度
我试图填补我对蟒蛇的理解中的空白property().以下是我提出的代码,以了解property():
class Temperature:
def __init__(self, temp = 10):
self.set_temp(temp)
def to_farenheit(self):
return (self._temp * 1.8) + 32
def get_temp(self):
print "getting temperature value"
return self._temp
def set_temp(self, temp):
print "setting temperature value"
if temp < -237:
raise ValueError("this shud be above -273")
else:
self._temp = temp
temps = property(get_temp, set_temp)
Run Code Online (Sandbox Code Playgroud)
我执行上面的类并执行以下操作:
>>> t = Temperature()
setting temperature value
>>> t.temps
getting temperature value
10
>>> t.temps = 13
>>> t.temps
13
>>> t.get_temp()
getting temperature value …Run Code Online (Sandbox Code Playgroud) 我想创建一个包含[0.000,1.000]范围内所有浮点数的数组,所有浮点数都是3位小数/ 4位精度.
例如
>>> np.arange(start=0.000, stop=1.001, decimals=3)
[0.000, 0.001, ..., 0.100, 0.101, ..., 0.900, ..., 0.999, 0.000]
Run Code Online (Sandbox Code Playgroud)
可以做一些与此相关的事情吗?
python ×10
numpy ×4
arrays ×2
python-2.7 ×2
class ×1
equation ×1
indexing ×1
list ×1
nested-lists ×1
oop ×1
precision ×1
properties ×1
python-2.x ×1
python-3.x ×1
scipy ×1
string ×1
super ×1
tkinter ×1