假设我的答案在列表中的范围是0-5,目标是将范围转换为0-10。我编写了一个小的python函数(基于上一篇文章:Convert a number range to another range, maintaining ratio)来进行此转换,以继续进行分析管道的下一步。这里的主要问题是输出列表不包括零,而仅存储零以上的所有内容。 (它不能从输入到输出传递零)。
这是我用作输入的列表:
值 1个 0 0 0 0 0 0 3
现在是带有一些注释的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
import random
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
button = QtWidgets.QPushButton("Print me")
self.tableWidget = QtWidgets.QTableWidget()
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(button)
lay.addWidget(self.tableWidget)
self.fill_table()
QtWidgets.QShortcut(
self.tableWidget,
key=QtGui.QKeySequence(QtCore.Qt.Key_Return),
context=QtCore.Qt.WidgetShortcut,
activated=self.sum_selected
)
def fill_table(self):
self.tableWidget.setColumnCount(4)
self.tableWidget.setRowCount(0)
for i, width in enumerate((100, 130, 130, 130)):
self.tableWidget.setColumnWidth(i, width)
self.tableWidget.setHorizontalHeaderLabels(['Player Name', 'Total Amount', 'Time and Date'])
for i in range(40):
self.tableWidget.insertRow(self.tableWidget.rowCount())
it = QtWidgets.QTableWidgetItem(str(random.randint(100, 150)))
self.tableWidget.setItem(i, 1, it)
@QtCore.pyqtSlot()
def sum_selected(self):
result = sum([float(ix.data()) for ix in self.tableWidget.selectedIndexes() if ix.column() == 1])
r, c = 0, 3
it = self.tableWidget.item(r, c)
if it is None:
it = QtWidgets.QTableWidgetItem(str(result))
self.tableWidget.setItem(r, c, it)
else:
it.setText(str(result))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
这是输出
[1.0,5.5]
原始输入的零在哪里:
[1、0、0、0、0、0、0、3]
我想要一个类似的输出:
[1.0,0,,0,0,0,0,0 5.5]
答案 0 :(得分:1)
在if (OldRange == 0):
条件下,您没有在数组中添加零,您可能需要添加零,请参见以下代码:
if (OldRange == 0):
NewValue = NewMin
new_array.append(OldRange)
else:
# set the new range as new max - newmin
NewRange = (NewMax - NewMin)
# the new value is the current possition in the loop - the old min multiplied by the new range and divided by the old range + NewMin
NewValue = (((adf[position] - OldMin) * NewRange) / OldRange) + NewMin
# append the new value in every loop in the new_array
new_array.append(NewValue)