[Solved] how to map two list in a single list in python using zip() in list comprehension

You can use map and zip for this: >>> data = [[‘Ankur’, ‘Avik’, ‘Kiran’, ‘Nitin’], [‘Narang’, ‘Sarkar’, ‘R’, ‘Sareen’]] >>> list(map(‘ ‘.join, zip(*data))) [‘Ankur Narang’, ‘Avik Sarkar’, ‘Kiran R’, ‘Nitin Sareen’] 0 solved how to map two list in a single list in python using zip() in list comprehension

[Solved] Python Create list of dict from multiple lists [closed]

This is a perfect example of the zip function. https://docs.python.org/3.8/library/functions.html#zip Given: name = [‘Mary’,’Susan’,’John’] age = [15,30,20] sex = [‘F’,’F’,’M’] Then: output = [] for item in zip(name, age, sex): output.append({‘name’: item[0], ‘age’: item[1], ‘sex’: item[2]}) Will produce: [ {‘name’: ‘Mary’, ‘age’: 15, ‘sex’: ‘F’}, {‘name’: ‘Susan’, ‘age’: 30, ‘sex’: ‘F’}, {‘name’: ‘John’, ‘age’: 20, … Read more

[Solved] Split string on capital letter and a capital letter followed by a lowercase letter [closed]

I’m trying to give an answer that will help you understanding the problem and work on the solution. You have a string with capital letters and at some point there is a small letter. You want the string to be split at the position before the first small letter. You can iterate through the string … Read more

[Solved] Python merge element of the list [closed]

If you want to preserve the order of the first elements, something like this might work: from collections import OrderedDict def merge(seq): d = OrderedDict() for k,v in seq: d.setdefault(k, []).append(v) result = [[k]+v for k,v in d.iteritems()] return result This loops over each pair in the sequence. For each k, we either get the … Read more

[Solved] How to hide a value in Python? (display it as a *) [closed]

This should be enough code to get you started. You just need a flag to determine whether a number should be hidden or not: grid = [ [{“number”: 1, “hidden”: True}, {“number”: 5, “hidden”: False}, {“number”: 8, “hidden”: True}], [{“number”: 3, “hidden”: True}, {“number”: 2, “hidden”: True}, {“number”: 4, “hidden”: False}], [{“number”: 9, “hidden”: True}, … Read more

[Solved] Python–always get “must be str not int”

It is hard to tell what you want, but based on your edits it looks like you are trying to do something like this: replys = [] while True: reply = input(‘Enter text, [type “stop” to quit]: ‘) print(reply) if reply == ‘stop’ or reply == ‘ ‘: print(” “.join(replys)) break replys.append(reply) 1 solved Python–always … Read more

[Solved] Error in this python code [closed]

Fix your indentation and add a colon after each if-statement as the following and change user_input == input(‘:’) to user_input = input(‘:’): while True: print (“Options”) print (“Write ‘Quit’ if you want to exit”) print (“Write ‘+’if you want to make an addition”) print (“Write ‘-‘ if you want to make a sottration”) print (“Write … Read more

[Solved] class Boo():pass var1 = Boo var1.x = 4 How is this possible

Classes have a __dict__ by default, precisely for this purpose (run-time assignment and modification of attributes). Regular attribute assignment just puts the corresponding names and values values in there. It’s no different from setting it at class definition time, really: >>> class Boo: … x = 1 … >>> Boo.y = 2 >>> Boo.__dict__ mappingproxy({‘__dict__’: … Read more

[Solved] Accessing a function from a module

EDIT: You’re actually not having a python issue but a bash one. You’re running your python script as if it were bash (hence the ‘from: can’t read from’), did you put #!/usr/bin/env python at the beginning of the file you’re running (not print_text.py, the other one)? You could alternatively call it that way: python myfile.py … Read more

[Solved] Python: Is there any reason a subset of a list would sort differently than the original list?

I think I found why. That is because there are some nan in dpd.txt. And nan is unable to compare: float(‘nan’) > 1 # False while float(‘nan’) < 1 # False So this totally breaks comparison. If you change your key compare function to: def _key(id_): import math result = -dpd[index_map[id_]], id_.lower() if math.isnan(result[0]): result … Read more

[Solved] Reverse first n items of the list and return it

>>> def reverse(n, lst): if n <= 0: return [] return lst[:n][::-1] + lst[n:] >>> reverse(4, [‘f’, ‘o’, ‘o’, ‘t’, ‘b’, ‘a’, ‘l’, ‘l’]) [‘t’, ‘o’, ‘o’, ‘f’, ‘b’, ‘a’, ‘l’, ‘l’] >>> Explanation: if n <= 0:: if n is less than or equal to zero… return []: return an empty list. Otherwise… return … Read more