(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to split a list into different variables. Sample Solution Python Code: color = [(“Black”, “#000000”, “rgb(0, 0, 0)”), (“Red”, “#FF0000”, “rgb(255, 0, 0)”), (“Yellow”, “#FFFF00”, “rgb(255, 255, 0)”)] var1, var2, var3 = color print(var1) print(var2) print(var3) Sample Output: (‘Black’, ‘#000000’, …
Month: February 2021
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to create multiple lists. Sample Solution Python Code: obj = {} for i in range(1, 21): obj[str(i)] = [] print(obj) Sample Output: {‘1’: [], ‘8’: [], ’14’: [], ‘5’: [], ’17’: [], ‘9’: [], ‘2’: [], ‘7’: [], ’16’: [], …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to split a list based on first character of word. Sample Input: word_list = [‘be’,’have’,’do’,’say’,’get’,’make’,’go’,’know’,’take’,’see’,’come’,’think’, ‘look’,’want’,’give’,’use’,’find’,’tell’,’ask’,’work’,’seem’,’feel’,’leave’,’call’] Sample Solution Python Code: from itertools import groupby from operator import itemgetter word_list = [‘be’,’have’,’do’,’say’,’get’,’make’,’go’,’know’,’take’,’see’,’come’,’think’, ‘look’,’want’,’give’,’use’,’find’,’tell’,’ask’,’work’,’seem’,’feel’,’leave’,’call’] for letter, words in groupby(sorted(word_list), key=itemgetter(0)): print(letter) for …
Python Example – Write a Python program to convert a list of multiple integers into a single integer
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to convert a list of multiple integers into a single integer. Sample Solution Python Code: L = [11, 33, 50] print(“Original List: “,L) x = int(“”.join(map(str, L))) print(“Single Integer: “,x) Sample Output: Original List: [11, 33, 50] Single Integer: 113350 …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to find common items from two lists. Sample Solution Python Code: color1 = “Red”, “Green”, “Orange”, “White” color2 = “Black”, “Green”, “White”, “Pink” print(set(color1) & set(color2)) Sample Output: {‘Green’, ‘White’} Write a Python program to find common items from …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to generate all sublists of a list. Sample Solution Python Code: from itertools import combinations def sub_lists(my_list): subs = [] for i in range(0, len(my_list)+1): temp = [list(x) for x in combinations(my_list, i)] if len(temp)>0: subs.extend(temp) return subs l1 = …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to check whether a list contains a sublist. Sample Solution Python Code: def is_Sublist(l, s): sub_set = False if s == []: sub_set = True elif s == l: sub_set = True elif len(s) > len(l): sub_set = False else: …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to count the number of elements in a list within a specified range. Sample Solution Python Code: def count_range_in_list(li, min, max): ctr = 0 for x in li: if min <= x <= max: ctr += 1 return ctr list1 …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to get the frequency of the elements in a list. Sample Solution Python Code: import collections my_list = [10,10,10,10,20,20,20,20,40,40,50,50,30] print(“Original List : “,my_list) ctr = collections.Counter(my_list) print(“Frequency of the elements in the List : “,ctr) Sample Output: Original List : …
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to get unique values from a list. Sample Solution Python Code: my_list = [10, 20, 30, 40, 20, 50, 60, 40] print(“Original List : “,my_list) my_set = set(my_list) my_new_list = list(my_set) print(“List of unique numbers : “,my_new_list) Sample Output: Original …