Accessing previous elements of python list comprehension - list

I have the following list:
a = [1,(landa/mu)]
I want to add elements to this list using previous elements:
a = a + [[f(i)*a[n-i] for i in range(1,n)] for n in range(2,K)]
in which f is a function and landa, mu and K are parameters. However, it is giving me the following error:
list index out of range
Could you please tell me how can I solve this problem? I can simply write it in a for loop but I want it to be in a list comprehension format to be done fast since K sometimes become a large number.

Related

SML counting within a pair of lists

I am trying to write a function in SML that takes in a pair of lists. The first list in the pair is a list of integers and the second list is a list of booleans. Ex: (([3, 5, 9], [true, false, false])). I am having trouble with the proper syntax to return how many times 'true' is found in the second list.
You would want to break this down.
Can you count the number of times a value is found in a list?
Can you pattern match out the second list in the tuple?
The first one can be accomplished by implementing a count function. A basic shell for that would look something like:
fun count (_, []) = ...
| count (v, (x::xs)) =
if ... then ...
else ...
For the second, well, you can see pattern-matching for binding names to the elements of a tuple in the above code.
Doing anything more would be doing your homework for you, and that would be a disservice.

run length decoding of a list in prolog?

I'm trying to decode a given list for example mydecode([(a,1), (b,2), (c,3), (d,2)],X) should give X = ['a','b','b','c','c','c','d','d']. What is the error in this code?
mydecode([],[]).
mydecode([X|Ys],[X|Zs]) :- \+ is_list(X), mydecode(Ys,Zs).
mydecode([[1,X]|Ys],[X|Zs]) :- mydecode(Ys,Zs).
mydecode([[N,X]|Ys],[X|Zs]) :- N > 1, N1 is N - 1, mydecode([[N1,X]|Ys],Zs).
you are asked to handle a list of 'tuples' of 2 elements, not a list of lists of 2 elements
then, the test in the second clause will always fail
the tuples elements are key and value, but you're 'accessing' them in inverse order.
So, remove the second clause - it's irrelevant, since pattern matching will discard ill formed lists.
Change the [1,X] to (X,1) and similarly other references to tuples, and test your code with the query assigned.

Add elements to another list that complies a certain condition in Prolog

Hello I want to make a program in Prolog where it's given a list as an input and the result is the list but only with the elements that obey a certain condition.
Example:
Output all elements < 6.
filter([1,6,4,9,13,5,2],X).
--> X = [1,4,5,2].
I've tried to do this:
filter([],[]).
filter([P|R], X) :-
P < 6,
filter(R, [P|X]).
Why doesn't this work?
I know I can use findall but I'd like to understand this 'manual' method.
Thank you

Accessing nth value in a key-value pair in dictionary in Python

I have a python dictionary of the type
Mutual={'A':[[1],[2],[],[]],'B':[[1],[],[],[]]}
I want to access the elements for key 'A'.
I tried this:
count=0
for z in range(2):
print Mutual["A"][z][count]
count+=1
I am getting the following error
IndexError: list index out of range
Can anyone suggest why is it so. Must be some silly mistake which I am unable to catch.
When you run the first iteration, Mutual["A"][z][count] returns 1. On the next iteration Mutual["A"][z][count] is supposed to be the second element in the list [2] (z is 1 and count is 1 as well) which does not exist. That is why you get an IndexError: list index out of range error.
A suggested modification to your code could be:
listA = Mutual["A"]
for z in range(len(listA)):
for w in range(len(listA[z])):
print listA[z][w]
This way, if the inner lists are of size zero the inner loop will not be executed and therefore, you will not try to access an out of bounds index.
It is usual in python to iterate the actual elements vs. using range() and indexes:
for z in Mutual['A']:
for count in z:
print count

How to read each element within a tuple from a list

I want to write a program which will read in a list of tuples, and in the tuple it will contain two elements. The first element can be an Object, and the second element will be the quantity of that Object. Just like: Mylist([{Object1,Numbers},{Object2, Numbers}]).
Then I want to read in the Numbers and print the related Object Numbers times and then store them in a list.
So if Mylist([{lol, 3},{lmao, 2}]), then I should get [lol, lol, lol, lmao, lmao] as the final result.
My thought is to first unzip those tuples (imagine if there are more than 2) into two tuples which the first one contains the Objects while the second one contains the quantity numbers.
After that read the numbers in second tuples and then print the related Object in first tuple with the exact times. But I don't know how to do this. THanks for any help!
A list comprehension can do that:
lists:flatten([lists:duplicate(N,A) || {A, N} <- L]).
If you really want printing too, use recursion:
p([]) -> [];
p([{A,N}|T]) ->
FmtString = string:join(lists:duplicate(N,"~p"), " ")++"\n",
D = lists:duplicate(N,A),
io:format(FmtString, D),
D++p(T).
This code creates a format string for io:format/2 using lists:duplicate/2 to replicate the "~p" format specifier N times, joins them with a space with string:join/2, and adds a newline. It then uses lists:duplicate/2 again to get a list of N copies of A, prints those N items using the format string, and then combines the list with the result of a recursive call to create the function result.