[Solved] Python – Swap a value for a dictionary item

First make from your string a list. list_name = string_name.split(‘ ‘) Then switch the keys and the values from your dict (if using python 3.x use iter instead of iteritems) my_dict = {y:x for x,y in my_dict.iteritems()} Then you can iter tro your code for numbers in list_name: print(my_dict[int(numbers)]) 6 solved Python – Swap a … Read more

[Solved] Write a Python function to concatenate all elements (except the last element) of a given list into a string and return the string

def conexclast(strlst): ”’ function to concatenate all elements (except the last element) of a given list into a string and return the string ”’ output = “” for elem in strlst: strng = str(elem) output = output+strng return ‘ ‘.join(strlst[0:-1]) print(“Enter data: “) strlst = raw_input().split() print(conexclast(strlst)) The main correction is splitting the user’s input … Read more

[Solved] Python making a counter

I would suggest a simplified “architecture”. Your functions to get the user and computer choices should return values that can be compared. import random CHOICES = (‘rock’, ‘paper’, ‘scissors’) def get_user_choice(): choice = input(‘Rock, paper, or scissors? ‘) choice = choice.lower().strip() if choice not in CHOICES: print(‘Please select one of rock, paper, or scissors’) choice … Read more

[Solved] Compute the ratio of the sum of cube of the first ‘n’ natural numbers to the sum of square of first ‘n’ natural numbers

from functools import reduce import ast,sys input_int = int(sys.stdin.read()) num_list = [x for x in range(1, input_int+1)] print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y ** ,num_list)) 1 solved Compute the ratio of the sum of cube of the first ‘n’ natural numbers to the … Read more

[Solved] if imagesNamesList==[“None” for x in range(len(listOfImages)]: [closed]

You are missing a closing parenthesis: if imagesNamesList==[“None” for x in range(len(listOfImages))]: # here–^ However, you could write this code better (cleaner and more efficiently) like so: if imagesNamesList == [“None”]*len(listOfImages): Or, if your lists are huge, you can do as @mgilson noted: if all(x == “None” for x in imagesNamesList) and len(imagesNamesList) == len(listOfImages): … Read more

[Solved] Importing a module in the module [closed]

The first time a module is imported, an entry for it is made in sys.modules. sys.modules is a dict, mapping the module name to the module code. All subsequent imports of the same module find the module’s name in sys.modules, and simply retrieve the module’s code from the sys.modules dict. So the code in the … Read more

[Solved] Reading a text file in python [closed]

Here is an example of reading and writing a text file below. I believe that you are looking for the “open” method. with open(‘New_Sample.txt’, ‘w’) as f_object: f_object.write(‘Python sample!\n’) f_object.write(‘Another line!\n’) You simple open the file that you want and enter that file name as the first argument, and then you use ‘w’ as the … Read more

[Solved] how to find string in text file by compare it with user input using python?

It appears to me that within your list comprehension you just have one minor issue! Instead of: item for item in textString within your list comprehension, I would suggest: item for item in listText as currently you are iterating through each char of the whole text, rather than each element in the list of the … Read more

[Solved] find hex value in list using regular expression

Try this: import re lst = [‘a’, ‘4’, ‘add’, ‘e’, ‘a’, ‘c0a8d202’, ‘128’, ‘4’, ‘0’, ’32’] pattern = re.compile(r’c[0-9a-fA-F]?’) for i in lst: if re.search(pattern, i): print(lst.index(i)) Note: this is as per your desired output but i am agree with @Jean-François Fabre who said that what’s wrong with lst.index(‘c0a8d202’) ? what’s the point of regular … Read more

[Solved] Python text formatting

Use string formatting: In [48]: def solve(a,b): a,b=str(a),str(b) spaces=len(a.split())-1 return “{0} {1} {2}”.format(a,”.”*(68-len(a)-len(b)-spaces),b) ….: In [49]: print solve(a,b);print solve(p,q) Hello …………………………………………………. False Python rocks …………………………………………… True 5 solved Python text formatting

[Solved] Storing values in a CSV file into a list in python

Please never use reserved words like list, type, id… as variables because masking built-in functions. If later in code use list e.g. list = data[‘FirstNames’].tolist() #another solution for converting to list list1 = list(data[‘SecondNames’]) get very weird errors and debug is very complicated. So need: L = data[‘FirstNames’].tolist() Or: L = list(data[‘FirstNames’]) Also can check … Read more