Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PyNamedTuple
#python 3.6.9 #PyNamedTuple: example of tuple, namedtuple #this code is created by Rezaul Hoque on May 05,2022; #contact:jewelmrh@yahoo.com;Dhaka,Bangladesh;https://rezaulhoque.wordpress.com,https://hoquestake.blogspot.com #note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; from collections import namedtuple print('|'*35) Intro=namedtuple('Intro',('title','dataStruct' ,'type','month','date','year','hour','space')) banner=Intro('Python','namedtuple','example','May','05','2022','18:20',' ') head=banner[0]+banner[7]+banner[1]+banner[7]+banner[2] time=banner[3]+banner[7]+banner[4]+banner[7]+banner[5] hr=banner[6] print(head) Fruit=namedtuple('Fruit',['name','season','color','price']) M=Fruit('Mango','summer','green','20') J=Fruit('Jackfruit','summer','gray','100') O=Fruit('Orange','winter','orange','20') #unpacking name,season,color,price=M print(f'{name} {season} {color} {price}') #fetching element using index print(M[0]) #fetching element using keyname print(J.color) #fetching element using getattr() print (getattr(O,'season')) #namedtuple is iterable for k in O: print(k) m=['Guava','rainy','green','10'] #converting list/iterable into namedtuple mNT=Fruit._make(m) #checking the conversion print(isinstance(mNT,Fruit)) print(isinstance(mNT,tuple)) print(mNT) dict={'name':'Water Melon','season':'summer','color':'dark green','price':'100'} #converting dictionary into namedtuple dictNT=Fruit(**dict) print(isinstance(dictNT,Fruit)) print(isinstance(dictNT,tuple)) print(dictNT) #getting all the field names using fields print(M._fields) #returning new namedtuple using _replace() without altering the original version print(J._replace(color='brown')) #the original instance print(J) print(time) print(hr) print ('|'*35)
run
|
edit
|
history
|
help
0
Python
list_copy_shallow_deep.py
Reverse linked-list in-place
pattern
Variables mutables e inmutables
Bubble sort
Test
Faiha HW #3
substring
31 dec