Answers for "python named tuple"

2

create a named tuple python

>>> from collections import namedtuple
>>> Person = namedtuple('Person', 'first_name last_name zip_code')
>>> p1 = Person('Joe', 'Schmoe', '93002')
>>> p1.first_name
'Joe'
>>> p1[0]
'Joe'
>>> p1.zip_code
'92002'
>>> len(p1)
3
>>> type(p1)
<class '__main__.Person'>
>>>
Posted by: Guest on April-21-2021
3

Python Ordered Dictionary

from collections import OrderedDict

# Remembers the order the keys are added!
x = OrderedDict(a=1, b=2, c=3)
Posted by: Guest on September-19-2020
0

py tuple

# Different types of tuples

# Empty tuple
my_tuple = ()
print(my_tuple) # ()

# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple) # (1, 2, 3)

# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple) # (1, 'Hello', 3.4)

# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple) # ('mouse', [8, 4, 6], (1, 2, 3))
Posted by: Guest on January-04-2021
-3

python collections

list = [1,2,3,4,1,2,6,7,3,8,1]
Counter(list)
Posted by: Guest on September-22-2020

Python Answers by Framework

Browse Popular Code Answers by Language