python-2.7

Why is there no symmetric difference for collections.Counter?

给你一囗甜甜゛ 提交于 2021-02-05 10:42:28
问题 So for sets you can do a symmetric difference (^) which is the equivalent of union minus intersection. Why is ^ an unsupported operand for Counter objects while union and intersection still work? 回答1: Expanding on my comment, turns out it was discussed at time, and rejected. Click the link for the full message (and its thread); I'll just quote the "high order bits" of Raymond Hettinger's reply: It's unlikely that I will add this [symmetric difference] method to the Counter API because the

Making a tkinter scrollbar wider

徘徊边缘 提交于 2021-02-05 09:47:01
问题 I'm coding in Python 2.7/tkinter in Windows and am putting a scrollbar on a listbar, which I can do easily enough (thanks effbot.org). However, I also want to make the scrollbar wider - it will be used on a touchscreen so the easier it is to select it, the better. I figured the width attribute would make it wider, but all it does is create some blank space. What am I doing wrong here? Code: from Tkinter import * top = Tk() scrollbar = Scrollbar(top, width=100) scrollbar.pack(side=RIGHT, fill

Making a tkinter scrollbar wider

时光怂恿深爱的人放手 提交于 2021-02-05 09:46:36
问题 I'm coding in Python 2.7/tkinter in Windows and am putting a scrollbar on a listbar, which I can do easily enough (thanks effbot.org). However, I also want to make the scrollbar wider - it will be used on a touchscreen so the easier it is to select it, the better. I figured the width attribute would make it wider, but all it does is create some blank space. What am I doing wrong here? Code: from Tkinter import * top = Tk() scrollbar = Scrollbar(top, width=100) scrollbar.pack(side=RIGHT, fill

Can anyone explain me Python for loop (or iteration) algorithm

限于喜欢 提交于 2021-02-05 09:33:25
问题 I am learning python from Coursera . I have written one program , According to that when I click on the screen , it draws circle . See the program below -- # Dots # importing import simplegui import math width = 600 height = 600 ball_list = [] radius = 20 colour = "Yellow" # position ditector def distance(p,q) : return math.sqrt((p[0]-q[0])**2 + (p[1]-q[1])**2) # Mouse click -- Change the position def click(pos) : ball_list.append(pos) # global position # global colour # if distance(pos,

Reading multiple lines in a file, and separating their values at the same time, to store it in dictionary

半世苍凉 提交于 2021-02-05 09:26:32
问题 Hello I am beginner in python. I have following file as "test.txt" 1 2 5 2 6 7 as so on ..... I want to read the values and store it to a dictionary I have come up with following code but its not working as [x.split(' ') for x in edges.readline().rstrip().split(' ')] can access only one line of the file. Also this returns list on which int() typecasting can't be applied. It can access multiple line in a loop but I would like to know pythonic way to do this. connection = {(int(source),int(dest

Euclidean Distance Between All Points in an 2 Vectors

≡放荡痞女 提交于 2021-02-05 08:56:09
问题 If I have two single-dimensional arrays of length M and N what is the most efficient way to calculate the euclidean distance between all points with the resultant being an NxM array? I'm trying to figure this out with Numpy but am pretty new to it so I'm a little stuck. Currently I am doing it this way: def get_distances(x,y): #compute distances between all points distances = np.zeros((len(y),len(x))) for i in range(len(y)): for j in range(len(x)): distances[i,j] = (x[j] - y[i])**2 return

Python “for i in” + variable

一世执手 提交于 2021-02-05 08:45:34
问题 I have the following code: #Euler Problem 1 print "We are going to solve Project Euler's Problem #1" euler_number = input('What number do you want to sum up all the multiples?') a = input('Insert the 1st multiple here: ') b = input('Insert the 2nd multiple here: ') total = 0 for i in euler_number: if i%a == 0 or i%b == 0: total += i print "Sum of all natural numbers below 'euler_number' that are multiples of 'a'" print "or 'b' is: ", total With the following error: Traceback (most recent call

Python tkinter trace error

让人想犯罪 __ 提交于 2021-02-05 08:25:53
问题 I'm trying to write a GUI for my code. My plan is to use tkinter's StringVar , DoubleVar , etc. to monitor my input in real time. So I found out the DoubleVar.trace('w', callback) function. However, every time I make the change I get an exception: Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Anaconda2\lib\lib-tk\Tkinter.py", line 1542, in __call__ return self.func(*args) TypeError: 'NoneType' object is not callable I have no idea what's going wrong. I'm

Replace string between tags if string begins with “1”

醉酒当歌 提交于 2021-02-05 08:23:08
问题 I have a huge XML file (about 100MB) and each line contains something along the lines of <tag>10005991</tag> . So for example: textextextextext<tag>10005991<tag>textextextextext textextextextext<tag>20005992</tag>textextextextext textextextextext<tag>10005993</tag>textextextextext textextextextext<tag>20005994</tag>textextextextext I want to replace any string between the tags and that begins with "1" to be replaced with a string of my choice and then write back to the file. I've tried using

Ubuntu - PySide module not found for python2 but works fine for python3

房东的猫 提交于 2021-02-05 08:22:26
问题 I had PyQt4 running fine with python2 on Ubuntu 12.04. I then installed python-PySide. But the installation test would give me a module not found error. Then I installed python3-PySide and it works fine. So obviously something to do with my environment paths, but I'm not sure what I need to do. I'm guessing PySide is automatically checking if python3 exists and if it does then it'll use it regardless. I need PySide to work with python2.7 because of Qt4.8 compatibility issues. Any suggestions?