[Solved] What is wrong with my class code [Python]?

[ad_1] You need to pass argument to your constructor. s1 = Square(1) # will call Square.__init__(self, 1) s2 = Square(2) # will call Square.__init__(self, 2) It’s not a big problem. Update I rewrite your class: import math class Square(): def __init__(self, length): #return an instance of this class with ‘length’ as the default length. self.length … Read more

[Solved] putting float numbers in lists [closed]

[ad_1] Just create lists: somelist = [p1, p2] You probably want to append those to another data structure initialized outside of your loop: somelargerlist = [] for presumed_loop_variable in presumed_loop_not_shown: p1 = … p2 = … somelist = [p1, p2] somelargerlist.append(somelist) 17 [ad_2] solved putting float numbers in lists [closed]

[Solved] Python dictionary operations

[ad_1] Yes, you can do this recursively, if all you have is lists and dictionaries, then that is pretty trivial. Test for the object type, and for containers, recurse: def unwrap_data(obj): if isinstance(obj, dict): if ‘data’ in obj: obj = obj[‘data’] return {key: unwrap_data(value) for key, value in obj.items()} if isinstance(obj, list): return [unwrap_data(value) for … Read more

[Solved] Python if and else

[ad_1] You don’t want to use the function random.choice in your if statement. Save the random name as a variable and check it. import random random_choice = [‘Noob’, ‘Average’, ‘Pro’, ‘Expert’] name = input(‘What is your gamername? ‘) random_name = random.choice(random_choice) print(name, ‘is a’, random_name, ‘Gamer’) if random_name == ‘Noob’: print(‘Im afraid there is nothing … Read more

[Solved] use dictionary as a list in python

[ad_1] Maybe you can consider using random.sample like so. >>> import random >>> p = 2*[‘a’] + 3*[‘b’] + 4*[‘c’] + 5*[‘d’] >>> random.sample(p, 3) [‘b’, ‘b’, ‘a’] From the docs, random.sample returns a k length list of unique elements chosen from the population sequence or set. It is used for random sampling without replacement. … Read more

[Solved] Creating a dictionary based on a word [closed]

[ad_1] from collections import defaultdict my_word=”apple” d = defaultdict(list) for a,b in zip(my_word,my_word[1:]): d[a].append(b) maybe?… collections.Counter might be usefull as well 0 [ad_2] solved Creating a dictionary based on a word [closed]

[Solved] Python. Converting string into directory [closed]

[ad_1] This is a very weird way to handle data. I would use a nested dicts: exampleDictionary = {‘thing on ground’: {‘backpack’: {‘tea’: ‘Earl Grey’}}} print exampleDictionary[‘thing on ground’] print exampleDictionary[‘thing on ground’][‘backpack’] print exampleDictionary[‘thing on ground’][‘backpack’][‘tea’] Outputs: {‘backpack’: {‘tea’: ‘Earl Grey’}} {‘tea’: ‘Earl Grey’} Earl Grey 2 [ad_2] solved Python. Converting string into directory … Read more

[Solved] How i can convert ISO time format to yyyy-mm-dd hh:mm:ss using python [closed]

[ad_1] Try this, >>> import datetime >>> datetime.datetime.now().strftime(‘%d-%m-%Y %H:%M:%S’) ’03-08-2019 12:43:16′ If you max_startdate is string, >>> max_startdate=”2019-08-03 01:08:58.155000″ >>> max_startdate.split(‘.’)[0] ‘2019-08-03 01:08:58’ 3 [ad_2] solved How i can convert ISO time format to yyyy-mm-dd hh:mm:ss using python [closed]

[Solved] Regular expression in python string [closed]

[ad_1] This works: import re s=””‘customer is given as per below customer are given not priority below given given below customer below customer information given customer is given not as per below”’ matches = re.findall(‘customer \S+ given \S+\s\S+ below’, s) for match in matches: print(match) 14 [ad_2] solved Regular expression in python string [closed]