Posts by Paulo Marques • 3,739 points
198 posts
-
1
votes1
answer62
viewsA: Python cs50, what happened with this simple code
Expensive, The problem is the version of Python you are using. Some commands have changed from Python 2 to Python 3 Python 2 name = raw_input("Name: ") print("Hello, " + name) Note The command print…
-
0
votes3
answers464
viewsA: Identify amount of upper and lower case letters in a string
One possibility would be to "play" with internal functions and libraries. Let’s see how the map and class Counter map The function map applies a function to each element of an eternal object. The…
-
1
votes2
answers51
viewsA: Python- I need to make an application that receives float values and organize them without using . Sort
@soaresde, I believe you’re looking to implement a bubble sort, right? There are two problems with your script: Data entry Ordering loop Data entry while a > 0: a = float(input("Colque os…
pythonanswered Paulo Marques 3,739 -
0
votes1
answer108
viewsA: How to split a string into n equal parts in python 3.8 without using functions?
Hello Splitting string >>> bs = '0010010010001100' >>> n = 3 >>> while (len(bs) % n) != 0: ... bs = '0' + bs ... The variable n has the size of the parts where the string…
-
0
votes1
answer110
viewsA: print the smallest lexicographically possible string that can be obtained by removing at most 1 character from the string
I think that’s about what you’re looking for. >>> a = 'lolthisiscoolfreehackforya' >>> n = 0 >>> while ord(a[n+1]) < ord(a[n]) and n < len(a) - 1: ... n += 1 ...…
python-3.xanswered Paulo Marques 3,739 -
0
votes1
answer152
viewsA: How to verify and print repeating values in an algorithm vector
I think that’s what you seek. Update N = int(input("Quantos números? ")) lista = [] for i in range(1, N+1): lista.append(float(input(f"{i} - Entre com o número: "))) NOTE: Typing errors are not…
-
1
votes1
answer297
viewsA: Error 1064 mysql in UPDATE function
I believe the problem lies in the formation of command with values. Once you’ve tagged python-3.x in the question I would suggest using f-string as below: value_id = 10 value_column = 'nome' ID =…
-
4
votes4
answers201
viewsA: How to reduce Python code 3?
If ficar pequeno is premise X = int(input("Digite um número inteiro: ")) print([o+X for o in list(range(12)) if ((o+X) % 2) == 1]) Update: I used list comprehension. If you want to know more about…
-
0
votes1
answer38
viewsA: Use a 30 x 3 array using class, def, parameters - Python
Based on the comments I understood the question. A programmer-defined class is a type mutable Read this post for more details. Modify the function servicos_prestados for def…
-
0
votes1
answer52
viewsA: copy text from pygame
Based on what we talked about in the comments. Pygame In the pygame I don’t think it’s possible... Tkinter Is there any way to copy the label in the tkinter, however nay is the standard. See the…
-
1
votes2
answers258
viewsA: How to divide the value of an element of a column by delimiter (p.e "|") in pandas?
Opa, You can do as below: Creating the Dataframe >>> import pandas as pd >>> df = pd.DataFrame({"coluna1": ["ola|52", "hey", "sou", "ja", "da|5", "24g"]}) >>> df…
-
0
votes2
answers509
viewsA: Filter data from a Dataframe pandas by a specific column and the last four dates of a set of dates
UPDATED FROM THE COMMENTS I hope I understand the problem. Let’s cut to the chase: Creating the dataframe A >>> import pandas as pd >>> dfA = pd.DataFrame({"sinal": ["SP1", "BH",…
-
2
votes1
answer107
viewsA: Replace data from one column by considering data from another in a Dataframe
Straightforward. Creating the Dataframe >>> import pandas as pd >>> df = pd.DataFrame({"A": [1, 2, 3, 4, 5, 6], "B": ["um", "dois", "a", "a", "cinco", "seis"], "C": ["a", "b",…
-
1
votes1
answer70
viewsA: Variable value is changing automatically
Expensive, Quickly, as you said in the comment: "you created a reference and not a copy. Use Xp = tuple(np.copy(X))" Explanation As I put it in the comment, you created a reference to the existing…
-
0
votes2
answers287
viewsA: What precautions should I take when naming a Python file?
When naming a file, that is, a module, the programmer should pay attention to the name itself and its form. The name of the module SHOULD NOT have the same name as a library that will be used in…
-
1
votes1
answer54
viewsA: Trying to access a value inside a dictionary in Python, but I get an error when printing run the code
The mistake happens because in your loop, pk is the name of the key, thus a string. To string can only have whole index. >>> a = "Teste" >>> a["stage"] Traceback (most recent call…
-
1
votes2
answers41
viewsA: How to get the X coordinate if the Y condition is met with Numpy?
I believe I don’t have a numpy method to do this. Certainly with pandas it would be easier. But since you want to do with numpy, I believe the way is: >>> import numpy as np >>>…
-
2
votes3
answers148
viewsA: Count recurrences in a list
Let’s use what’s good about Python... :) See the example below: >>> from collections import Counter >>> lista = [1, 1, 2, 3, 4, 4, 2, 2] >>> c = Counter(lista)…
pythonanswered Paulo Marques 3,739 -
0
votes2
answers189
viewsA: filter dataframe by python line
In the example below I created a small DataFrame containing two columns and four rows. The date of 3/10/2020 repeats twice and it is she who will be filtered. Creating the Dataframe >>>…
-
1
votes5
answers312
viewsA: Make a function that calculates the following sum:
I hope the code below helps: def fatorial(n): if n == 0 or n == 1: return 1 return n * fatorial(n - 1) NUMERO = 10 SOMA = True num_dem = zip(reversed(range(1, NUMERO + 1)), range(1, NUMERO + 1)) S =…
-
1
votes1
answer63
viewsA: How to sort with Elasticsearch while maintaining search relevance?
If you had passed the return list of Elasticserach it would be easier to answer, but I believe by modifying the line below, you will have the result you want. buscaproduto: list = s.execute() for…
-
4
votes2
answers238
viewsA: Python dictionary and functions
It always causes confusion. The * and ** are for "unpack" the received variable in the function. For me, the unpacking word can cause confusion. Usually used as *args and **kwargs, what they do is:…
-
3
votes2
answers584
viewsA: How do I draw random names from each list without repeating them
You can remove the drawn item from the list. See below: from random import choice from time import sleep lista = [] j1 = ['Jose', 'Bruno', 'Lucas', 'Eduardo', 'Pedro', 'Luciano', 'Vitor', 'Diego',…
-
0
votes1
answer118
viewsA: Web Scraping with Pandas - How to treat values that are null in the collection and how to concatenate two columns in the final result?
Without knowing the structure of the web page that you are collecting the data, it becomes complicated to help. However, I believe that the logic used is the problem. See the example below: from bs4…
-
1
votes1
answer20
viewsA: Matrix list-to-list conversion doesn’t even work using numpy, how could I be doing this conversion?
That’s what you got >>> X = 10 >>> matriz = [] >>> for i in range(X): ... matriz.append([]) ... >>> for l in range(0, X): ... for c in range(0,1): ...…
python-3.xanswered Paulo Marques 3,739 -
1
votes1
answer27
viewsA: How do I get other users to access my Mysql database with Python?
The issue here is not exactly Python’s. The problem is that access is being made to localhost:3306. Localhost is the address of loopback, in other words, it is the machine itself. I believe in the…
-
0
votes2
answers489
viewsA: Compare two Dataframe and create a new Dataframe
Based on the example given, the solution would be to use the method merge >>> dfNovo = pd.merge(df1, df2, on=['x', 'y', 'z', 'w']) >>> dfNovo x y z w var1_x var2_x var3_x classe_x…
-
1
votes1
answer694
viewsA: Python - Dataframe - Create a new Dataframe from comparing two other Dataframe
I believe the method merge will solve the problem import pandas as pd df3 = pd.merge(df1, df2, on=['x', 'y', 'z', 'w']) For more details see the documentation here Note: any other column that has…
-
1
votes1
answer308
viewsA: How to create a condition for each dataframe subset in pandas?
See the example below, I hope it helps you solve >>> import pandas as pd >>> df = pd.DataFrame({"A": [1,1,2,2], "B": [2,2,3,3]}) >>> df[(df.A == 2)] A B 2 2 3 3 2 3…
-
2
votes2
answers83
viewsA: How to consult with Elasticsearch-dsl?
I believe the use of the method query be mistaken. The ideal would be to use Multimatch. Try to use from elasticsearch_dsl import MultiMatch (...) result = s.MultiMatch("match", query='Computador…
-
3
votes3
answers515
viewsA: Sum of factorial in a specific range
As I didn’t notice any prerequisite in the question. I believe that the most direct way would be to use Python resources for this. In this case the recursion. >>> def fatorial(n): ... if n…
pythonanswered Paulo Marques 3,739 -
1
votes2
answers392
viewsA: concatenate python csv files
If you really want to do it with Python, it would be something like txt = '' for arquivo in contatenar: with open(arquivo, 'r') as f: txt += f.read() with open("arquivo_final.csv", "w") as saida:…
-
0
votes1
answer28
viewsA: Dynamically merge lines that share the same key into one
Opa, I believe that with the example below you can unroll. Specifically answering part of your question How can I combine a dynamical number of columns whose name starts with the same value in…
-
3
votes3
answers82
viewsA: How do I differentiate an int and float value for my program
The most elegant way to test yourself is by using builtin isinstance. Her use is isinstance(VARIAVEL, CLASSE) Update: I used the term classe to understand that it is not necessarily a type primary.…
pythonanswered Paulo Marques 3,739 -
2
votes1
answer21
viewsA: " 'case' is not a Valid Java identifer"
Alexsander, Straightforward, case is a reserved word and cannot be used in the package name. See the full list in the documentation here I hope I’ve helped…
-
4
votes2
answers246
viewsA: How to read Mysql data with Python?
The method you’re looking for is the fetchall() rows = cursor.fetchall() UPDATE: rows will be as a list, which can be iterated as below for row in rows: print(row["CAMPO_AQUI"]) # Também pode usar o…
-
2
votes2
answers43
viewsA: doubts in the creation of dataframe pandas
The solution is simple use: df = pd.DataFrame([dicionario]) However, this is because there is only one line. Normally you would create Dataframe as the example below: >>> dicionario =…
-
1
votes2
answers37
viewsA: What is the syntax error in my function?
Carlos, The for is not correct. I believe that what you want is the below. def Grow_population_rate(year_1, year_2, City_name, pop_1, pop_2): pop_diff = pop_2 - pop_1 year_diff = year_2 - year_1…
-
0
votes1
answer278
viewsA: Relate two Dataframes in pandas and return value in python
Expensive, I was in doubt with your placement below. The only columns in common is the login column, but they have different names. The login column in the history table is the CUSUAR_INCL_REG…
-
0
votes1
answer49
viewsA: How do I import the missing library? Google Calendar Python API
If this is your entire code, it is missing to load Ibraries and instantiate service Something like: from googleapiclient.discovery import build service = build('calendar', 'v3',…
-
2
votes1
answer47
viewsA: What is the difference between split(" ") and split()
If you give the command below print(help(palavra.split)) you will see the result below Help on built-in function split: split(sep=None, maxsplit=-1) method of builtins.str instance Return a list of…
python-3.xanswered Paulo Marques 3,739 -
0
votes1
answer88
viewsA: How to treat columns with the same name in csv file
Very strange. Pandas already treat it. Behold here create a file teste.csv containing a,b,a 1,2,3 Then use in Python >>> import pandas as pd >>> df = pd.read_csv(open("teste.csv",…
-
2
votes1
answer52
viewsA: How to merge these 2 dataframes and filter the latest timestamp to repeated values?
If this is the real case, the solution would be to carry out the following steps: merge of the two dataframes using the columns MRN, Encounter ID, First Name, Last Name and Birth Date with:…
-
1
votes1
answer64
viewsA: ERROR 10043 - Protocol not configured in system
Expensive, By mistake [WinError 10043], I believe you are running the code on Windows. See in the source code of socket.py line 133 refers to this error. It is within the if that checks if the…
-
0
votes2
answers66
viewsA: Python - Create dynamic variable for firebase
@Giovane, If the library you are using is firebase. Use something like below: from firebase import firebase firebase = firebase.FirebaseApplication('URL of Database', None) data = { 'Name': 'John…
-
0
votes1
answer25
viewsA: How to use a custom tag within an IF tag?
I believe you are asking about JINJA, the template language of Django. And the wish is to use a different HTML tag depending on a condition Just use something like: <ul> {% if athlete_list %}…
-
2
votes1
answer76
viewsA: How to load whole values from a txt file? - Python
@Userdel, The variable name has characters that cannot be converted to number. In this case [, ] and , Ideal is to clear your variable before converting. Using the command below, you will remove a…
-
1
votes1
answer33
viewsA: Find photo in Drive
From what I understand, you need the PIL to open an image that is on a link. For that, I believe the bilioteca requests associated with PIL would help in this. from PIL import Image import requests…