5

Python dictionary with objects in the dictionary with strings

 3 years ago
source link: https://www.codesd.com/item/python-dictionary-with-objects-in-the-dictionary-with-strings.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python dictionary with objects in the dictionary with strings

advertisements

I've got dictionary with objects values and string keys:

dict{
'key1': object_1
'key2': object_2
}

And I'd like to convert it into:

dict{
'key1': str(object_1)
'key2': str(object_2)
}

Where str(object_1) is a string representation of object_1. What is the simplest and the most pythonic way of performing this transformation?


dict((k, str(v)) for k, v in d.iteritems())

or in Python2.7+:

{k: str(v) for k, v in d.items()}

For more complicated dicts (with tuples of objects as values):

dict((k, tuple(str(x) for x in v)) for k, v in d.iteritems())

{k: tuple(str(x) for x in v) for k, v in d.items()}

Related Articles

Given two lists in python, one with strings and one with objects, how to map them?

I have a list of strings string_list = ["key_val_1", "key_val_2", "key_val_3", "key_val_4", ...] and a list with objects object_list = [object_1, object_2, object_3,...] Every object object_i has an attribute key. I

Initialize the python dictionary containing strings and lists

What's the best way to initialize (e.g. before a loop) a dictionary that will contain strings and a list in each item? For instance: dict = [("string1", [1,2]), ("string2", [5,6]),..] So: dict["string1"] returns: [1,2] Please

Call to Python rust with string in function parameters

I can call my test Rust program with integers as input and handle these fine, even without reference to ctypes. However I cannot seem to get a string without segfaulting in Rust. Here is my test Rust code: use std::env; #[no_mangle] pub extern fn hel

Python add an object to the list in the same object method

I need to add an object, e.g. Person, to a list that is given as a parameter. This is an example of the code class Person: def __init__(self, age): self.age = age def add_to_list(self, list_of_persons): list_of_persons.append(self) I know this situat

Python mapping with strings

I implemented a version of the str_replace function available in php using python. Here is my original code that didn't work def replacer(items,str,repl): return "".join(map(lambda x:repl if x in items else x,str)) test = "hello world"

Python: Adds JSON Objects to the Nested List

I'm trying to iterate through a list of IP addresses, and extracting the JSON data from my url, and trying to put that JSON data into a nested list. It seems as if my code is overwriting my list over and over, and will only show one JSON object, inst

link the json object to the api web string

I'm posting the following json to the web.api: { "tipoReporte":"039", "fecha":"20/05/2016", "datos":{ "Prop1":"prop1", "Prop2":"prop2", "Prop3":"prop3

Create an object of the class by string value

Is it possible to create an object from a class with a dynamic name? I have the name of the class in a string, but I want to create an instance of that class.Yes you can: var classRef:Class = getDefinitionByName("YourClassIncludingPackage") as C

How to pass the Json object in the GET request string url

I have a json object in javascript, filters = Object {assetType: "CAR~LIGHT TRUCK", bodyType: "SEDAN~SPORT UTIL"} I need to pass these values along with 5 more string value in query string. url = '/starHome/exportToCsv/'+tier+'/?period

Sort the value of the object in the Hashmap & lt; String, Object & gt;

I want to sort an Hashmap by the object's value. In this case, by country code. KEY OBJECT String LoyaltyCountry - country name - country code - country loc My code is as follows: public static HashMap<String, LoyaltyCountry> loyaltyCountrySortMap(H

Convert the String String object to the base64 encoded string

I have a question, which Unicode encoding to use while encoding .NET string into base64? I know strings are UTF-16 encoded on Windows, so is my way of encoding is the right one? public static String ToBase64String(this String source) { return Convert

Resolving a Python Object in the Global Namespace by String Search

Let's say you want the object at the end of strings of object names: an example string would be 'first_class.second_class.third_class.id', and the list of strings all take the form 'X1object.X2object...XNobject.what_you_want_is_here_object'. In every

NodeJS: route.get with the traditional query string format

I'm using express to redirect my web request. One of my web request takes in three optional parameters, so I have router.get('/function/:param1?/:param2?/:param3?', function(req, res) {...}); My issue is that as all these three params are optional an

How to pass the object in the online onclic event in the dynamic creation of elements

I am creating a table row dynamically. Each row is having onclick event. When I click on that table row I want to pass row object to a function but my problem is, while passing that object, I am getting [object object] string. So I am not able to use

How to extract very specific sets of chain parts from the QR decoded string?

I needed to pick specific parts of a very big string (that's being decoded from a QR code) to set as text in TextView elements in Android. //*these are the parts of the string whose corresponding values I need as a text for my TextViews.* String uid,

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK