[Solved] Python. Even-numbered lines in text file
So, just output the even lines. i = 1 f = open(‘file’) for line in f.readlines(): if i % 2 == 0 : print line i += 1 6 solved Python. Even-numbered lines in text file
So, just output the even lines. i = 1 f = open(‘file’) for line in f.readlines(): if i % 2 == 0 : print line i += 1 6 solved Python. Even-numbered lines in text file
You need to do the assignment yourself (or there is no point in learning to program) and if you don’t understand the question, you should ask your teacher for clarification. That said, shifting is quite simple in principle. You can do it by hand. If you have a letter, say A, shifting it by 1 … Read more
You can get rid of the exponential form with this: a=3.8485341e-06 ‘{0:.20f}’.format(a) or this print(“%.20f” % a) solved How can I round long decimal numbers into 0.00 format [duplicate]
You may open k and not ‘k’ imports = [“import json”, “import defaultdict”] d = {‘1’: ‘print (“This is test file”)’, ‘2’: ‘print (“This is test file”)’} for k, v in d.items(): with open(k + “.py”, ‘w’) as fr: for imp in imports: fr.write(imp + “\n”) fr.write(v) solved How to create a file with filename … Read more
Using the clear-method on a list also affects all references to it, e.g. >>a = [1, 2, 3] >>b = a >>a.clear() >>print(‘a=”,a) a = [] >>print(“b =’,b) b = [] So what you are doing in ad_list.append(tempAdList) is to repeatedly add references to the same object to ad_list, i.e. each time you update tempAdList, … Read more
The first behaviour you list is mathematically called the signum operation. If you are allowed to use numpy, i’d simply do: import numpy sign = numpy.sign(x) With regard to your 2nd question, that’s quite easy. Simply use: int(bool(x)) Edit: With some tinkering i found a solution for your first question, too: negsign = int(int(num) >> … Read more
The date class defines the __format__() magic method, which is called by str.format() to produce a “formatted” string representation of an object. To quote the documentation for date.__format__(): Same as date.strftime(). This makes it possible to specify a format string for a date object in formatted string literals and when using str.format(). For a complete … Read more
The easiest way is to check how nums looks like after every operation (with print(nums) in scratch or calling it (nums) in terminal). Next time, try it yourself. In A: With the first line (nums[:k] = nums[len(nums)-k:]): nums = [5, 6, 7, 4, 5, 6, 7] – you’ve substituted nums[0:3] (1, 2, 3) with nums[4:7] … Read more
As @Hitobat mentioned in commend – you have list with dictionary inside so you have to use [0] to get this dictionary. Or you have to use for-loop if you have more elements on list data = [{‘_id’: ‘5f563c1bf8eaa9d98eca231f’, ‘allEnabledDs’: None, ‘allEnabledIdSor’: None, ‘correlationFilterEntitySource’: True, ‘created_at’: ‘2020-09-07T13:56:43.469Z’, ‘dsConnectionList’: None, ‘folderToLabelMapping’: None, ‘idConnectionList’: None, ‘identityResolutionScan’: False, … Read more
You can get the numerical index of the last item with the documented index command, passing in the special string “end”: last_index = the_listbox.index(“end”) To get the value of the last item you can use “end” in the get method as well: last_value = the_listbox.get(“end”) To make sure that the last item in the listbox … Read more
text = “Hello World”.lower() li = [] for i in text: if i!=’ ‘: li.append(str(ord(i)-96)) print(‘.’.join(li)) This should work solved Convert String to numbers separated by dots where a=1 and z=26
while not the most elegant solution this does what you describe. import csv with open(‘test.csv’, ‘rb’) as myFile: reader = csv.reader(myFile, delimiter=”,”, quotechar=”|”) for row in reader: print row[4] + ‘ 1:’ + row[0] + ‘ 2:’ + row[1] + ‘ 3:’ + row[2] + ‘ 4:’ + row[3] OUTPUT: a 1:43 2:674 3:345 4:547 … Read more
Solution for this problem import json inp = {} cout = {} def flatrn_dict(inp, name, pk_name): cop_in = inp.copy() pkn, pkv = identify_id(cop_in) for k, v in cop_in.items(): if type(v) == dict: if pkv is not None: v.update({pkn: pkv}) flatrn_dict(v, k, name) inp.pop(k) elif type(v) == list: for vx in v: if pkn is not … Read more
Go through every class in data_struct, finding a John, and get its “id”. Notice “a” John. What if you have more than one? There are a lot of Johns in the world. So, make sure you’re thinking about what “John” means in your design. If there are only one of each name, it may be … Read more
import re txt = “””div><div class=”ShLswe”><a class=”_2WFi0x” href=”https://stackoverflow.com/order_details?order_id=OD40818094004&item_id=OD408180940040000&unit_id=OD408180940040000000″><div class=”row”><div class=”col-6-12″><div class=”row”><div class=”col-3-12″><div class=”J2h1WZ”><div class=”_3BTv9X” style=”height: 75px; width: 75px;”><img class=”_1Nyybr _30XEf0″ alt=”” src=”https://rukminim1.flixcart.com/image/75/75/usb-adaptor/s/9/8/tp-link-150-mbps-wireless-n-original-imad8rruefj6rf3y.jpeg”></div></div></div><div class=”col-8-12″><div class=”_3D-3p2″><span class=”row _13y4_y _1iu0PI”>TP-LINK 150 Mbps TL-WN721N Wireless N</span><div class=”row _3i00zY”><span class=”_3i00zY _2n1WrW”>Seller: </span><span class=”_2dTbPB”>WS Retail</span></div></div></div></div></div><div class=”col-2-12 JL36Xz”>₹512</div><div class=”col-4-12 _3Yi3bU”><div><div class=”_30ud5x _3ELbo9″></div><span class=”_7BRRQk”>Delivered on Aug 20, 2014</span><div class=”_2t-3dH”>Your item has been delivered</div></div><div … Read more