Day02 -- list, tuple, dictionary and set of Python data types

List in python

#List
#List class, list
#Enclosed in brackets, separated by commas, the elements in the list can be numbers, strings, lists, Booleans, etc.
#Lists can also be nested

=========Basic operation of list=========
(1) Common operations of list
list1 = [11,22,33,44,55]
# len Number of elements to view the list
print(len(list1))

# Value by index
print(list1[3])

# Slice the list
print(list1[0:2])
print(list1[:])

# for loop operation
for i in list1:
  print(i)

(2) List elements, which can be modified

# 1.Modifying elements by list index
list1 = [11,22,33,44,55]
list1[1] = 123
print(list1)

# 2.Modifying elements through list slices
list1 = [11,22,33,44,55]
list1[1:3] = [222,333]
print(list1)

The above operation results are:

 

(3) precautions for converting list to string:
1. If there are both numbers and strings in the list, you need to use for loop conversion.

# There are both numbers and strings in the list
list1 = [11,22,33,44,"hello","python"]
string = ""
for i in list1:
    string = string + str(i)
print(string)

The operation result is:

 

2. If there is only string in the list, the join method can be used to convert the list to string

list1 = ["hello","python"]
v1 = "".join(list1)
print(v1)

The operation result is:

 

 

Common methods for lists=========

list1 = [11,22,33,44,55]

# 1.append Append element to list,List can also be added
v1 = list1.append(235)
print(list1)

# 2.clear clear list 
list1.clear()
print(list1)

# 3.copy Copy, light copy
list1 = [11, 22, 33, 44]
v1 = list1.copy()
print(v1)

# 4.Count the number of occurrences of an element
v1 = list1.count(11)
print(v1)
# 5.insert Inserts an element at the specified index location,There are two parameters. Parameter 1 specifies the index to be inserted and parameter 2 specifies the element to be inserted list2 = [11, 22, 33, 44, "hello", "python"] list2.insert(1, 99) print(list2) # 6.Delete list # remove Delete the element specified in the list list2.remove(22) del list2[1] # Delete by index del list2[1:5] # Delete by slice # pop() Delete a value and get the deleted value,No index specified, delete the last element by default # v1 = list2.pop(1) # print(list2) # print(v1) # 7.extend Expand original list,Must be an iteratable object (executed internally for Cycle) list2 = [11, 22, 33, 44, "hello", "python"] list2.extend([111, 222]) list2.extend("hello") print(list2) # 8.reverse,Flip the current list li = [11, 22, 33, 44, 55] li.reverse() print(li) # 9.sort sort(Default ascending)sort(reverse=False) li = [11, 23, 10, 24, 26, 39, 73, 55] li.sort(reverse=True) # Descending order print(li)
Results of the seventh question:

Tuples in python

#A tuple that cannot be modified, added, or deleted
#When writing tuples, it is recommended to add commas at the end
#The first level element of a tuple cannot be modified / deleted or added
#Tuple usage is the same as list, slice, member operation in / not in

# Indexes
age = (11, 22, 33, 44, 55)
v = age[2]

# Section
v1 = age[0:3]
print(v, v1)

# for loop
for i in age:
    print(i)

# in or not in
print(22 in age)
print(123 not in age)        

Dictionary in python

#The value of a dictionary can be any value, such as dictionary, list, tuple, string, etc.
#Lists and dictionaries cannot be the key of dictionaries, tuples can be the key of dictionaries,
#Boolean value can be used as key, but when the key with dictionary is 1 / 0, which one is in the front and the key value pair behind will not be displayed.
#The dictionary is out of order
info = {'name': 'egon', 'age': '18', 'sex': 'male'}

# Dictionary value
print(info['name'])

# Delete dictionary
del info['name']
print(info)

# Lexicographic for Loop, default loop output all key
for i in info:
    print(i)

# adopt values()Method can get the dictionary's values
info = {'name': 'egon', 'age': '18', 'sex': 'male'}
for i in info.values():
    print(i)

# for Cycle to get key and value
for k, v in info.items():
    print(k, v)

The above operation results are:

 

 

Common methods of dictionary========

 

dict1 = {'hello': "python", "world": "java"}

# 1.fromkeys() From the sequence, create a dictionary and specify a uniform value

v1 = dict.fromkeys(["k1", "999", 123], 111)
print(v1)

# 2.get() according to key Get the value, key When it does not exist, you can specify a default value( None)

dict1 = {'hello': "python", "world": "java"}
v = dict1.get('hello', 'hadoop')
print(v)

# 3.pop() Delete and get value

dict1 = {'hello': "python", "world": "java"}
v = dict1.pop('hello')
print(v)

# Delete all dictionary elements v1
= dict1.popitem() print(v1) # 4.setdefault() Set value, there is no set, there is no set, get current key Corresponding value dict1 = {'hello': "python", "world": "java"} v1 = dict1.setdefault('hello', 123) print(dict1, v1)
v2
= dict1.setdefault('jsddd', 'jjshhd') print(dict1, v2)
# 5.update() To update dict1 = {'hello': "python", "world": "java"} dict1.update({'k1': 1111, 'k2': 999}) print(dict1) dict1.update(k1=234, k2=3344, k3=9988) print(dict1)

 

The results of the fourth question are as follows:

 

Collections in python

#In the set is a set of unordered hash values, which can be used as the key of the dictionary
#A collection is composed of different elements, which are unordered. The elements in the collection must be immutable
#Immutable type: number, string, tuple
#Sets are also iterative types, including strings, lists, tuples, sets, dictionaries

# set Definition
s = {11, 22, 33, 44, 55}

# add() Add element
s.add('hello')
print(s)

# clear() Clear collection
s.clear()

# Collection delete element
# 1.s.pop() Randomly delete elements
s.pop()
print(s)

# 2.s.remove()  # Specifies that collection elements are deleted
s.remove(11)
print(s)

# 3.s.discard()  # Delete the element. If the element does not exist, no error will be reported
s.discard('hello')
print(s)
print("========================")
# 4.update() Line multiple values s1 = {1, 2, 3, 4} s2 = {1, 3, 5, 7, 8} s1.update(s2) s1.update((5,6,7,8,9)) update The number following must be of an iterative type s1.add((2, 3, 4, 5, 6, 7, 8, 9)) # add The number following must be immutable s1.add("hello world") # add() Only one value can be updated s1.union(s2) print(s1)
# union() Intersection operation, the original set data is unchanged, update()Update operation, source collection changed.
# 5.for cycle for i in s1: print(i)

 



Tags: Python Java Hadoop

Posted on Thu, 23 Apr 2020 08:36:01 -0700 by djr587