Reputation: 6366
I have:
ListOne = ['foo', 'bar', ..]
I now want to create a new List by zipping ListOne with ListTwo. ListTwo looks like this:
ListTwo = [{count, 1},{count, 1},{count,1}, ..]
What's a nice way to dynamically generate ListTwo? Every list item will be the same.
I want to feed the result of the zip to dict:from_list
. So maybe a zip is not the best approach.
Upvotes: 0
Views: 1345
Reputation: 3235
I'm not if this is what you are exactly looking for, but if ListTwo is something like this : [{count,1}, {count,1},.....] you may generate it with:
ListTwo = [{count, 1} || _X <- lists:seq(1,5)].
I think that each argument of ListTwo is a tuple, thus you should separate the two value with ","...otherwise if your example is correct you may do something as:
ListTwo = [{'count:1'} || _X <- lists:seq(1,5)].
obviously you should pick the right range for the size of your list (in my example from 1 to 5)
Upvotes: 3
Reputation: 5113
While your question could be more specific, my guess is that you want to use a dictionary to store key, value pairs where the value is a counter. What you are trying to achieve by asking this, is how to initialize the dict with the counter set to 1.
The folloing code will create a new dict, where the key is the key from ListOne and the value is 1:
ListOne = [foo, bar, baz, quux].
D1 = dict:from_list([{Elem, 1} || Elem <- ListOne]).
Now, to increment these counters, you can use dict:update_counter/3
:
D2 = dict:update_counter(foo, 1, D1).
To decrement, you would simply give a negative number as the increment.
It is also worth noting that dict:update_counter/3
, will create the key in the dict if it is not already present, with the given increment as the initial value.
Upvotes: 4