Posts by Pedro von Hertwig Batista • 3,434 points
117 posts
-
3
votes1
answer178
viewsA: Sort an alphanumeric list by number
First you have to join the values with their descriptions. The list, like this, does not have a mapping between product and value. You could for example make tuples of two items: the product and its…
-
0
votes2
answers1192
viewsA: Python matrices - Concatenate
You don’t even have to wear one for. You can do the following: matriz_a = [ [0, 0, 1], [0, 1, 0], [1, 0, 1] ] matriz_b = [ [2, 2, 3], [3, 2, 2], [3, 3, 3] ] matriz_c = [*matriz_a, *matriz_b]…
-
0
votes2
answers4222
viewsA: How to find and show the position of an item in the list, not setting the string value for the search?
If you want to get the third item in the list, you should use brackets to access the item in the list you want. It’s important to remember that indexes start at 0, then to access the 3rd item, you…
-
2
votes1
answer98
viewsA: I cannot save the information to the Database (PYTHON MYSQL)
Missed the bd.commit(). Basically, the data does not enter the database at the time you execute the query. It is necessary to commit to perform the actual actions. bd =…
-
1
votes1
answer483
viewsA: Matrices in python
The question is not exactly trivial. One way to check whether the words are contained in the columns is to use zip to take a letter from each column and form column strings: # Criar lista de…
pythonanswered Pedro von Hertwig Batista 3,434 -
0
votes1
answer413
viewsA: How much can I use Instagram data on a Crawler?
If the information is not public (or you do not have access after logging in), it is not possible to scrap it. Sometimes the information is loaded but not shown; hence, an analysis of the requests…
pythonanswered Pedro von Hertwig Batista 3,434 -
2
votes2
answers130
viewsA: Datetime without the year
I could create my own datetime class, but as I will need to make date comparisons I would have to overload operators, but I don’t want to have to reinvent the wheel Unless you compare dates without…
-
5
votes2
answers408
viewsA: How to make 9 potentials quickly
asks to raise the number to 9 You say one thing and your code says another. When you do pow(9,numero), is bringing 9 to a number, and not a number at 9. Let’s look at things in two ways. Last…
-
1
votes3
answers9117
viewsA: convert a string list into a list of integer numbers
Only one thing about the accepted answer: the median, according to Wikipedia, is the value separating the larger half and the smaller half of a sample, population or probability distribution. I…
-
2
votes1
answer724
viewsA: Site with hidden HTML
This happens because the page initially does not contain the information about the cars. It is loaded empty, and then uses Javascript to load the data dynamically and insert them into the page. One…
-
0
votes1
answer119
viewsA: Who first executes the condition or code block in Python?
it first executes the code then checks That’s not true, I believe for no language. if 1 > 2: print('Isso nunca vai ser executado.') In the example above, the print will not be executed. If the…
-
0
votes1
answer41
viewsA: Check clients using python Curl
with open('file.txt', 'r') as fd: for linha in fd.readlines(): user, pwd = linha.strip().split('|') payload = "username={}&password={}&clientVersion=2.4.9-undefined".format(user, pwd)…
python-3.xanswered Pedro von Hertwig Batista 3,434 -
1
votes1
answer88
viewsA: If you have this word in the Curl answer, print "OK"
for url in response.text: if 'userId' in url.lower(): print ("OK") Here you see if userId is in url.lower(). This will never work in True because the I in userId is uppercase, and you compare that…
-
4
votes1
answer152
viewsA: Python - Type function in tuples
To check if it is integer, we can compare the result of the function type with int: >>> type(10) == int True To check if it is positive, we must see if the given number is greater than 0:…
-
0
votes1
answer53
viewsA: Pass several attributes on Django
Yes. What do you put in render is a dictionary, and dictionaries may contain more than one key/value pair. What you want is equivalent to the following: return render(request,…
-
2
votes1
answer424
viewsA: error in Math.sqrt
You are confusing your variables. First do delta *= (-1) raizDelta = float(math.sqrt(delta)) And then use delta, when you calculated the root and put it into the variable raizDelta: imaginario =…
-
6
votes3
answers551
viewsA: if and Else with almost equal blocks
You almost got it right with your example. It’s the ternary operator: def func(x): j = x/2 if x % 2 == 0 else x print(j) It is not possible to make a elif, but it is possible to connect more than…
-
5
votes2
answers399
viewsA: Can you replace commands with variables in python?
You can save functions: def minha_funcao(x): while True: x += 1 print(x) if x % 5 == 0: break minha_variavel_func = minha_funcao minha_variavel_func(2) # Executa minha_funcao com 2 como argumento x…
python-3.xanswered Pedro von Hertwig Batista 3,434 -
1
votes1
answer119
viewsA: Error every time I make a Flask Requests
jinja2.exceptions.Undefinederror: list Object has no element 246 I believe the function fails when you try to pass a number greater than 245. That’s because the list only has 245 elements. You can…
-
1
votes2
answers6760
viewsA: Math Domain error in PYTHON
The error happens exactly on this line: delta = math.sqrt(d) And it happens because you tell him to calculate the square root of d, which may be negative, before checking whether it really is…
pythonanswered Pedro von Hertwig Batista 3,434 -
1
votes1
answer197
viewsA: Why is such a simple game getting so heavy?
Use Pycharm’s Profiling mode (which is nothing more than utility cProfile under the table) indicates that most of the time (91.3%) is spent on function blit. Although it is a necessary function to…
-
2
votes1
answer137
viewsA: python content management
There are two things you need to understand when dealing with binary files like this: Read X amount of bytes with read advances the reading position of the X file positions. It happens every time…
python-3.xanswered Pedro von Hertwig Batista 3,434 -
0
votes1
answer88
viewsA: Mechanicalsoup Proxy in python
Through session.proxies: import mechanicalsoup br = mechanicalsoup.StatefulBrowser() site = 'www.exemplo.com.br' proxies = { 'https': 'my.https.proxy:8080', 'http': 'my.http.proxy:8080' }…
pythonanswered Pedro von Hertwig Batista 3,434 -
0
votes1
answer237
viewsA: Converting list to float array
You can use eval to turn a string into its Python equivalent: minha_lista_str = '[1, 2, "a", 2.3]' minha_lista = eval(minha_lista_str) print(minha_lista, type(minha_lista)) # [1, 2, 'a', 2.3]…
-
0
votes1
answer739
viewsA: Update a value within a function
As you’ve noticed, do k = 28 at the start of the function will cause the 28 value to be assigned to k whenever the function is executed. So how to make it not happen? Just don’t do k = 28 within the…
-
1
votes1
answer278
viewsA: Transfer list to Python binary file
The error tells you exactly what the problem is. When you do arq1.write(struct.pack('=i', x)) Is trying to get x in a space int (as indicated by =i), that is, an integer. A careful observation of…
python-3.xanswered Pedro von Hertwig Batista 3,434 -
1
votes1
answer25
viewsA: My inserts are not being saved
You need to give a commit to save changes to the bank. cur.execute(query); con.commit()…
-
3
votes1
answer54
viewsA: For i uses only the last variable - Python
Because of indentation, your for only runs the assignment line to arquivo5. In the end, you modified arquivo5 three times before entering the with, then when you enter the with it executes only once…
-
0
votes1
answer262
viewsA: Tkinter locking into function
This happens because your program is synchronous. That means it only does one thing at a time. When you call sleep, the program stops to perform to sleep that certain time, and while sleeping it…
-
1
votes1
answer80
viewsA: What does this -J8 mean?
The -j8 is a parameter passed pro compiler to use 8 processor cores during build, to speed up the process. For questions about command line utility parameters in general: use man <comando>…
-
2
votes1
answer635
viewsA: Python: Error translating text with the Translate api
You must specify that you expect a Unicode string: # -*- coding: utf-8 -*- from googletrans import Translator translator = Translator() print unicode(translator.translate('hello', dest='pt')) #…
-
0
votes1
answer780
viewsA: How do I make an object move on the screen using Tkinter?
Your code has three problems: It does not call the play function repeatedly because it is not passed to after. def jogar(self): self.update() self.janela.after(1, 10) Should be: def jogar(self):…
-
3
votes2
answers789
viewsA: Replace all characters in a string with another character
You are always overwriting secretword without modifying it. def getGuessedWord(secretWord, lettersGuessed): secretWord_copy = "" for i in secretWord: print(i) secretWord_copy =…
-
3
votes2
answers106
viewsA: Function that determines whether all letters contained in one string are in the other
You can do it in a row: def is_word_guessed(secret_word, letters_guessed): return all([letter in letters_guessed for letter in secret_word]) print(is_word_guessed('abcd', ['a', 'b', 'd', 'c'])) #…
-
5
votes2
answers322
viewsA: How to implement Fibonacci more efficiently using dictionaries?
You are on the right track of trying not to recalculate a recursive function multiple times when your result does not change, but the easiest and most efficient way is not to use dictionaries; it is…
-
1
votes2
answers3216
viewsA: Crud with Python
Best practice is to isolate your interface logic from data logic. Your class Students don’t have to know or depend on the interface, and their interface should control as little logic as possible…
pythonanswered Pedro von Hertwig Batista 3,434 -
0
votes1
answer208
viewsA: Select with pyodbc
You should only wear one % after the string and pass a tuple: def checkin(self): veri = self.cur.execute("select %s.id_aluno " "from alunos,%s" " where %s.id_aluno = alunos.matricula" %…
-
2
votes1
answer1013
viewsA: How to fit the geckodriver for use of Selenium?
You can move the geckodriver pro /usr/local/bin with super user permissions. sudo cp geckodriver /usr/local/bin/
-
2
votes1
answer2565
viewsA: Extract information from lattes
I do not know the specific system of captcha Lates, but I will try to give a "broad" solution. In general the ideal is to scrap only HTML with requests and BeautifulSoup as you mentioned (or, with…
-
4
votes2
answers75
viewsA: Program that gives a secret number using bisection search
A typo made you create a new variable. elif guess == "l": inio =meio meio = round((inio +fim)/2) guess = input("Is your secret number {}?:".format(meio)) should be: elif guess == "l": inicio = meio…
-
9
votes1
answer2436
viewsQ: What is the purpose of the`reduce` function in Python?
In which cases is useful and how can I use the function reduce?
-
14
votes1
answer2436
viewsA: What is the purpose of the`reduce` function in Python?
The function reduce, available in the built-in module functools, serves to "reduce" an iterable (as a list) to a single value. It is a slightly more common paradigm in functional languages, but it…
-
1
votes2
answers7177
viewsA: How to repeat the code at the end in python?
Just wrap your code in a loop while True. The while checks a condition, and if it is true, runs again everything that is inside your indentation block. How we pass True, he will always run…
-
19
votes2
answers4659
viewsA: What is the flush parameter of the print function?
The print uses stdout, as C. This is nothing more than the "file" in the operating system to which the text output of a program is sent, and so can be shown to the user. By default, the stdout is…
-
2
votes3
answers713
viewsA: Print the largest substring of s where the letters occur in alphabetical order
You weren’t far away, OP. I just needed to remember the biggest word: s = "azcbobobegghakl" indice = 0 palavra = "" resposta = "" maior_palavra = "" while indice < len(s) - 1: if palavra == "":…
-
5
votes3
answers2911
viewsA: How to assign 3 values to 3 variables in only one python input line?
A first step would be to take the scores and call the method split to get a list where each element is one of the scores: pontuacoes = input("Digite as pontuações: ").split() Next, we have to turn…
pythonanswered Pedro von Hertwig Batista 3,434 -
1
votes3
answers359
viewsA: Numbers over 32, 64 bits
>>> import random >>> random.randint(0, 2**64) 6502449964907846195 Python automatically handles large numbers, and you don’t have to worry about the maximum bit value of a 32-bit…
pythonanswered Pedro von Hertwig Batista 3,434 -
1
votes2
answers214
viewsA: Count trading amounts performed with python replace
You increment the counter but do nothing with it. One way is to return it too: def corrigePalavra(str): palavra = [str[-1:], str[-2:], str[-3:], str[-4:]] result = str cont = 0 for w in palavra: if…
pythonanswered Pedro von Hertwig Batista 3,434 -
0
votes1
answer105
viewsA: API WEB error with flask
Remember that json is nothing more than a string. When you call p = pokedex(), is putting in p what its function pokedex returns, which is a Response flask with data being a json string, not a…
-
3
votes1
answer3104
viewsA: Python string corrupted with character
The line diret = "C:\\Users\\" + d_user is correct. What happens is that the \ is an escape character; that is, when you need for example to use quotes without finishing the string, you can do s =…