在两个函数之间传递变量
Passing a variable between two functions
以下是我一直在研究的一段代码。我已经能够毫无错误地编译和 运行 代码。但是,我很难在我的代码中将变量从一个函数传递到另一个函数。
问题似乎发生在我 运行 choose()
并根据所需索引创建 self.newLists
之后。您会注意到我在此函数的末尾添加了 print(self.newLists)
,以便我可以检查它是否生成了我想要的结果。
下一个函数 simplify()
是我的问题所在。当我尝试从上一个函数传递 self.newLists
时,它似乎没有产生任何结果。我还尝试打印 and/or 返回名为 answer
的变量,但它 returns "none"。我已经在这个障碍上磕磕绊绊了一段时间,没有任何进展。下面是我正在处理的代码以及我希望 simplify()
生成的示例。
from tkinter import *
from tkinter.filedialog import askopenfilename
class myFileOpener:
def __init__(self, master):
frame = Frame(master)
frame.pack()
print()
self.newLists = ()
self.printButton = Button(frame, text="Select File", command=self.openfile)
self.printButton.pack(side=LEFT)
self.runButton = Button(frame, text="Run", command=self.combine)
self.runButton.pack(side=LEFT)
self.quitButton = Button(frame, text="Quit", command=frame.quit)
self.quitButton.pack(side=LEFT)
def openfile(self):
filename = askopenfilename(parent=root)
self.lines = open(filename)
# print(self.lines.read())
def choose(self):
g = self.lines.readlines()
for line in g:
matrix = line.split()
JD = matrix[2]
mintime = matrix[5]
maxtime = matrix[7]
self.newLists = [JD, mintime, maxtime]
print(self.newLists)
def simplify(self):
dates = {}
for sub in self.newLists:
date = sub[0]
if date not in dates:
dates[date] = []
dates[date].extend(sub[1])
answer = []
for date in sorted(dates):
answer.append([date] + dates[date])
return answer
def combine(self):
self.choose()
self.simplify()
root = Tk()
b = myFileOpener(root)
root.mainloop()
simplify()
的所需输出示例:
[['2014-158', '20:07:11.881', '20:43:04.546', '20:43:47.447', '21:11:08.997', '21:11:16.697', '21:22:07.717'],
['2014-163', '17:12:09.071', '17:38:08.219', '17:38:28.310', '17:59:25.649', '18:05:59.536', '18:09:53.243', '18:13:47.671', '18:16:53.976', '18:20:31.538', '18:23:02.243']]
它本质上是按特定日期对时间进行分组。
您不是在生成列表列表。您正在 将每个循环迭代 self.newLists
重置为包含 3 个元素的单个列表:
for line in g:
matrix = line.split()
JD = matrix[2]
mintime = matrix[5]
maxtime = matrix[7]
self.newLists = [JD, mintime, maxtime]
您需要改为使用 list.append()
将这 3 个元素添加到您在循环外设置 一次 的列表:
self.newLists = []
for line in g:
matrix = line.split()
JD = matrix[2]
mintime = matrix[5]
maxtime = matrix[7]
self.newLists.append([JD, mintime, maxtime])
您的 simplify
方法正在将 mintime
的 单个字符 添加到您的输出列表中:
for sub in self.newLists:
date = sub[0]
if date not in dates:
dates[date] = []
dates[date].extend(sub[1])
您想在那里使用 list.append()
,而不是 list.extend()
。可以使用 dict.setdefault()
简化该循环,而不是手动测试密钥:
for date, mintime, maxtime in self.newLists:
dates.setdefault(date, []).append(mintime)
以下是我一直在研究的一段代码。我已经能够毫无错误地编译和 运行 代码。但是,我很难在我的代码中将变量从一个函数传递到另一个函数。
问题似乎发生在我 运行 choose()
并根据所需索引创建 self.newLists
之后。您会注意到我在此函数的末尾添加了 print(self.newLists)
,以便我可以检查它是否生成了我想要的结果。
下一个函数 simplify()
是我的问题所在。当我尝试从上一个函数传递 self.newLists
时,它似乎没有产生任何结果。我还尝试打印 and/or 返回名为 answer
的变量,但它 returns "none"。我已经在这个障碍上磕磕绊绊了一段时间,没有任何进展。下面是我正在处理的代码以及我希望 simplify()
生成的示例。
from tkinter import *
from tkinter.filedialog import askopenfilename
class myFileOpener:
def __init__(self, master):
frame = Frame(master)
frame.pack()
print()
self.newLists = ()
self.printButton = Button(frame, text="Select File", command=self.openfile)
self.printButton.pack(side=LEFT)
self.runButton = Button(frame, text="Run", command=self.combine)
self.runButton.pack(side=LEFT)
self.quitButton = Button(frame, text="Quit", command=frame.quit)
self.quitButton.pack(side=LEFT)
def openfile(self):
filename = askopenfilename(parent=root)
self.lines = open(filename)
# print(self.lines.read())
def choose(self):
g = self.lines.readlines()
for line in g:
matrix = line.split()
JD = matrix[2]
mintime = matrix[5]
maxtime = matrix[7]
self.newLists = [JD, mintime, maxtime]
print(self.newLists)
def simplify(self):
dates = {}
for sub in self.newLists:
date = sub[0]
if date not in dates:
dates[date] = []
dates[date].extend(sub[1])
answer = []
for date in sorted(dates):
answer.append([date] + dates[date])
return answer
def combine(self):
self.choose()
self.simplify()
root = Tk()
b = myFileOpener(root)
root.mainloop()
simplify()
的所需输出示例:
[['2014-158', '20:07:11.881', '20:43:04.546', '20:43:47.447', '21:11:08.997', '21:11:16.697', '21:22:07.717'],
['2014-163', '17:12:09.071', '17:38:08.219', '17:38:28.310', '17:59:25.649', '18:05:59.536', '18:09:53.243', '18:13:47.671', '18:16:53.976', '18:20:31.538', '18:23:02.243']]
它本质上是按特定日期对时间进行分组。
您不是在生成列表列表。您正在 将每个循环迭代 self.newLists
重置为包含 3 个元素的单个列表:
for line in g:
matrix = line.split()
JD = matrix[2]
mintime = matrix[5]
maxtime = matrix[7]
self.newLists = [JD, mintime, maxtime]
您需要改为使用 list.append()
将这 3 个元素添加到您在循环外设置 一次 的列表:
self.newLists = []
for line in g:
matrix = line.split()
JD = matrix[2]
mintime = matrix[5]
maxtime = matrix[7]
self.newLists.append([JD, mintime, maxtime])
您的 simplify
方法正在将 mintime
的 单个字符 添加到您的输出列表中:
for sub in self.newLists:
date = sub[0]
if date not in dates:
dates[date] = []
dates[date].extend(sub[1])
您想在那里使用 list.append()
,而不是 list.extend()
。可以使用 dict.setdefault()
简化该循环,而不是手动测试密钥:
for date, mintime, maxtime in self.newLists:
dates.setdefault(date, []).append(mintime)