Python Job 2

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.