Most voted "python-2.7" questions
In the 2.x series of the Python programming language, version 2.7 is last and the latest. Use this tag only if the problem is specific to this version. Also use the [python] tag in your question.
Learn more…477 questions
Sort by count of
-
29
votes3
answers15296
viewsWhat is Yield for?
I’ve been writing some for some time scripts with Python, in some tutorials I am sometimes presented with the yield, that normally arises in repeat structures commonly while. What is it for? And how…
-
21
votes3
answers553
viewsIn Python, what are the consequences of using is instead of '=='
The two forms below return True, but what are the consequences of this in a more complex code? >>> a = '2' >>> a is '2' >>> True >>> a == '2' >>> True…
-
15
votes3
answers1932
viewsIs there a more efficient way to create an array from another array dynamically, filtering the contents of the first one?
I have an array of values that can include several numpy.Nan: import numpy as np a = np.array ( [1, 2, np.nan, 4] ) And I want to iterate over your items to create a new array without np.Nan. The…
-
15
votes1
answer6546
viewsNaming conventions for variables and functions in Python?
In the R, there is a lot of freedom and variety in function names between packages. Dot names (get.this), names in camel (getThis), names in snake_case (get_this). This has its pros and cons. Why,…
-
14
votes2
answers8854
viewsDifferences between Python versions?
Hello ! I recently decided to start studying the Python language, however when researching the language I realized that there are differences between version 2 and 3. These differences are really…
-
13
votes1
answer2693
viewsDifferences between Python versions 3.x and 2.7
I will start a project in college related to search engines and want to take the opportunity to also learn Python. I don’t know much about the language and my biggest doubt is about which version,…
-
12
votes3
answers8338
viewsWhat does %=mean?
I have a code that contains the situation below: id %= 1000 But I don’t know the function of that operator.
-
12
votes3
answers844
viewsHow to initialize a list of empty lists?
Using Python 2.7.12 I need to create a list as follows: lista = [[],[],[],[],.........,[]] This list needs to have a very large number of lists within it (so the .....). I found around the following…
-
11
votes1
answer916
viewsProblems with installing Pip (python package manager)
I use Mac OS and use HomeBrew as a package manager. I installed the python 2.7.10. Together with this installation was to have occurred the pip. However the following problem occurs: ==>…
-
9
votes7
answers7922
viewsConvert an array of floats to integer
Suppose the following: import numpy as np a = np.array ( [1.1, 2.2, 3.3] ) How to convert this array to int without having to iterate each element or using another array? Why do: b = int(a) Gives an…
-
9
votes1
answer5098
viewsFuncionamento @classmethod
I would like to better understand the @classmethod annotation of Python. Briefly it works as a second alternative to constructor. But what are the benefits? When to use? @classmethod def…
-
9
votes2
answers1356
viewsConvert a list list into a single list
I’m working on Python 2.7. I have this result: Resultado=[['A','B','C'],['D','E','F'],['G','H','I']] How can I turn this into a list? And get the following:…
-
9
votes1
answer597
viewsHow to calculate Shannon entropy based on HTTP header
Shannon’s entropy is given by the formula: Where Ti will be the data extracted from my network dump (dump.pcap). The end of an HTTP header on a normal connection is marked by \r\n\r\n: Example of an…
-
8
votes1
answer349
viewsIn Python, how do you explain the expression 'x = not x'
I already know the result, but I would like to understand what happens, why of such a result... For example: >>> a = True >>> b = not a >>> print(b) False It’s a simple…
-
8
votes4
answers11584
viewsHow to put a Python module in a different folder than my script?
I’m trying to run a Python script (v2.4 and v2.7) and created a module with some methods. Unfortunately this module needs to stay in a separate folder. In the python documentation and so far on…
-
8
votes2
answers1172
viewsIn Python, is there any rule or advantage regarding the use of 'Self'?
Let us consider the examples below: Example 1: > class PrintText(): > def __init__(self): > text = 'Funciona!' > self.printa(text) > > def printa(self, text): > print(text)…
-
8
votes4
answers5471
viewsCount occurrences in a list according to prefixes
Let’s say I have a list ['rato', 'roeu', 'rolha', 'rainha', 'rei', 'russia'] and another list with prefixes ['ro', 'ra', 'r'] how do I count how many times each prefix is within the first list?…
-
8
votes1
answer787
viewsCheck if list is equal
I have the following list: x = [([1,2], [1, 2]), ([1,2], [4, 5])] And I wanted to check if the first list of each Tuple is always the same. Note: the list x contains tuples with lists. These tuples…
-
8
votes4
answers485
viewsHow to determine the index of items in one list in another
Work with python 2.7. Whereas the following: S_names=["A", "B", "C", "D", "E", "F", "G"] S_values=[1,3,4,2,5,8,10] other=["Z","W","B","S","A","E","X","T","D","G","L","C","F"] I need to find in what…
-
8
votes4
answers392
viewsProgram that returns the unusual numbers of two lists (python)
I’m trying to make a program, in Python, that returns a list with the unusual elements between two other lists. I made the following code: def indice(a,b): ind = [] for i in range (len(a)): for w in…
-
7
votes2
answers7019
viewsTkinter focus a window ignoring the back?
In my code there is a frame with a button and it is called "Open", when clicking "Open" another window will appear. The problem is that if I click on the window that this "Open" button again, it…
-
7
votes2
answers5650
viewsHow to Direct Commands to the Linux Python Terminal
I need to create a script in Phyton that when executed is via mouse click or keyboard 'enter', it opens the linux terminal itself and execute any command inside it. I have already managed to get him…
-
7
votes3
answers27933
viewsPython str.replace() doubt
I have the following text:: 'box 1 is blue, box 10 is green, box 100 is empty' I want to replace 'box 1' with 'package 2', so I do: >>> texto = 'caixa 1 é azul, caixa 10 está verde, caixa…
-
7
votes3
answers25438
viewsHow to run programs written in Python in windows by hiding terminal feedback
I have the following situation: I have a program written in python that works with Gtk + Webkit + flask. To run such program I must enter from CMD and type "python.py file" and it starts my GUI…
-
7
votes3
answers1045
viewsFibonacci with parallel execution? Threads?
def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) This code makes the recursive Fibonacci "simple"...…
-
7
votes4
answers16556
viewsGenerate random numbers in Python without repeating
I have the following situation: I have a vector with 4 indexes. In each index a random value is generated from 0 to 100. I have a code that does this perfectly, but sometimes the numbers repeat.…
-
6
votes2
answers1434
viewsWhat are the best practices for working with Legacy Database in Django?
I have a legacy database and would like to know what good practices for table names, or tips on how to use inflections (like in Rails) for Django in the latest version.
-
6
votes1
answer237
viewsRecursive function for integer number input
I have this code but it doesn’t work very well, I mean, if it’s whole at first numOfValues is correct, but if it is not gets the type None, for what remains in memory is the first input (which is…
-
6
votes1
answer1044
viewsSave the result of a variable to the Mysql database using python passed as parameter
I have a database on mysql which contains a table with several expressions. I can extract the expressions through a procedure, solve them, but when I want to return the value of the calculation to…
-
6
votes2
answers298
viewsHow to run audio with Pyqt?
Is there any way to run an audio file with Pyqt? You can do this only with Pyqt or you need to install another library?
-
6
votes1
answer705
viewsHow to extract information from an HTTP header with Python?
We know that in the HTTP protocol, the end of the header is indicated by "\r\n\r\n". Example: It may be that, for some reason, the customer does not send the "\r\n\r\n" to the server (could be an…
-
6
votes4
answers320
viewsFlatten lists. Is a more concise solution possible?
I solved the following Python exercise: Implement a function that receives a list of length lists any and return a list of a dimension. The solution I was able to make was this:: #!/usr/bin/env…
-
5
votes1
answer1266
viewsHelp Tables python27
Good Morning, I’m new with Python/Tkinter and I have some projects in mind, but all of them have use of tables along with them, could anyone tell me if there is a library I could use to create these…
-
5
votes3
answers4053
viewsHow to add list values in tuples?
I have for example a list with tuples, and inside these tuples I have 2 lists, where only the second list of each tuple varies: x= [(y, [1, 2]), (y, [3,4]), (y, [5, 6])] How do I add the first…
-
5
votes1
answer437
viewsUrllib2, exception handling
I’m a beginner in the art of programming. I’m learning to code in Python through a book, Learning to Program: The art of teaching the computer (Cesar Brod - Novatec Editora) In one of the exercises,…
-
5
votes1
answer6374
viewsPython 3.4 and Python 2.7: How to remove the preinstalled Macbook Version?
I am currently migrating from a mac to a Windows and today I discovered, when installing all the resources I needed, that there was already a Python 2.X in it and I need to run Python 3.4.3. How can…
-
5
votes1
answer938
viewsHow to create buttons next and back QUI Pyqt4
Hello, I’m new to Pyqt and I have a question, I need to create a program with several steps, or screens, where the user will do his due processing on each screen and clicking 'forward' to the next…
-
5
votes4
answers788
viewsGenerate random values from a distribution in Python
Hello! I’m trying to generate random values of a distribution (Gamma, normal and etc) in python, but I’m having trouble importing the library. I am using the following library "import scipy.special…
-
5
votes1
answer995
viewsAdding a comma to a string
Good personal and possible to add comma in an example string: a = '1234567890abcdefghijklmnopqrstuvwxyz_' for her to stay like this a = 1,2,3,4,5,6,7....…
-
5
votes1
answer125
viewsError importing xml.parsers.Expat in Python 2.7 only works with Python3
Good morning, a few months ago I started with python I’m using it to do tests on android applications. I have the following problem importing the xml.parsers.expat. when running straight into the…
-
5
votes1
answer480
viewsPython lists and dictionaries
I’m working on Python 2.7. import collections counter_ger=collections.Counter(sub_todaas) a=counter_ger.values() b=counter_ger.keys() sub_ind_novo = [Ntodos.index(i) for i in b if i in Ntodos]…
-
5
votes1
answer4176
viewsAlignment with string.format and Unicode
I’m having trouble with the alignment of strings when using the .format(), for example when doing: >>> print '{:>6}'.format('agua'} agua >>> print '{:>6}'.format('água'} água…
-
5
votes2
answers14521
viewsSort dictionary by Python value
I have a dictionary with the following format dic={759147': 54, '186398060': 8, '199846203': 42, '191725321': 10, '158947719': 4} would like to know if there is how to sort it by value and print on…
-
5
votes1
answer741
viewsSubtract 2 weeks from a full date in Pyhton
I’m thinking a date in this format: time.strftime("%d/%m/%Y") // 00/00/0000 I want to subtract two weeks from that date, but I don’t know how to do it.
-
5
votes1
answer499
viewsPython "Attributeerror: __exit__" problem
In a group, I asked them to give me tips on some program to test my knowledge. He told me to create a program that reads a file called "arquive.txt" and manages files CHAR and STAGE, with the file…
-
5
votes2
answers323
viewsPython reserved word Yield
What is the difference between the following codes ? def bottom(): yield 42 def bottom(): return (yield 42)
-
4
votes1
answer256
viewsHow to allow just one instance of a program made in Python?
Assuming I created a program in Python and it is working perfectly, how do I allow just one instance of the program at a time? I Googled and found a person saying to have solved the issue using…
-
4
votes1
answer326
viewsPython - Test read permissions
Is there a function that returns, in Python, True if I have read permissions for the python file?
-
4
votes1
answer1843
viewsHow to add string numbers
Well I have a string like a=("1234") and I wanted to separate these numbers in order to add them, like this: 1+2+3+4 How can I do that? I appreciate any help :)
python-2.7asked 10 years ago CSAnimor 731 -
4
votes2
answers152
viewsPermutations and files
I’m working on a game that involves permutations, to create a list of words, in several separate files. Problems start to arise when I require words with more than 4 letters, as the corresponding…