[Solved] Turning a list into a dictionary?
I believe you want a dictionary with a single key and a list: l = [1,2,3,4,5] d = {l[0]:l[1:]} Output: {1: [2, 3, 4, 5]} 0 solved Turning a list into a dictionary?
I believe you want a dictionary with a single key and a list: l = [1,2,3,4,5] d = {l[0]:l[1:]} Output: {1: [2, 3, 4, 5]} 0 solved Turning a list into a dictionary?
res = [] for item in s.split(‘ ‘): … try: … int(item) … res.append(item) … except: … res += list(item) 5 solved How to separate string and integers to a list [closed]
Issues with Code : Object creation is not correct for password class. also argument self is not passed to class method . Fixed Code class password: def pas1(self): pas = [] tedad = int(input(‘how many do you have information ? : ‘)) for i in range(1,tedad): b=input(‘enter : ‘) pas.append(b) print(‘this is your pas —> … Read more
if you need to count characters in string, try the following a = “aaabbcccd” b = dict.fromkeys(a, 0) for i in a: b[i] += 1 b now holds the counts you require: {‘a’: 3, ‘c’: 3, ‘b’: 2, ‘d’: 1} solved Counting the occurrence of unique letters in a string in Python 3
[EDIT]The question was updated and the below was answered based on the original question. if you fix your input var1 then you can do something like this: var1 = ‘{“text”:”tool”,”pos”:”\\xd1\\x81\\xd1\\x83\\xd1\\x89\\xd0\\xb5\\xd1\\x81\\xd1\\x82\\xd0\\xb2\\xd0\\xb8\\xd1\\x82\\xd0\\xb5\\xd0\\xbb\\xd1\\x8c\\xd0\\xbd\\xd0\\xbe\\xd0\\xb5″}’ md = {} for e in var1[1:-1].split(‘,’): md[e.split(‘:’)[0][1:-1]] = e.split(‘:’)[1][1:-1] md[‘pos’] = (bytes.fromhex(”.join([h for h in md[‘pos’].split(‘\\x’)]))).decode(‘utf-8’) print(md) output: {‘text’: ‘tool’, ‘pos’: ‘существительное’} 2 solved … Read more
try setx PATH “%PATH%;C:\Python34\Scripts” if it is not working install your python modules like this python -m pip install [packagename] solved Cannot install pip install numpy [duplicate]
The above code can be rewritten as: indices = [] for n, value in enumerate(self.BUSES[i]): if value==1: indices.append(n) enumerate returns a pair of (index, value at that index) for a given list. So you are testing if value at a given index is 1, and if that is true, you add the index to indices. … Read more
You should try to code something before posing on Stack Overflow. This will work but won’t catch any error conditions. options = [ {‘vehicle’: ‘Car’, ‘destination’: ‘Ghana’, ‘coeff’: 0.3}, {‘vehicle’: ‘Van’, ‘destination’: ‘Nigeria’, ‘coeff’: 0.2}, {‘vehicle’: ‘Truck’, ‘destination’: ‘Togo’, ‘coeff’: 0.33}, {‘vehicle’: ‘Van’, ‘destination’: ‘Kenya’, ‘coeff’: 0.17}, {‘vehicle’: ‘Truck’, ‘destination’: ‘Somalia’, ‘coeff’: 0.31}, ] while … Read more
Just index the list/string. It’s the same kind of syntax for both lists and strings. >>> s=”abcdefg” >>> s[0] + s[1] # This does the operation ‘a’ + ‘b’ ‘ab’ Lists and strings are both sequence types in Python and sequences in Python are 0-indexed. This means the first item is at index 0, the … Read more
Sure: excludes = [‘Q’] prev = None out = [] for item in my_list: if item != prev or item in excludes: out += item prev = item print(out) 2 solved Is there a way of deleting specific adjacent duplicates in a list on python 3? [closed]
The question is not stated very clearly, but I would assume that those two functions are in some class. The problem is that you create a wrapper inside the class. Your wrapper check_if_logged expects two arguments self and function. When you use it as wrapper @ you give only one argument to the function check_if_logged … Read more
Every integer is divisible by itself and by 1. If the integer is composite, it will have at least one other pair of divisors (which may be the same, as in the case of 4, which is divisible by 1,4 and 2,2). lim = int(math.sqrt(num)) for i in range (1, lim): if num%i==0: print(i, int(num/i)) … Read more
Some info on enumerate by typing help(enumerate) enumerate(iterable[, start]) -> iterator for index, value of iterable Return an enumerate object. iterable must be another object that supports iteration. The enumerate object yields pairs containing a count (from start, which defaults to zero) and a value yielded by the iterable argument. enumerate is useful for obtaining … Read more
Consider: print “hello world” The above statement is OK when you are using Python 2.x, because in Python 2.x, print is a statement. But in Python 3.x, print is a function and there is no way to turn it back into a statement. So you must use parentheses. So for Python 3.x, the answer is: … Read more
I don’t know Python but this is Java so hopefully you can translate it. I put the board into a two-dimensional array of int in the form board[1][2] = 16 where the class on that tile would be tile tile-16 tile-position-1-2 tile-new. I know that’s not what you asked for but you didn’t give enough … Read more