[Solved] How to do random shuffling in Python?

random.shuffle is your friend, according to the docs random.shuffle(x[, random]) . Shuffle the sequence x in place. import random #Convert string to list of chars li = list(‘ABDEB’) for i in range(5): #Shuffle the list, the last shuffled list is shuffled every time random.shuffle(li) #Convert list to string again and print print(”.join(li)) Output might look … Read more

[Solved] Python looping code

You miss the lines if grade != -1: numbers.append(grade) after grade = int(grade). This adds the grade to the list numbers. Review Use PEP8 (you can use pep8online.com) to check your code For unnamed input, I prefer “Largest is %i, smallest is %i” % (max(numbers), min(numbers)) You can use sum to sum up integers in … Read more

[Solved] Sort a list of number by last 2 digits

You can get the last two digits using the remainder operator, then use the digits as key of sorted: a = [311, 409, 305, 104, 301, 204, 101, 306, 313, 202, 303, 410, 401, 105, 407, 408] result = sorted(a, key=lambda x: (x % 100, x)) print(result) Output [101, 301, 401, 202, 303, 104, 204, … Read more

[Solved] Given a non-empty list of strings of the same length, how do I create a list of lists of the letters at each position in the string without using zip? [closed]

Given a non-empty list of strings of the same length, how do I create a list of lists of the letters at each position in the string without using zip? [closed] solved Given a non-empty list of strings of the same length, how do I create a list of lists of the letters at each … Read more

[Solved] What is the use of for in python [closed]

for is used to loop through the defined variable s, which in this case is a string containing letters, digits (numbers), and whitespace (spaces, tabs etc.). First time: ‘ Abs3 asdasd asd11 111 11ss’ ^ As we can see, the first character of the string is: “” (space). The code goes on to test if … Read more

[Solved] How to inner join two tables?

You say you want solutions in Python, MySQL or MongoDB. But you’ve tagged the question with “perl”. So here’s a Perl solution. #!/usr/bin/perl use strict; use warnings; my %file1 = get_file1(); open my $fh2, ‘<‘, ‘File2’ or die “File 2: $!\n”; chomp(my $header = <$fh2>); print “$header\tcol_F\n”; while (<$fh2>) { chomp; my $colA = (split … Read more

[Solved] How to print word that you want inside a textfile?

Use readlines and output the lines according to their index. with open(‘addon/entertainment.txt’) as f: lines = f.readlines() for line in lines: print(“{}. {}”.format(lines.index(line) + 1, line)) desired_lines = [3, 5] output = [] for desired in desired_lines: output.append(lines[desired – 1]) for o in output: print(“{}. {}”.format(output.index(o) + 1, o)) Alternatively, if you want to select … Read more

[Solved] Python – OS Module ‘TypeError: ‘bool’ object is not iterable” [closed]

path.exists() just returns True or False depending on if that path exists. You should first check its existence via exists(), then use os.listdir() or glob.glob() to actually get a list of items in that path, but only if exists() returned True. solved Python – OS Module ‘TypeError: ‘bool’ object is not iterable” [closed]