person = {'name':'xiaobai','age':18}
print ("The value of key 'name' is : ",person['name'])
print ("The value of key 'city' is : ",person['city'])
Out: The value of key 'name' is : xiaobai
Traceback (most recent call last):
File "C:\Users\E560\Desktop\test.py", line 17, in <module>
print ("The value of key 'city' is : ",person['city'])
KeyError: 'city'
用defaultdict再试试:
from collections import defaultdict
person = defaultdict(lambda : 'Key Not found') # 初始默认所有key对应的value均为‘Key Not Found’
person['name'] = 'xiaobai'
person['age'] = 18
print ("The value of key 'name' is : ",person['name'])
print ("The value of key 'adress' is : ",person['city'])
Out:The value of key 'name' is : xiaobai
The value of key 'adress' is : Key Not found
from collections import defaultdict
d = defaultdict(list)
d['person'].append("xiaobai")
d['city'].append("paris")
d['person'].append("student")
for i in d.items():
print(i)
Out: ('person', ['xiaobai', 'student'])
('city', ['paris'])
# create hash id for a namedtuple
cols =['a','b','c']
fields = " ".join(cols)+'hash_id'
Data = namedtuple('Data',fields,defaults=""*len(fields))
raw_result = []
with open('xx.csv',encoding='utf-8') as file:
file_iter = iter(file)
_ = next(file_iter) # Jump first line
for line in file_iter:
each_line = line.strip('\n').split(';')+['']
raw_result.append(Data(*each_line))
def create_hash_id(each):
text = "".join(x.replace(" ","") for x in each.asdict().values()
return each._replace(hash_id=hashlib.sha256(text.encode('utf-8')).hexdigest())
# A list of tuples
a = [Data(...),Data(...)]
new_a = list(map(create_hash_id,a))
# Get values for a namedtuple
a[0]._asdict().values()
# Get fields for a namedtuple
a[0]._fields
# Turn the result to dataframe
df = pd.Dataframe(a,columns=Data._fields)
With class
class DataPoint(namedtuple('DataPoint', ['date', 'value'])):
__slots__ = ()
def __le__(self, other):
return self.value <= other.value
def __lt__(self, other):
return self.value < other.value
def __gt__(self, other):
return self.value > other.value
City = namedtuple('City', 'name country population coordinates')
tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
tokyo
=>City(name='Tokyo', country='JP', population=36.933, coordinates=(35.689722, 139.691667))
tokyo._fields
=>('name', 'country', 'population', 'coordinates')
LatLong = namedtuple('LatLong', 'lat long')
delhi_data = ('Delhi NCR', 'IN', 21.935, LatLong(28.613889, 77.208889))
delhi = City._make(delhi_data)
delhi._asdict()
for key, value in delhi._asdict().items():
print(key + ':', value)
name: Delhi NCR
country: IN
population: 21.935
coordinates: LatLong(lat=28.613889, long=77.208889)
delhi.coordinates.lat # 28.613889
Person = namedtuple("Person", "name age height")
jane = Person("Jane", 25, 1.75)
print(jane._asdict())
jane._asdict()['name']
OrderedDict([('name', 'Jane'), ('age', 25), ('height', 1.75)])
'Jane'
Replacing Fields in Existing namedtuple Instances
from collections import namedtuple
Person = namedtuple("Person", "name age height")
jane = Person("Jane", 25, 1.75)
# After Jane's birthday
jane = jane._replace(age=26)
jane
=>Person(name='Jane', age=26, height=1.75)
Exploring Additional namedtuple Attributes
Person = namedtuple("Person", "name age height")
ExtendedPerson = namedtuple(
"ExtendedPerson",
[*Person._fields, "weight"]
)
jane = ExtendedPerson("Jane", 26, 1.75, 67)
jane
jane.weight
=>67
For loop namedtuple
Person = namedtuple("Person", "name age height weight")
jane = Person("Jane", 26, 1.75, 67)
for field, value in zip(jane._fields, jane):
print(field, "->", value)
name -> Jane
age -> 26
height -> 1.75
weight -> 67
Default Values
Person = namedtuple(
"Person",
"name age height weight country",
defaults=[185,"Canada",75]
)
print(Person._field_defaults)
{'height': 185, 'weight': 'Canada', 'country': 75}
Mike= Person("Mike",24)
Mike
Person(name='Mike', age=24, height=185, weight='Canada', country=75)
Data Classes can be thought of as “mutable namedtuples with defaults.” (Source) However, it’d be more accurate to say that data classes are like mutable named tuples with type hints. The “defaults” part isn’t a difference at all because named tuples can also have default values for their fields. So, at first glance, the main differences are mutability and type hints.
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
height: float
weight: float
country: str = "Canada"
jane = Person("Jane", 25, 1.75, 67)
print(jane.name)
jane.name = "Mike"
jane.name
'Jane'
'Mike'
Add fronzen=True, can't modify data any more
@dataclass(frozen=True)
class Person:
name: str
....
Subclassing namedtuple Classes
from collections import namedtuple
from datetime import date
BasePerson = namedtuple(
"BasePerson",
"name birthdate country",
defaults=["Canada"]
)
class Person(BasePerson):
"""A namedtuple subclass to hold a person's data."""
__slots__ = ()
def __repr__(self):
return f"Name: {self.name}, age: {self.age} years old."
@property
def age(self):
return (date.today() - self.birthdate).days // 365
print(Person.__doc__)
jane = Person("Jane", date(1996, 3, 5))
jane.age
A namedtuple subclass to hold a person's data.
25