Last active
December 27, 2018 05:58
-
-
Save zealinux/f8f3e6787603222bee3929d7db6d6fba to your computer and use it in GitHub Desktop.
Python 里的各种转换
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def tuple_to_dict(t): | |
return dict([t]) | |
def get_first(d): | |
t = sorted(d.items())[0] | |
return tuple_to_dic(t) | |
def combine_to_dict(keys, values): | |
return dict(zip(keys, values)) | |
# np about | |
def nparray_to_list(npa): | |
return npa.tolist() | |
def list_to_nparray(list): | |
return numpy.array(list) | |
from collections import namedtuple | |
def dict_to_object(d, class_name): | |
# d = {"name": "joe", "age": 20} | |
# d_named = namedtuple("Employee", d.keys())(*d.values()) | |
# d_named #=> Employee(name='joe', age=20) | |
# d_named.name #=> 'joe' | |
return namedtuple(class_name, d.keys())(*d.values()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment