Job 2:
There is a list of List1 = [All-in-one, ' a ', ' B ', 4, ' C ']
There is an unknown dictionary dict1, unclear internal key value pair condition
If the Dict1 dictionary does not have a ' K1 ' key value pair, then create and set the value of the key to an empty list and add the elements of the odd index bit in the List1 into the list
If the Dict1 dictionary has a ' K1 ' key-value pair and the corresponding value is a list type, add the elements of the odd-indexed bit in the List1 to the list
Ideas
Since all of them need to be used, the LIST1 elements of the odd bits are extracted first.
Nothing is created, there is a list to determine if, then add, if not list, do not manipulate
Final Print Dictionary
List1 = [A-D, ' A ', ' B ', 4, ' C ']
Dict1 = {' K2 ': 2, ' K3 ': 3, ' KK ': ' A ', ' K1 ': [1]}
Temp1 = []
#由于两个判断条件都要用到奇数索引位元素, so I'm going to extract it first.
For elment in List1:
A = List1.index (elment)
if a%2 = = 1:
Temp1.append (elment)
Print (TEMP1)
#先打印奇数索引号的列表
If Dict1.get (' K1 ') is None:
Dict1.setdefault (' K1 ', Temp1)
#没有就创建, the SetDefault function is to check the key, if there is a dictionary, return the value, if not, increase the value of this key-default to the dictionary
Elif isinstance (dict1.get (' K1 '), list):
Dict1.get (' K1 '). Extend (TEMP1)
# There's the type of judgment, then add the entry list
Else
Pass
#如果不是列表类型, nothing's changed.
Print (DICT1)
Python Job 2