0

I have a list for example

list =

and i give and i want to give the letters in the list numeric value for example

a=2,b=1,c=4,d=3

i want to display the list like this list =

but than i wanted to do a sum(list) and get a value in this case 10

is this possible to do and if so how?

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by snippsat
1

Some point to this.

>>> a = 2
>>> b = 1
>>> c = 4
>>> d = 3
>>> l = [a,b,c,d]
>>> l
[2, 1, 4, 3]
>>> sum(l)
10
>>>

So here i declare variables an put it in a list and sum() it up. ['a','b','c','d'] will always be list with strings.
So 'a' wil be a.
You can make a new list with numbers,and make it to dictionary so a point to 2.

>>> l = ['a','b','c','d']
>>> n = [2,1,4,3]
>>> d = dict(zip(l, n))
>>> d
{'a': 2, 'b': 1, 'c': 4, 'd': 3}
>>> d['a']
2
>>> [i for i in d.keys()]
['a', 'c', 'b', 'd']
>>> [i for i in d.values()]
[2, 4, 1, 3]
>>> sum([i for i in d.values()])
10
>>> for k,v in d.items():
...     print('%s has a value of %s' % (k, v))
...     
a has a value of 2
c has a value of 4
b has a value of 1
d has a value of 3

Edited by snippsat: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.