For example, x=[[' Beijing ', 2], [' Beijing ', 1], [' tianjin ', 1], [' fujian, 1]]
Because there are two values is Beijing, finally the array (also can be a new array) should be:
[[' Beijing ', 2, 1], [' tianjin ', 1], [' fujian, 1]]
CodePudding user response:
Simple idea is the whole to the dictionary again into list,
X=[[' Beijing ', 2], [' Beijing ', 1], [' tianjin ', 1], [' fujian, 1]]
X_dict={}
For xi in x:
If x_dict. Has_key (xi [0]) :
X_temp=x_dict [xi [0]]
X_temp. Append (xi [1])
X_dict [xi [0]]=x_temp
The else:
X_dict [xi [0]]=[xi [1]]
X_list=[]
For I in x_dict:
X_item=[I]
X_item. The extend (x_dict [I])
X_list. Append (x_item)
Print (x_list)
CodePudding user response:
X=[[' Beijing ', 2], [' Beijing ', 1], [' tianjin ', 1], [' fujian, 1]]
Dict_x={}
List_x=[]
For I in x:
If I [0] in dict_x. Keys () :
Dict_x [I [0]]. Append (I [1])
The else:
Dict_x [I [0]]=[I [0], [1]] I
Print (dict_x)
For j in dict_x keys () :
List_x. Append (dict_x [j])
Print (list_x)
CodePudding user response:
Lists=,1,2,3,5,6 [1]# to set (set to heavy) ~ _
Sets=set (lists)
# to return arrays ~ _
Listss=list (sets)
Print (listss)
CodePudding user response:
dic={}
L=[[' Beijing ', 2], [' Beijing ', 1], [' tianjin ', 1], [' fujian, 1], [' Beijing ', 1]]
Def func_map (item) :
Key=item [0]
Val=item [1]
If the key in dic:
Dic [key]=STR (dic [key]) + ", "+ STR (val)
The else:
Dic [key]=val
Print ([list (I) for I in zip (dic) keys (), dic. The values ())))
CodePudding user response:
L=[[' Beijing ', 2], [' Beijing ', 1], [' tianjin ', 1], [' fujian, 1], [' Beijing ', 1]]
Dic=dict (l)
Tmp_dict={}
For the key in dic:
Tmp_dict [key]=[]
For a, b in l:
Tmp_dict [a]. Append (b)
LST=[]
For k, v in tmp_dict. The items () :
Tmp_list=[k]
Tmp_list. The extend (v)
LST. Append (tmp_list)
Print (LST)
CodePudding user response:
3/f positive solutions, directly set, turn back,CodePudding user response:
Def amp_merge_value () :# keys corresponding to the amplitude of the same frequency to a superposition of
X_dict={}
For xi in fraq_amplitude:
If x_dict. __contains__ (xi [0]) :
X_dict [xi [0]] [0] +=xi [1]
The else:
X_dict [xi [0]]=[xi [1]]
X_list=[]
For I in x_dict:
X_item=[I]
X_item. The extend (x_dict [I])
X_list. Append (x_item)
Return x_list
CodePudding user response:
Method one:According to the first place sort, traverse again, can,
Please look at the code
CodePudding user response:
Method 2:Use a dictionary