不使用计数器创建字典

时间:2016-12-08 00:22:25

标签: python python-2.7 list dictionary counter

from Tkinter import * # --- --- # global variable to use in different functions win = None def get_input(text, variable): global win # inform function to use global variable `win` to assign value win = Toplevel() win.title("Get value") f = LabelFrame(win, text=text) f.pack() # use `f` instead of `win` to put inside LabelFrame e = Entry(f, textvariable=variable) e.pack()#side="right") e.bind("<Return>", lambda event:do_more(variable)) b = Button(win, text="OK", command=lambda:do_more(variable)) b.pack() def do_more(variable): data = variable.get() print 'do_more:', data, len(data), type(data) win.destroy() # --- --- def get_value(text, variable): # create window get_input(text, variable) # this code is executed directly after creating window, not after closing window # so code has to wait till window is closed win.wait_window() data = variable.get() print 'get_value:', data, len(data), type(data) # --- main -- root = Tk() root.title("Ask-name-SUB") # global variables var_name = StringVar() var_address = StringVar() b = Button(root, text="Enter your name", command=lambda:get_value("Your name:", var_name)) b.pack() b = Button(root, text="Enter your address", command=lambda:get_value("Your address:", var_address)) b.pack() b = Button(root, text="Cancel", command=root.destroy) b.pack() root.mainloop() INFO我正在使用list进行排序,并希望输出采用字典的形式。但是,我得到了不必要的字符:

Counter

不必要的字符是:

wanted_dict = collections.Counter(INFO)
>>> Counter({'BLA': 102, 'BLABLA': 96, 'BLABLABLA': 96...})

有没有办法输出以下内容?

Counter
(
)

1 个答案:

答案 0 :(得分:2)

将其包裹在dict电话中。

>>> dict(Counter({'BLA': 102, 'BLABLA': 96, 'BLABLABLA': 96}))
{'BLABLABLA': 96, 'BLABLA': 96, 'BLA': 102}

您可以使用此方法将大多数特殊collections字典对象转换为普通字典。