Hi everyone!

I have difficulties solving a plot issue:
I have these two datas in an array: np.array([Q-value, length])
Q-value......length
A.................60
B.................40
C.................90
B................150
C................230
A................220

I want to plot these like:
Q-value..........total length
A...................280
B...................190
C...................230

Though it seems easy above my "real" array contains a lot more values.

Thankful for any help!

Recommended Answers

All 2 Replies

Here would be one way to accomplish this:

mylist = [
['A', 60],
['B', 40],
['C', 90],
['B', 150],
['C', 230],
['A', 220]
]

# convert to dictionary, put collision values into a list 
mydict = {}
for sub in mylist:
    print sub  # testing
    mydict.setdefault(sub[0], []).append(sub[1])

print
print mydict
print

# convert dictionary to list of lists
newlist = []
for k, v in mydict.items():
    print k, v, sum(v)  # testing
    newlist.append([k, sum(v)])

print
print newlist

"""my result -->
['A', 60]
['B', 40]
['C', 90]
['B', 150]
['C', 230]
['A', 220]

{'A': [60, 220], 'C': [90, 230], 'B': [40, 150]}

A [60, 220] 280
C [90, 230] 320
B [40, 150] 190

[['A', 280], ['C', 320], ['B', 190]]

"""

Thanks for the help!

Now I need to plot like a column chart. Have tried a bit but with no real luck. Some tips maybe?

Thanks again mate!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.