Questions tagged as 'python-3.x'

1
answer

Count how many times a word in a file appears in another file

I would like to count how many times a list of words (file1) appears in another list of words (file2) with open("corpus.txt", "r") as f1, open("lexico.txt", "r") as f2: file1 = f1.read() file2 = f2.read() corpus1 = file1.split(" "...
asked by 26.09.2017 / 12:04
1
answer

How to install Python 3

I'm new to Ubuntu and I'm learning Python. I have installed Pyhton 2.7.12 on my pc and would like to know how I can upgrade to version 3. I'm doing a course where I need to make use of a lib. import urllib.request But the following er...
asked by 02.09.2016 / 12:44
2
answers

Calculate the divisors of a number in Python in a performatic way

The code I'm using is: import time def divisores(num): for i in range(1, int(num/2+1)): if num % i == 0: yield i yield num inicio = time.time() d = divisores(47587950) [print(i) for i in d] print("Fim da execuçã...
asked by 25.03.2017 / 19:32
1
answer

If possible, how can I work with integer values with 1 million digits or more in python?

I'm running some experiments with prime numbers and needed to process huge integers, however when trying to process a small routine an error occurs in the following line:   OverflowError: int too large to convert to float How can I...
asked by 14.09.2018 / 13:20
2
answers

How to change the value of a variable through function in Python?

How can I change the value of a variable through a function? Here's an example of what you'd like to do: def func(t): t = 2 + 3 t = 7 func(t) print(t) The output of the print (t) function returns me the value 7 and not 5 as the desired...
asked by 04.08.2017 / 22:46
3
answers

Search for tuple in an array

I have the following vectors: a = [('FI', 'SE'), ('SE', 'DK'), ('DK', 'DE'), ('DE', 'PL'), ('PL', 'BY')] b = [['NL', 'DK', 0], ['NL', 'DE', 0], ['NL', 'BE', 0], ['FI', 'SE', 0.054]] I need to go through the a vector and find it in the ve...
asked by 02.05.2018 / 20:00
1
answer

Extract index for repeated elements in lists in Python

If I have a x list that contains multiple values, and a few repeats, and I want to have the index for each element, how can I do it? I tried doing it using the .index() method but it only returns the index of the first element. &...
asked by 17.09.2016 / 17:26
1
answer

Hangman Game in Python

I'm doing a hangman game in Python. Every loop the program asks the letter or the word: #Jogo perguntarNovamente = True game_on = True while game_on: palavra_secreta = palavra() senha_list = [l for l in palavra_secreta] chances =...
asked by 17.09.2016 / 05:03
2
answers

Divide a list into n sublists

I have the following list: l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] What I need is to split this list into n sublists, in this case you can not do it manually since n is going to be dynamic.     
asked by 16.08.2016 / 23:12
1
answer

Reordering of dictionary, exchange values with keys

I have the following dictionary: tests_dict = {'test 1': ['Manuel', 'Mariana', 'Filipa'], 'test 2': ['Manuel', 'Filipa', 'Mariana'], 'test 3': ['Mariana', 'Manuel', 'Filipa']} What I need is that the keys of the new dictionary are the names...
asked by 11.12.2016 / 10:19