Most voted "python" questions
Python is a dynamic and heavily typed programming language whose design philosophy emphasizes usability. Two similar but incompatible versions of Python are in use (2 and 3). Please mention the version you are using when asking a question about Python.
Learn more…8,642 questions
Sort by count of
-
2
votes1
answer79
viewsHow do I access the name of a class instance to use later?
I have a question how to use the "NAME" of a class instance. If I declare it directly: Sala_10 = Sala("Sala_10") works well. I can use the Sala_10.método(), but I would like to create (instantiate)…
pythonasked 6 years, 4 months ago George Bueno 23 -
2
votes4
answers1138
viewsDelete the last line of a txt file
I need to delete the last line of a txt file. I found the last line like this: with open('arquivo.txt', 'r') as content: line = content.readlines() last_line = line[len(line)-1] Note: you can change…
pythonasked 5 years, 9 months ago Igor Gabriel 530 -
2
votes1
answer608
viewsWhat does the class name mean in super(). __init__()?
How the name of the classes inserted within the constructor’s parentheses works __init__( ), in the classes Telefones, TiposTelefone and Agenda? This is only an excerpt of Chapter 10 code from the…
-
2
votes1
answer7854
viewsHow to insert a line in a Dataframe Pandas in the middle of other lines?
I have an output of sensor data that has the following desired structure: --- Beginning --- $LAGM,Colar03,Yellow,32262,-31226,-5120,-104,40,190,1662.00,1670.00,236.00,MGAL…
-
2
votes2
answers507
viewsHow to Improve Maximum Common Divisor Algorithm - URI 1028
I’m solving this URI problem which basically calls for the maximum common divide between two values to be found. I originally resolved with the math.gcd in half a dozen lines, however, the code was…
-
2
votes4
answers295
viewsPrint null or non-null in Python array
If matrix has only zero print 'null' if not print 'not null' I did so: m=[[0,0,0],[0,0,0],[0,0,0]] soma1=0 for novo1 in m[0]: soma1+=novo1 soma2=0 for novo2 in m[1]: soma2+=novo2 soma3=0 for novo3…
-
2
votes1
answer529
viewsHow to add a new dictionary inside another dictionary?
ini={"MAIN":{}} ini['MAIN']['Nome']="Jaque" print ini Print output: {'MAIN': {'Nome': 'Jaque'}} How to put a new dictionary into what already exists? I want to have it: {'MAIN': {'Nome': 'Jaque'}…
-
2
votes2
answers130
viewsLocal variable declaration in a PYTHON function
def fib(n): a, b = 0, 1 while a < n: print(a, end=' ') a, b = b, a+b print() result-> 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 But if I declare the variables a and b in different…
python python-3.x function variables variable-declarationasked 6 years, 3 months ago Rafael Eduh 51 -
2
votes1
answer3034
viewsHow to customize the x-axis of a graph with two y-axes, for text?
I would like to know how I change my x-axis from a graph with two y-axes, because I want to give the x-axis names of Brazilian states. numpy_matrix = df.as_matrix() x = numpy_matrix[0:,0] y1 =…
-
2
votes1
answer239
viewsPython processes
I’m studying the module multiprocessing and in all the examples in python documentation there is always a if verification in the examples, I know this checks whether the file is running directly or…
-
2
votes1
answer72
viewsExport file . SAP in R
It would be possible to export a file in . SAP using R or Python ? Find a tutorial that said that in R to export to . SAP it would be as follows: write_sas(df, "table.sas7bdat") But I was left with…
-
2
votes1
answer995
viewsHow to make Python 3.7 recognize the libraries installed in Python 3.6?
I have installed on my computer the Python 3.6 for some time now. Today I installed the Python 3.7.2. But when turning the import numpy as np, gave the following error: in the module named numpy In…
-
2
votes1
answer3640
viewsHow to check if there is an element in a Python list?
I’m trying to do an algorithm in Python to check if there’s an element in a certain list. If yes, the program will print on the screen, if not, it will add a new element in the list. My intention is…
-
2
votes0
answers136
viewsDjango admin using the request attributes
I am developing an application in Django and I used the request data on form to validate some fields. For example, if the user is changing the form for the "Administrator" group, when changing the…
-
2
votes1
answer1063
viewsNameerror : is not defined in python3
I have a simple problem, when I run this function it returns an error : Traceback (Most recent call last): file.py3 on line ? , in getUserOutputs userOutput = _runwwmdh(testInputs[i]) file.py3 on…
-
2
votes3
answers108
viewsHow to write a large amount of values per line in Python file?
I have a physical simulation code that I deal with LOTS OF INFORMATION and lots of interactions. For example, I have the calculation of the Qt load, however, it is 100 interactions and I am working…
pythonasked 6 years, 3 months ago Bernardo Cecchetto 21 -
2
votes1
answer280
viewsHow do I intersect each position on my list?
I tried it in the following ways based on answers achieved here in the forum. First form: A = [[1,2,3,4,5,6,7,8,9,10],[1,3,5,7,8,9,10,20],[4,5,7,13,16,20,21,30]] B = [1,2,3,4,5,6,7,8,9] C =…
-
2
votes2
answers180
viewsFind values in a list that are equal to the index they are in. How to optimize?
I am learning programming with Python 3.6 and participate in a challenge site. I need to find the smallest number whose value corresponds to its index in a list. If the list contains only one…
-
2
votes1
answer261
viewsCode sum error starting with 0; Problem inserting data into.txt file
As you can see in the code below I have tried several ways to make each product inserted to create a new code with codigo += 1, but is returning: Unboundlocalerror: local variable 'code' referenced…
-
2
votes4
answers2332
viewsPrint String vertically in Python
It’s a string-only exercise, I haven’t gotten to the list part yet What my mistake ? print("-"*30) nome = str(input("Digite seu nome: ")) cont = '0' while cont < nome: c = int(cont)…
-
2
votes2
answers197
viewsRegex able to ignore the prefixes of a word
I’ve got those regex next: regex_list = [ '(?:arquiv|auto)(\w+) (?:auto|arquiv) (?:definitiv)', '(?:arquiv|auto)(\w+) (?:auto|arquiv)', '(?:arquiv)(\w+) (?:definitiv)' ] My goal is to capture…
-
2
votes1
answer700
viewsModel field processing before writing to the database (Python + Django)
I am trying to perform a password encryption using Python 3.7 and Django 2.1. Looking at the documentation of Python, Django and some answers here in Stackoverflow, I arrived at the code as follows,…
-
2
votes1
answer1543
viewsCalculate pi with python and recursion
Calculate pi by formula pi=(s*32)**(1/3), being s=(1/1^3)-(1/3^3)+(1/5^3)... Amount of s determined by the user. It should be developed in python with use of recursivity. I’m not getting the…
-
2
votes2
answers2470
viewsHow to use Python Random.Seed()?
I need to generate a random number using the Python language, in the documentation saw that there is a function random.seed() to increase the randomness of the generated number, but I found very…
-
2
votes1
answer1231
viewsOpencv error in Python:
Good afternoon Everybody, all right? I’m looking to learn about Face Detection in Python using Opencv, installed all necessary libs, however while running the file I come across the following error…
-
2
votes1
answer364
viewsHow to filter tweets (status) with tweepy (Cursor)
I took from the book "Mastering social media Mining with python" the code below, which saves all a user’s tweets in JSON format. But I would like you to save in json only tweets of a certain date.…
-
2
votes2
answers711
viewsSplit and rsplit methods, python
I made this little program to try to simulate the methods 'split' and 'rsplit' of Python, but when I run it does not print the last sentence of the string. For example, if in the string I type 'pao…
-
2
votes1
answer1132
viewsConvert Float to Python Binary
I’m studying the IEEE 754 standard, and I should create a new 16-bit method. This function is to convert the mantissa that is already in format 1,xxx*2 yyy What may have gone wrong in this code,…
-
2
votes1
answer842
viewsPandas Settingwithcopywarning: A value is trying to be set on a copy of a Slice from a Dataframe
I want to copy an element of a dataframe and insert it into another dataframe. In essence there is a dataframe with name x area and another that I need to load with the data of the area, from the…
-
2
votes2
answers253
viewsExtract content from "Name:" field until end of line
Given a string in the following format: 'a1in1iionia\n\nDados do cliente\nNome: Foo\nE-mail: [email protected]\n' I need to extract the full content from the "Name" field. For this I wrote the code:…
-
2
votes3
answers4406
viewsHow to comment on python3
I’m starting to learn programming, and I’ve already learned the basics of language c++, in this language there is the function of commenting the programming that can be accessed by //, but the same…
-
2
votes2
answers180
viewsDoubt about Python derived class initialization
Imagine I have a base class Food and create a second class Rice that will inherit the class functionalities Food. If at the time of initializing, what difference does it make if I do: class…
-
2
votes1
answer63
viewsHow to assemble a set of different data types into a named structure in python?
Good afternoon, I had a question about how to gather a set of different types of data in a structure named in Python. I’m in the process of migrating from Delphi to the Python language. But I want…
-
2
votes1
answer35
viewsRemove two values from a text file
I need to remove two numbers from a text file, and add them to two variables (X and Y). I’m trying to use the CSV but I’m not getting it. with open('circulo.plt','r') as csvfile: plots =…
pythonasked 5 years, 7 months ago Guilhermeths 33 -
2
votes1
answer585
viewsError: "illegal target for variable Annotation" using "@Property"
Like pygame does not have a function similar to the library SFML -> View, I am developing a "camera" format to scroll the screen and preserve the positions of objects within the general…
-
2
votes3
answers4508
viewsSpace between result (Python)
So, I’m doing an exercise where the goal is for the machine to ask a number and then it type the number according to its value. For example, if I type "2" it gives me the answer "22", if I type "3"…
-
2
votes1
answer1373
viewsHow to add values of a csv using Python?
I have a csv file similar to this, with all the information of all the municipalities of Brazil (I shortened the csv to not get too extensive): ESTADO,MUNICIPIO,HABITANTES,AREA…
-
2
votes3
answers6345
viewsHow to turn upper-case and lower-case letters into upper-case letters in Python?
What was the name of the Python method that turns upper-case and lower-case letters into upper-case letters in the same string? For example: texto = "AquI TeM umA StrinG" saída = "aQUi tEm UMa…
-
2
votes3
answers98
viewsDoubt in my code is not printing correctly
question: Using the text file notes_students.dat write a program that calculates the minimum and maximum score of each student and prints the name of each student along with their maximum and…
pythonasked 6 years, 1 month ago Matheus Andrade 205 -
2
votes1
answer166
viewsLambda function for Dict Dict - Python
I would like to know if there is the possibility of using the filter with a function lambda to select a conjunto de dados of a array de J SON in the Python. Example: I have the following JSON…
-
2
votes1
answer1286
viewsHow to do after user use input not break line
Turns out I want to use an input, ie put a value but after hitting enter not break the line but continue in the place where I wrote the value, I have no idea how to do with the python input, I tried…
-
2
votes3
answers85
viewsRepeat if again when validation fails
ConvInicial = str(input('Você: ')) if ConvInicial == 'Não estou passando bem' or ConvInicial == 'Estou com dor' or ConvInicial == 'Preciso de ajuda': print('O que você está sentindo?') RespDor =…
-
2
votes1
answer1859
viewsdoubt installing chatterbot in windows
I am trying to install the chatterbot via Pip install chatterbot, however it is giving the error below, which may be? C:\Windows\system32>pip3 install chatterbot Collecting chatterbot Using…
pythonasked 5 years, 6 months ago Murilo Albeest 93 -
2
votes1
answer234
viewsCSV file reading and vector line storage
I need to read a csv file and store each one line of the file into different vectors. After that, convert these vectors to new csv files. The problem is that my code generates two vectors with data…
-
2
votes1
answer50
viewsHow to associate a set of words to a "do_" method of the Cmd class?
I’m using the class Cmd of module cmd Python to create an interpreter for a game in Text Adventure. However, I have come up with a problem regarding the words I use as methods for the interpreter to…
-
2
votes1
answer3380
viewsWait page for python Selenium
I created a program to log in to the site, but it loads very slowly when it opens, and for that I need to put Sleep(40), which waits 40 seconds to open, but sometimes it takes 50 seconds and error…
-
2
votes0
answers295
viewsHow to page using Flask and return in Json?
I need to make a query to the bank (neo4j) and return a json, but as the answer is very large precise paging. I’ve looked at the stack a lot, but most responses use the sqlalchemy pagination method…
-
2
votes2
answers121
viewsRepeat loop with problem
I have the algorithm below, and on the line while lista[i]==lista[i-1]: is returning the error: index out of range import random n1=0 n2=0 n1=int(input('Insira um numero para inicio'))…
-
2
votes1
answer487
viewsConcatenating equal data from a column into rows in Python
What I need is to find a function in Python that mounts my file this way. I already searched in pandas and did not find.…
-
2
votes1
answer2027
viewsPython - Run two scripts at once
In have a file x.py and the y.py, in another file I have the following code: import os os.system('py -3.7 x.py') os.system('py -3.7 y.py') But the problem is that the y.py only executes when…