python-2.7

Using sum() to print the sum of even numbers in a list

自作多情 提交于 2021-01-27 22:40:27
问题 I am having trouble finding information on using sum to take from a list. I know how to use sum with range, for example: sum = 0 for i in range(50): sum=sum + i print (sum) But I can't get my code to work when I am using a list such as [1, 2, 6, 7, 8, 10] and taking the even numbers using sum . Can anyone point me in the right direction? 回答1: You can filter out odd-values: def is_even(x): # if the remainder (modulo) is 0 then it's evenly divisible by 2 => even return x % 2 == 0 def sum_of

Displaying multiple bar graph in django

喜夏-厌秋 提交于 2021-01-27 21:08:10
问题 I am new to django and know little of python. I am learning to draw graphs in django framework. I drew single bar-charts but have problem to draw multiple bar-chart using the database telecom_db of my project in django. However, in wxPython the following code worked fine. Could you figure out if something wrong with django in code below: def graph(request): figName="figGraph.png" path="F:\MajorWorkspace\Visualisations\\"+figName if os.path.exists(path)==False: age_gr = [] countm = [] countf =

Splitting Python string into list of pairs

风格不统一 提交于 2021-01-27 20:34:09
问题 I have data in the following format: "22.926 g 47.377 g 73.510 g 131.567 g 322.744 g" What I would like to do is to split it into a list such that value and units are grouped together, e.g.: ["22.926 g","47.377 g","73.510 g","131.567 g","322.744 g"] Of course, in Python 2.7, I can do this the hard way: result = [] tokens = "22.926 g 47.377 g 73.510 g 131.567 g 322.744 g".split() for index,item in enumerate(tokens[::2]): result.append(item+" "+tokens[index+1]) but I hoped that there is a

NameError — imported modules when script is broken down in multiple python files

倾然丶 夕夏残阳落幕 提交于 2021-01-27 19:58:57
问题 It is hard to find a title for this question and hopefully this thread is not a duplicate. I was writing that long script in Python 2.7 (using PyCharm 2016.2.2) for a project and decided to split it in different .py files which I could then import into a main file. Unfortunately, it seems that when importing a module (e.g. numpy) earlier in the code does not mean that the .py files that are imported below will have knowledge of that. I am new to python and I was wondering whether there is an

getting module import error while running PyInstaller generated binary

為{幸葍}努か 提交于 2021-01-27 19:42:40
问题 I'm getting an error while trying to a simple hello.py using PyInstaller on RHEL X64. Python 2.7.12 is alt installed in /opt/python Compilation Output: [root@myrig CommandManager]# pyinstaller Hello.py 21 INFO: PyInstaller: 3.2 21 INFO: Python: 2.7.12 22 INFO: Platform: Linux-3.10.0-327.22.2.el7.x86_64-x86_64-with-redhat-7.2-Maipo 62 INFO: wrote /home/myuser/CommandManager/Hello.spec 66 INFO: UPX is not available. 107 INFO: Extending PYTHONPATH with paths ['/home/myuser/CommandManager', '

Python Pandas: How to replace string contain “?”

孤街醉人 提交于 2021-01-27 19:40:14
问题 I have a Python 2.7 Pandas Data frame like following: Id Title URL Id-1 Bruce Almighty https://www.youtube.com/watch?v=5VGyTOGxyVA Id-2 Superhero Movie https://www.youtube.com/watch?v=3BnXz-7-y-o Id-3 Taken https://www.youtube.com/watch?v=vjbfiOERDYs Id-4 Forest Gump https://www.youtube.com/watch?v=eJFkCJySHdY&t=524s I want to replace part of string "?v=" with some other string, for example "ppp"from the column "URL". I tried by normal replace command: df['URL'] = df['URL'].str.replace('?v=',

regex split into groups

倖福魔咒の 提交于 2021-01-27 19:20:21
问题 My regex-fu is weak today. I'm trying to capture groups in a string into 5 parts with the format: substring delimiter substring number(space) substring I've tried using word boundaries but no success. I've resorted to using *.(greedy and lazy, I know) which is bit better than not working at all Here's what I have: import re s = "FOREVER - Alabaster Cuttlefish - 01 This Style Is Cheese" m = re.compile("(.*)(\s-\s)(\d{1,3}\s)(.*)") g = m.match(s) if g: print m.match(s).group(1) # FOREVER print

Getting “ AttributeError: 'ImageData' object has no attribute 'data' ” in headless gym jupyter Python 2.7

心已入冬 提交于 2021-01-27 18:54:15
问题 I am trying to run gym in headless server and render the same in jupyter. Python version 2.7. I have started the jupyter using xvfb-run -a -s "-screen 0 1400x900x24" jupyter notebook Below is the Jupyte cell that I run. import matplotlib.pyplot as plt import gym from IPython import display %matplotlib inline env = gym.make('CartPole-v0') env.reset() plt.imshow(env.render(mode='rgb_array')) display.display(plt.gcf()) display.clear_output(wait=True) env.step(env.action_space.sample()) # take a

Debugging floating-point exception in python

戏子无情 提交于 2021-01-27 18:45:22
问题 I'm trying to run something that gives me a floating point exception ( Floating point exception. Core dumped . No traceback). Since that kind of exception can't be caught using usual try and except , I can't figure out what's causing the exception. Since I'm working on a VM, I'm not concerned about the implications of using fpectl . However, since I'm using anaconda python, I have no idea how to reinstall it with the appropriate flag. I tried downloading a python distribution from the

Breakpoint on continue in if statement not hit

不打扰是莪最后的温柔 提交于 2021-01-27 18:42:54
问题 In the following code, both a and b are outputs of generator functions, and could evaluate to None or have a value. def testBehaviour(self): a = None b = 5 while True: if not a or not b: continue print('blaat') If I put breakpoints (using Visual Studio Code) on the line with the continue statement, and the line with the print statement, neither is hit. The print statement is not called, and the loop just keeps running indefinitely as expected, but I would expect that breakpoints are hit. If I