Home > other >  Python sort merge
Python sort merge

Time:09-17

List=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]]

How to sort merge, want to output is as follows:

List=[[' ABC ', 6], [' BCD, 5]]

Modify the original list or create a new list can be
How do I achieve?

CodePudding user response:

 list1=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dic={}
For the item in list1:
If the item [0] not in dic:
Dic [item [0]]=item [1]
The else:
Dic [item [0]] +=item [1]
List_new=[(x, y) for the x, y in dic. The items ()]

CodePudding user response:

reference 1st floor Bluebluesea response:
 list1=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dic={}
For the item in list1:
If the item [0] not in dic:
Dic [item [0]]=item [1]
The else:
Dic [item [0]] +=item [1]
List_new=[(x, y) for the x, y in dic. The items ()]

This dictionary can continue to use a derived type
 ls=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dc dict ()
For k, v in ls. __iter__ () :
If k=v dc [k] not in dc else dc [k] + v
Print ([[k, v] for k, v in dc. The items ()])

CodePudding user response:

 
List1=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]]

List2=list (set ([x [0] for x in list1]))
Print ([[0] * len (list2)])
Dict1=dict (zip (list2, [0] * len (list2)))
For x in list1:
Dict1 [0] [x]=dict1 [0] [x] + [1] x
Print (dict1)

Dict2={}
For x in list1:
If dict2. Get (x [0], "FFF")=='FFF' :
Dict2 [0]] to [x=x [1]
The else:
Dict2 [0] [x]=dict2 [0] [x] + [1] x
Print (dict2)


CodePudding user response:

 ls=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dc={}
For k, v in ls. __iter__ () :
If k=v dc [k] not in dc else dc [k] + v
Print (dc)

Ls=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]]
Dc dict ()
For k, v in ls. __iter__ () :
If k=v dc [k] not in dc else dc [k] + v
Print (dc)


Should also can

CodePudding user response:

refer to the second floor slap-happy explode response:
Quote: refer to 1st floor Bluebluesea response:

 list1=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dic={}
For the item in list1:
If the item [0] not in dic:
Dic [item [0]]=item [1]
The else:
Dic [item [0]] +=item [1]
List_new=[(x, y) for the x, y in dic. The items ()]

This dictionary can continue to use a derived type
 ls=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dc dict ()
For k, v in ls. __iter__ () :
If k=v dc [k] not in dc else dc [k] + v
Print ([[k, v] for k, v in dc. The items ()])

Can you further simplification of the some

CodePudding user response:

reference 5 floor Bluebluesea reply:
Quote: refer to the second floor slap-happy explode response:

Quote: refer to 1st floor Bluebluesea response:

 list1=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dic={}
For the item in list1:
If the item [0] not in dic:
Dic [item [0]]=item [1]
The else:
Dic [item [0]] +=item [1]
List_new=[(x, y) for the x, y in dic. The items ()]

This dictionary can continue to use a derived type
 ls=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dc dict ()
For k, v in ls. __iter__ () :
If k=v dc [k] not in dc else dc [k] + v
Print ([[k, v] for k, v in dc. The items ()])

Can be, the more you simplify some

Can also simplify again

CodePudding user response:

refer to 6th floor goofs off explode reply:
Quote: refer to the fifth floor Bluebluesea reply:

Quote: refer to the second floor slap-happy explode response:

Quote: refer to 1st floor Bluebluesea response:

 list1=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dic={}
For the item in list1:
If the item [0] not in dic:
Dic [item [0]]=item [1]
The else:
Dic [item [0]] +=item [1]
List_new=[(x, y) for the x, y in dic. The items ()]

This dictionary can continue to use a derived type
 ls=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dc dict ()
For k, v in ls. __iter__ () :
If k=v dc [k] not in dc else dc [k] + v
Print ([[k, v] for k, v in dc. The items ()])

Can be, the more you simplify some

Please can also simplify
I feel as if I had no more simplified

CodePudding user response:

Quote: refer to 7th floor Bluebluesea response:


Find a best simplified derivation of type
 ls=[[' ABC ', 6], [' BCD, 3], [' BCD, 2]] 
Dc dict ()
[dc. Update (} {k: v). If k is not in dc else dc update (} {k: dc [k] + v) for k, v in the ls]
Print ([[k, v] for k, v in dc. The items ()])
Print (dc)

CodePudding user response:

 import itertools as it 

nullnullnull
  • Related