[Solved] what is the difference? Why the first one give me an error?

When you call dict() with an iterable it expects that iterable to return pairs of values, that it can use as keys and values to create the dict. So, these examples are all valid: dict([(‘key’, ‘value’), (‘other_key’, ‘other_value’)]) dict([‘ab’, ‘bc’, ‘dd’]) # ‘ab’ is pretty much equivalent to a list [‘a’, ‘b’] dict([[‘a’, 1], [‘b’, … Read more

[Solved] function returning an object in python [closed]

You can do this with whatever class you want, but here’s a quick example using namedtuple >>> from collections import namedtuple >>> Point = namedtuple(‘Point’, ‘x y’) >>> def my_func(): return Point(1, 9) >>> my_func().x 1 This code is completely useless though 8 solved function returning an object in python [closed]

[Solved] Python – How to Compare a column value of one row with value in next row

Use groupby (http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html) Assume your input is saved in a pandas Dataframe (or equivalently save it into csv and read it using pandas.read_csv). Now you can loop over the groups with same S.No values with the following: output = {} for key, group in df.groupby(‘S.No.’): # print key # print group output[key] = {} output[key][‘Details’] … Read more

[Solved] Taking 1 specific item out of JSON response using Python

Consider a simplified example where your line of code >>> response=conn.getresponse() has retrieved the response from the server and saved it as an HTTPResponse object, and when you .read() the response you see a JSON string like this >>> responseData = response.read().decode(‘utf-8’) >>> responseData ‘{“day”: “Thursday”, “id”: 3720348749}’ You can use Python’s built-in JSON decoder … Read more

[Solved] What does this piece of code mean?

The code loops through word with a step size of 3 and groups every 3 consecutive words. Let’s say word = [1, 2, 3, 4, 5, 6, 7, 8, 9] Over the course of the loop, i will be = 0, 3, 6 To grid, you append word[0:3],word[3:6],word[6:9] So grid will have in it [[1,2,3],[4,5,6],[7,8,9]] … Read more

[Solved] python string parse with @ and space [closed]

It is not very hard to write an expression for this. >>> import re >>> re.findall(r’@(\S+)’, ‘ I want to tell @susan and @rick that I love you all’) [‘susan’, ‘rick’] Or use \w which matches any word character. >>> re.findall(r’@(\w+)’, ‘ I want to tell @susan and @rick that I love you all’) [‘susan’, … Read more

[Solved] Are booleans overwritten in python?

If it was for i in range(1,10): if space_check(board, i): return False else: return True then after the first iteration in the for loop the function would return. This would not lead the the expected behaviour. Currently, you check every space, and not just the first one 9 solved Are booleans overwritten in python?

[Solved] What does this `key=func` part mean in `max(a,b,c,key=func)` in Python?

it allows to define a criterion which replaces the < comparison between elements. For instance: >>>l = [“hhfhfhh”,”xx”,”123455676883″] >>>max(l, key=len) ‘123455676883’ returns the longest string in the list which is “123455676883” Without it, it would return “xx” because it’s the highest ranking string according to string comparison. >>>l = [“hhfhfhh”,”xx”,”123455676883″] >>>max(l) ‘xx’ 2 solved What … Read more

[Solved] can someone explain how to use str.index and str.find and why the following code is wrong

Check the documentation https://docs.python.org/3/library/stdtypes.html#string-methods https://docs.python.org/3/library/stdtypes.html#str.index Build your code around the method: # characters to look for in a list (string would work as well) vowels = [“a”,”i”,”o”,”u”,”e”,”y”] # a function (method) def vowel_indices(word): # prepare empty list to collect the positions in hits = [] # test every of your vowels for vowel in vowels: … Read more