Posts by Fabiano • 555 points
23 posts
-
7
votes2
answers26379
viewsA: Returning only the highest value of a Python list
Use int as the sort function for the max function >>> l = ['4', '07', '08', '2017', '364', '355673087875675'] >>> max(l, key=int) '355673087875675'
-
5
votes3
answers9942
viewsA: Remove non-numeric characters from a Python string
An alternative to Marcelo’s answer is to make a Join only of the digit: >>> a = "0LÁM@UN@D0" >>> b = "A1B2C3" >>> ''.join(c for c in a if c.isdigit()) '00' >>>…
-
1
votes4
answers94
viewsA: Putting elements in lists
>>> lista = ['banana [amarela]\n', 'uva [vinho]\n', 'laranja [laranjado]\n', 'kiwi [verde]\n', 'framboesa [vermelho]\n', 'coco [marrom]\n'] >>> l1, l2 =…
-
0
votes4
answers5300
viewsA: How to fix the number of numeric characters in a float?
def trunca_sete(num): return '%.7s' % (str(num).ljust(7, '0')) trunca_sete(-3.1700000000419095) '-3.1700' trunca_sete(0.0) '0.00000' trunca_sete(1000.01) '1000.01' trunca_sete(1000.0199999)…
-
0
votes3
answers1598
viewsA: Countdown by using Tkinter
Your 'sec' variable is always taking the value that is in the input, so it seems that it is not updating. To illustrate, I started the variable as global. See: from Tkinter import* root = Tk() sec =…
-
4
votes3
answers4263
viewsA: Parse Nfe XML in Python
You need to add the namespace to the search string of the find function. Then change: nodefind = doc.find('NFe/infNFE/ide/cUF') for: nodefind =…
-
1
votes3
answers479
viewsA: Maximum occurrences in a python dictionary
>>> max(map(tuple, map(reversed, Meu_dic.items()))) (10, 'D') EDIT: I thought of another way: >>> max([i[::-1] for i in Meu_dic.items()]) (10, 'D')…
-
3
votes2
answers1057
viewsA: Add elements to a list inside a list compreension
Because you are adding in the understanding the return of the append function and the function returns None. Example: >>> print(result.append(i)) None But the result variable received the…
-
1
votes1
answer578
viewsA: Compare fields in two datasets
The way I know it, using pandas, would look like this: atual.where(~atual['CPF Favorecido'].isin(seguinte['CPF Favorecido'])).count() seguinte.where(~seguinte['CPF Favorecido'].isin(atual['CPF…
-
2
votes4
answers485
viewsA: How to determine the index of items in one list in another
To get the Result you want, you can do it as follows: >>> S_names = ["A", "B", "C", "D", "E", "F", "G"] >>> S_values =[1, 3, 4, 2, 5, 8, 10] >>> other = ["Z", "W", "B",…
-
0
votes2
answers1117
viewsA: How do I return a new instance of the python class itself dynamically inside it?
If I understand the problem, just return a new instance of the class itself. I changed some things in your class to run and show the result: class Time(object): def __init__(self, **kw):…
-
0
votes2
answers109
viewsA: Multiple keys and values, Dictionary compreension
{i: {'values':dictLines[i][-1], 'code':dictLines[i][0]} for i in dictLines} For a better view, use lib pprint: >>> import pprint >>> ret = {i: {'values':dictLines[i][-1],…
-
1
votes3
answers2390
viewsA: How to access a list resulting from one function in another?
Returns the list in function 1 and calls function 1 within function 2: def funcao1(): ... lista1 = [.....] return lista1 def funcao2(): #preciso de chamar a lista aqui lista = funcao1() funcao2() Or…
-
2
votes4
answers21246
viewsA: Doubt average with Python 3.5
from functools import partial notas = list(map(float,list(iter(partial(input, 'Nota: '), '')))) ac = list(map(float,list(iter(partial(input, 'AC: '), '')))) simulado = float(input('Nota simulado:…
-
0
votes2
answers272
viewsA: Items of B whose indices are the positions of occurrences of equal elements in A
>>> A = [12, 15, 10, 15, 12, 10, 10, 10, 15, 12, 12, 15, 15, 15] >>> B = [0.2, 0.3, 1.1, 0.2, 0.2, 0.7, 0.4, 0.6, 0.1, 0.3, 0.7, 0.4, 0.5, 0.5] >>> >>> D = {}…
-
0
votes1
answer73
viewsA: list indexes
>>> B_I = ['Cab', 'Bou', 'Bou', 'RFF', 'RF1', 'Rf2', 'Cor'] >>> Ba_F = ['Bou', 'Zez1', 'Zez2', 'Praca', 'Sro', 'Sro', 'Falag'] >>> Final = list(set(B_I+Ba_F)) >>>…
-
1
votes4
answers16229
viewsA: How to add entries to a dictionary?
d = {} v = raw_input('valor: ') d.update({len(d)+1:v}) Or put it in a bow: d = {} while True: v = raw_input('valor') if v == 'Q': break d.update({len(d)+1:v}) print d…
-
0
votes2
answers92
viewsA: Find the first line in the matrix with all positive elements, and the sum of these elements. Reduce all elements to that sum
>>> matrix = [[-5, -6, 2], [7, 2, 3], [8, 4, -9]] >>> matrix [[-5, -6, 2], [7, 2, 3], [8, 4, -9]] >>> for line in matrix: ... if not [i for i in line if i<0]: ... soma…
-
1
votes2
answers226
viewsA: Why does the following code print 'None' instead of the list?
You are assigning the return of the append method to a variable and printing, but this method returns nothing. append only adds a value at the bottom of the list. The correct thing is to do as @M8n…
-
2
votes1
answer379
viewsA: Vertical crossword
Your code is working for the word 'cat', but it will not work if the word search is in the last column ('Mkt', for example) because your comparison is being made in the next iteration of the column…
-
1
votes1
answer1609
viewsA: Iterate in multiple text files
If you want to count how many times the words appear, per file: import os strings = ['uma', 'frase'] files = [f for f in os.listdir("/tmp") if f.startswith('arq') and f.endswith('.txt')] for…
-
1
votes2
answers281
viewsA: Count occurrences in tuples
Word count is the basic example of map/reduce. In this case, just adapt the map step and implement the traditional reduce. Follow an example for mapper: mapper.py lista =…
-
2
votes1
answer3779
views