Home > other >  Help, help the rewritten into a function
Help, help the rewritten into a function

Time:11-26

 data_str='{" category ": [gate" layer 1 ", "1 layer 2 gate", "1 layer 3 gate", "number 1 layer 5 door", "1 layer 6 door", "1 layer 7 gate", "1 layer gate 8", "gate 1 layer 9]", "series_in" : 5285,31,51,136 [1872946911170], "series_out" : [557262175, 9759935611273, 25]}' 
The import json
Data=https://bbs.csdn.net/topics/json.loads (data_str)
Ls=[' gate 1 layer 1, gate '2', 'no. 3 door]
For CG in ls:
If CG in data [' category '] :
The index data of=[' category ']. Index (CG)
Print (" {} {} {}, out of ". The format (CG, data [' series_in] [index], data [' series_out] [index]))



Can help to rewrite the function, convenience and other data splicing together output
Call this function directly, according to
1 layer 1 door into the 1872, 557
1 layer 2 gate into 946, out of 557
1 layer 3 door into the 1485, 1725

CodePudding user response:

Didn't you write it?

CodePudding user response:

This is not square and other string concatenation

CodePudding user response:

Code rewritten into the function of main point: pay attention to the parameters, which parts can be change, put it in the parameters,

CodePudding user response:

 def f (data_str, ls) : 
The import json
Data=https://bbs.csdn.net/topics/json.loads (data_str)
Result=[]
For CG in ls:
If CG in data [' category '] :
The index data of=[' category ']. Index (CG)
Result. Append (" {} {} {}, out of ". The format (CG, data [' series_in] [index], data [' series_out] [index]))
Return the result

Data_str='{" category ": [gate" layer 1 ", "1 layer 2 gate", "1 layer 3 gate", "number 1 layer 5 door", "1 layer 6 door", "1 layer 7 gate", "1 layer gate 8", "gate 1 layer 9]", "series_in" : 5285,31,51,136 [1872946911170], "series_out" : [557262175, 9759935611273, 25]}'
Ls=[' gate 1 layer 1 ', 'gate 1 layer 2', 'gate 1 layer 3]
Print (f (data_str, ls))