python-2.7

Check if a value still remains the same in a While loop Python

爷,独闯天下 提交于 2021-02-17 03:38:12
问题 I want know if there is a elegant method for looking if a value that continually changes in a while loop can be checked and stop the while loop if the value stops change and remains the same. For example: Value = 0 while True: value changes everytime (if value still the same break) 回答1: How about this way? BTW: Fix your typo error while is not While in python. value = 0 while True: old_value, value = value, way_to_new_value if value == old_value: break 回答2: previous = None current = object()

How to make zsh on a mac 10.9 use python 2.7.6 instead of Apple's preinstalled 2.7.5

筅森魡賤 提交于 2021-02-17 03:17:22
问题 How do I make zsh on a mac use python 2.7.6 which I have in my /usr/local/bin/python instead of python 2.7.5 which is in /usr/bin/python? (without needing to type /usr/local/bin/python before the script I want to run). My path setting on .zprofile is: # Setting PATH for Python 2.7 # The orginal version is saved in .zprofile.pysave PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}" export PATH 回答1: Set: export PATH="/usr/local/bin:$PATH" in your ~/.zshrc file. 来源: https:/

How to column_stack a numpy array with a scipy sparse matrix?

倖福魔咒の 提交于 2021-02-16 19:24:49
问题 I have the following matrices: A.toarray() array([[0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], ..., [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0], [0, 0, 0, ..., 0, 0, 0]], dtype=int64) type(A) scipy.sparse.csr.csr_matrix A.shape (878049, 942) And matrix B: B array([2248, 2248, 2248, ..., 0, 0, 0]) type(B) numpy.ndarray B.shape (878049,) I would like to column stack A and B in C, I tried the folowing: C = sparse.column_stack([A,B]) Then: /usr/local/lib/python3

How to convert this Python 2.7 code to Python 3?

浪尽此生 提交于 2021-02-16 16:32:13
问题 The following code works in Python 2.7, to dynamically inject local variables into a function scope: myvars = {"var": 123} def func(): exec("") locals().update(myvars) print(var) func() # assert "var" not in globals() It's a bit subtle, but the presence of an exec statement indicates to the compiler that the local namespace may be modified. In the reference implementation, it will transform the lookup of the name "locals" from a LOAD_GLOBAL op into a LOAD_NAME op, enabling addition to the

How to save numpy ndarray as .csv file?

狂风中的少年 提交于 2021-02-16 15:51:05
问题 I created a numpy array as follows: import numpy as np names = np.array(['NAME_1', 'NAME_2', 'NAME_3']) floats = np.array([ 0.1234 , 0.5678 , 0.9123 ]) ab = np.zeros(names.size, dtype=[('var1', 'U6'), ('var2', float)]) ab['var1'] = names ab['var2'] = floats The values in ab are shown below: array([(u'NAME_1', 0.1234), (u'NAME_2', 0.5678), (u'NAME_3', 0.9123)], dtype=[('var1', '<U6'), ('var2', '<f8')]) When I try to save ab as a .csv file using savetxt() command, np.savetxt('D:\test.csv',ab

Running a .py file in a loop

守給你的承諾、 提交于 2021-02-16 15:38:14
问题 I am currently trying to run a .py file but in a loop. Just for a test I am using I = 0 while I<10: os.pause(10) open(home/Tyler/desktop/test.py) I = I + 1 I am sure this is a very simple question but I can't figure this one out. I would also like to add in the very end of this I have to make this run infinitely and let it run for some other things. 回答1: There are a few reasons why your code isn't working: Incorrect indentation (this may just be how you copied it on to StackOverflow though).

Running a .py file in a loop

China☆狼群 提交于 2021-02-16 15:38:05
问题 I am currently trying to run a .py file but in a loop. Just for a test I am using I = 0 while I<10: os.pause(10) open(home/Tyler/desktop/test.py) I = I + 1 I am sure this is a very simple question but I can't figure this one out. I would also like to add in the very end of this I have to make this run infinitely and let it run for some other things. 回答1: There are a few reasons why your code isn't working: Incorrect indentation (this may just be how you copied it on to StackOverflow though).

pygame installation via pip

别来无恙 提交于 2021-02-16 15:20:16
问题 I'm trying to install pygame in my python 2.7. I do it in this python because for some reason if I try to install pip in python 3.7 it won't work. From the terminal I entered: sudo pip install pygame And supposedly I installed it well, but when I try to do the import it tells me: Traceback (most recent call last): File "example.py", line 1, in <module> import pygame ImportError: No module named pygame if i try to do again sudo pip install pygame : Requirement already satisfied: pygame in

with NLTK, How can I generate different form of word, when a certain word is given?

别来无恙 提交于 2021-02-16 14:39:06
问题 For example, Suppose the word "happy" is given, I want to generate other forms of happy such as happiness, happily... etc. I have read some other previous questions on Stackoverflow and NLTK references. However, there are only POS tagging, morph just like identifying the grammatical form of certain words within sentences, not generating a list of different words. Is there anyone who bumped into similar issues? Thank you. 回答1: This type of information is included in the Lemma class of NLTK's

Why multi-threaded python program slow on ec2 micro-instance?

佐手、 提交于 2021-02-16 09:23:28
问题 I am working on a Online Judge code checker.My code uses multi-threading in python 2.7.The same program on my local machine (i core 3 RAM 4GB) evaluates about 1000 submisions in 1 minute 10 seconds. But when I run it on ec2 micro instance(about 600 MB RAM) it takes about 40 minutes(It gets slow for some random seconds).To know the reason I broke down things. First this is how my evaluator works: I have a main program worker.py , which creates multiple threads The main thread pulls submissions