Methods and Techniques

Source: Internet
Author: User

If you have a dictionary: dict_nu = {}, if you want to add a value to the dictionary, you can use the

dict_nu[' k1 '] = ' v1 '

If you have a list: List_nu =[], if you want to add elements to the list, you can use the

list_nu.append[' Tony '


The following topics are available:

List_nu = [11,22,33,44,55,66,77,88,99,90]

Requirements:

Corresponds to ' K1 ' of greater than 66: [number less than 66]

Corresponds to ' K2 ' of less than 66: [number greater than 66]

i.e. Dict_nu {' K1 ': [Number less than 66], ' K2 ': [Number greater than 66]}

{' K2 ': [K1], ' 33, ' [11, ' 22 ', ' 44, 55, 66]}


Method ①


List_nu = [11,22,33,44,55,66,77,88,99,90]
List_1 = []
List_2 = []
Dict_nu = {}
For num in List_nu:
If num > 66:
List_1.append (num)
dict_nu[' k1 '] = list_1
Else
List_2.append (num)
dict_nu[' K2 '] =list_2
Print Dict_nu


Method ②


DiC = {' K1 ': [], ' K2 ': []}
For Nu in List_nu:
If Nu >66:
dic[' K1 '].append (NU)
Else
dic[' K2 '].append (NU)
Print dic

Method ③

DIC = {}
For Nu in List_nu:
If Nu >66:
If ' K2 ' in Dic.keys ():

#如果k2在在字典dic中, then append a value

dic[' K2 '].append (NU)
Else

#如果k2不在字典dic中, then give K2 a value nu
dic[' k2 ' = [Nu,]
Else
If ' K1 ' in Dic.keys ():

#如果k1在在字典dic中, then append a value

dic[' K1 '].append (NU)
Else

#如果k1不在字典dic中, then give K2 a value nu

dic[' k1 ' = [Nu,]

Print dic


There are the following content file Log.txt:

Alex|123|1eric|123|1tony|123|1susan|123|1

Requires that the content be printed in the following format:

DIC = {
' Alex ': [123,1],
' Eirc ': [123,1],
' Tony ': [123,1],
' Susan ': [123,1]

}

The following statements are implemented:

obj= () Line_list = Obj.readlines () obj.close () dic = {}for line line_list:line = Line.strip () ele_list = Line.split () Dic[ele_list[]] = ele_list[:]print dic


This article from "Flat Light is true" blog, please be sure to keep this source http://cryan.blog.51cto.com/10837891/1716557

Methods and Techniques

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.