#python List
‘‘‘
There are many ways to create a list:
1. Create an empty list:[with one square bracket]
2. Use a pair of brackets, with ', ' to separate the elements inside: [A, B, C], [a]
3.Using a list comprehension:[x for x in iterable]
4.Using the Type constructor:list () or list (iterable)
"Def create_empty_list ():" ' Using a pair of square brackets to denote the empty list: []. return []def create_common_list (): ' Using square brackets, separating items with commas: [A], [A, B, c]. ' return [' A ', ' B ', ' C ', 1, 3, 5]def create_common_list2 (): ' Using a list comprehension: [x for X in iterable]. " return [x for x in range (1, ten)]def str_to_list (s): ' ' Using a string to convert list ' if s! = None:return List (s) else:ret Urn []def main (): Test_lista = Create_empty_list () print (Test_lista) print (' # ' *) Test_listb = Create_common_list () Print (TEST_LISTB) print (' # ' *) TEST_LISTC = Create_common_list2 () print (TEST_LISTC) print (' # ' *) Test_str = ' I want to t Alk about this problem! ' TEST_LISTD = Str_to_list (test_str) print (TEST_LISTD) if __name__ = = ' __main__ ': Main ()
2. Tuple to List
#!/usr/bin/env python#-*-coding:utf-8-*-atuple= (12,3, ' a ', ' yy ') alist=list (atuple) print "Atyple type is:", type ( atuple) print "Alist type is:", type (alist) print "Alist's value is:", alist
Run results
[[email protected] 20170120] # is: 'tuple'>is: ' List'>alist's value is: 'a' 'yy']
3, tuple and list similar, but the tuple can not be modified, tuple use parentheses, list with square brackets
Tuple creation, simply add the elements in parentheses and separate them with commas.
When a tuple is created that contains only one element, you need to add a comma after the element
Tup1= (50,)
1>>> Tup1 = (" All")2>>>PrintTup13 All4 The output string all, because parentheses () can represent both a tuple and a brace in a mathematical formula. 5 Therefore, if the tuple has only 1 elements, you must add a comma to prevent it from being treated as parentheses:6>>> Tup1 = (" All",)7>>>PrintTup18(' All',)9>>>
[[email protected] 20170120]#Cat tuple.py#!/usr/bin/env python#-*-coding:utf-8-*-Tup1=('Physics','Chemistry', 1997,2000); Tup2= (1,2,3,4,5,6,7,8,9);Print "Tup1[0]:", tup1[0];Print "Tup2[1:5]:", Tup2[1:5]; [[Email protected]20170120]#python tuple.pytup1[0]: physicstup2[1:5]: (2, 3, 4, 5)
modifying tuples
Elements in a tuple cannot be modified, but they can be combined in groups of tuples
#Code#!/usr/bin/env python#-*-coding:utf-8-*-Tup1= (12,34.56) tup2=('ABC','XYZ') Tup3=tup1+tup2PrintTup3#Run Results[[email protected] 20170120]#python tuple.py(12, 34.560000000000002,'ABC','XYZ')
Delete a tuple
#!/usr/bin/env python#-*-coding:utf-8-*-Tup1=('Physics','Chemistry', 1997,2000);PrintTup1delTup1Print "After deleting tup1:"PrintTup1#Execution Results[[email protected] 20170120]#python tuple.py('Physics','Chemistry', 1997, 2000) after deleting tup1:traceback (most recent call last): File"tuple.py", line 8,inch<module>PrintTup1nameerror:name'Tup1' is notDefined
Python tuple, list related