Category : list

I have a csv file (uneditable), which looks like this: fruit,price,amount apple,1,2 banana,3,4 kiwi,5,6 that I’ve read into a list of dicts: goodies = [] with open(blabla) as file: reader = csv.DictReader(file) for fruit in file: goodies.append(fruit) It looks like this: goodies = [{‘fruit’ : ‘apple’, ‘price’ : ‘1’, ‘amount’:’2′}, {‘fruit’ : ‘banana’, ‘price’ : ..

Read more

i have written this code to write the contents of the list in a text file def writefile(daymonths): filename = "/Users/admin/Documents/Month.txt" outputfile = open(filename, "w") months = ["{}n".format(month) for month in daymonths] outputfile.writelines(months) outputfile.close() but the file shows only one last item, i have tried using outputfile.write(), but still not showing whole list. textfile screenshot ..

Read more

I meet a interview question to implement following function: def f(sequence_type, element): pass for example: f(str, ‘a’) return ‘a’ f(list, ‘a’) return [‘a’] f(list, ‘ab’) return [‘ab’] f(list, 1) return [1] f(tuple, 1) return (1, ) f(set, 1) return {1} f(str, 1) return ‘1’ f(str, [1]) return ‘[1]’ I came up a naive method: def ..

Read more

I meet a interview question to implement following function: def f(sequence_type, element): pass for example: f(str, ‘a’) return ‘a’ f(list, ‘a’) return [‘a’] f(list, ‘ab’) return [‘ab’] f(list, 1) return [1] f(tuple, 1) return (1, ) f(set, 1) return {1} f(str, 1) return ‘1’ f(str, [1]) return ‘[1]’ I came up a naive method: def ..

Read more