[Solved] How can i read a csv based on data in other columns? [closed]

Using the built-in csv library to iterate over each row’s values will do the trick: import csv with open(‘data.csv’) as csvfile: csvin = csv.DictReader(csvfile) for row in csvin: if row[‘col2:’] == “true” and row[‘col3:’] == “false”: print(row[‘col1:’]) Output result: 1 0 solved How can i read a csv based on data in other columns? [closed]

[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] write() file by inserting dict in specific key order and format

Probably the closest short of looping and building a string, is something using pprint.pformat, such as: >>> from pprint import pformat >>> my_dct = dict( k1=1, k3=3, k2=2,) >>> print(‘my_dct = {{\n {}\n}}’.format(pformat(my_dct, width=1)[1:-1])) my_dct = { ‘k1’: 1, ‘k2’: 2, ‘k3’: 3 } solved write() file by inserting dict in specific key order and … 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] There is no error but nothing is printed, but if I print inside the while loop then it is printed multiple times. How do I fix this? [closed]

There is no error but nothing is printed, but if I print inside the while loop then it is printed multiple times. How do I fix this? [closed] solved There is no error but nothing is printed, but if I print inside the while loop then it is printed multiple times. How do I fix … Read more

[Solved] How do I loop this? [closed]

You almost certainly want to replace your specific variables for each nation with a data structure (such as a dictionary) that uses the nation’s name as a key. So, rather than referring to USA_REGION_DATA you’d look up REGION_DATA[“USA”]. This is extensible to any number of nations, as you can simply add new values to the … Read more

[Solved] How can I increase the i in a for?

Try using the “step size” argument to range or xrange: fd = open(dFile) lineas = fd.readlines() fd.close() for i in xrange(0, len(lineas), 6): print “CA:”+lineas[i]+”Q:”+lineas[i+1]+”A1:”+lineas[i+2]+”A2:”+lineas[i+3]+”A3:”+lineas[i+4]+”A4:”+lineas[i+5]; 0 solved How can I increase the i in a for?

[Solved] How to convert unequal list to nested dictionary using zip? [closed]

You can nest zips: studentName = [“kelly”,”Micheal”,”Agatha”,”Chris”,”frank”,”matthew”] subject = [“math”,”phy”,”chem”] math = [34,45,78,79,60,36] phy = [57,98,67,90,56,60] chem = [67,86,35,98,50,67] output = {name: dict(zip(subject, scores)) for name, scores in zip(studentName, zip(math, phy, chem))} print(output) # {‘kelly’: {‘math’: 34, ‘phy’: 57, ‘chem’: 67}, # ‘Micheal’: {‘math’: 45, ‘phy’: 98, ‘chem’: 86}, # ‘Agatha’: {‘math’: 78, ‘phy’: 67, … Read more