for i in list404:
for j in range(len(table)):
if table[j][0] == i:
table.remove(table[j])
for k in range(len(stack)):
if stack[k][0] == i:
stack.remove(stack[k])
for l in range(len(symbols)):
if symbols[l] == i:
symbols.remove(symbols[l])
list404 is a list of stocks. I'd like to remove the stocks inside the table, stack and symbols with the name shows up in the list404. The table and stack are the nested list I guess. The structure is like this: [[a, b, c], [a, b, c], [a, b, c]]. All I want is to check whether a is in the list404. If it exist, remove the whole list from table.
Here's the error. IndexError: list index out of range in if table[j][0] == i:
Can anyone help me with it??? Thx!
as Ken White said , you should not remove elements from table in the loop of its length,
i think instead of that you can just make a new list and if the item is valid in your conditions , put it in the new list :
note: as i dont know what the structure of your data exactly looks like , I just write a sample code but you can change it for your data.
new_table = []
new_stack = []
new_symbols = []
for i in list404:
for j in range(len(table)):
if table[j][0] != i:
new_table.append(table[j])
for k in range(len(stack)):
if stack[k][0] != i:
new_stack.append(stack[k])
for l in range(len(symbols)):
if symbols[l] != i:
new_symbols.append(symbols[l])
Related
I want to get this solution for my question. For example, ?- rlen([a,[a,b],c],X). returns X = 4. I tried the following code but it returns answer 3. Maybe, it's taking [a,b] as one element. I am new to prolog and learning, any help would be really appreciated.
rlen([],0).
rlen([_|T],N) :- rlen(T,N1),N is N1+1.
The code you wrote returns the number of elements in a list and [a,[a,b],c] has exactly 3 elements: a, [a,b] and c. If you want to do a nested count, I would suggest using the build-in predicate flatten/2 to flatten the list.
example (tested with SWISH):
?- rlen([a,[a,b],c],X).
X = 3.
?- flatten([a,[a,b],c],L).
L = [a, a, b, c].
?- flatten([a,[a,b],c],L), rlen(L,N).
L = [a, a, b, c],
N = 4.
However not using inbuild predicates is a bit more challenging, because you have to go through your list like in any normal length predicate, and for every Head element you have to distinguish between the case that the head element is a list or is not a list. If A is a list, count the elements of A, otherwise just add 1. (a -> b ; c ) is an if-then else: if a, then b, else c. Tested with SWISH:
rlen([],0).
rlen([A|T],N) :-
( is_list(A)
-> rlen(A,NA)
; NA is 1
),
rlen(T,NT),
N is NA+NT.
?- rlen([a,[a,b],c],X).
X = 4.
I am scripting in Python2.7.I have an array like k = ((12,12.356,40.365),(458,12.2258,42.69)) and I want to create a list out of the first element of each subset like:
for i in K:
l = k[i][0]
While I have encountered the error "tuple indices must be integers, not tuple".
Would you please let me know how can I come up with a solution?
You are trying to access k tuple item by tuple like k[(12,12.356,40.365)][0], while an item of ordered sequence should be accessed by its position/index (for ex. k[0][0]).
Use list comprehension to get a list out of the first element of each subset:
k = ((12,12.356,40.365),(458,12.2258,42.69))
result = [t[0] for t in k]
print(result)
The outtut:
[12, 458]
a = ['french', 'english']
b = ['(portuguese; french)', '(english)']
I want to compare two lists and in return I want the index of the matches
I tried:
matches = list([i for i, item in enumerate(a) if item in b]+[i for i, item in enumerate(b) if item in a])
But the result was an empty list
do you need this..
b = ['(portuguese, french)', '(english)']
a = ['french', 'english']
matching = [i for i, x in enumerate(b) if any(thing in x for thing in a)]
print matching
output: [0, 1]
If I have a nested list like:
l = [['AB','BCD','TGH'], ['UTY','AB','WEQ'],['XZY','LIY']]
In this example, 'AB' is common to the first two nested lists. How can I remove 'AB' in both lists while keeping the other elements as is? In general how can I remove a element from every nested list that occurs in two or more nested lists so that each nested list is unique?
l = [['BCD','TGH'],['UTY','WEQ'],['XZY','LIY']]
Is it possible to do this with a for loop?
Thanks
from collections import Counter
from itertools import chain
counts = Counter(chain(*ls)) # find counts
result = [[e for e in l if counts[e] == 1] for l in ls] # take uniqs
One option is to do something like this:
from collections import Counter
counts = Counter([b for a in l for b in a])
for a in l:
for b in a:
if counts[b] > 1:
a.remove(b)
Edit: If you want to avoid the (awfully useful standard library) collections module (cf. the comment), you could replace counts above by the following custom counter:
counts = {}
for a in l:
for b in a:
if b in counts:
counts[b] += 1
else:
counts[b] = 1
A somewhat short solution without imports would be to create a reduced version of the original list first, then iterate through the original list and remove elements with counts greater than 1:
lst = lst = [['AB','BCD','TGH'], ['UTY','AB','WEQ'],['XZY','LIY']]
reduced_lst = [y for x in lst for y in x]
output_lst = []
for chunk in lst:
chunk_copy = chunk[:]
for elm in chunk:
if reduced_lst.count(elm)>1:
chunk_copy.remove(elm)
output_lst.append(chunk_copy)
print(output_lst)
Should print:
[['BCD', 'TGH'], ['UTY', 'WEQ'], ['XZY', 'LIY']]
I hope this proves useful.
I am trying to compare two different lists, but the comparation must be succesful when the first and the last items of the lists are equal and vice versa, while the other items of the lists must be equal.
So the thing must be that
cmp([a,b,c,d,e],[e,b,c,d,a]).
true.
With the following code we succeed but only with the first two items of the lists:
swap([X,Y],[Y,X]).
swap([X,Y|T],[Y,X|Z]):- T=Z.
Some idea?
I like the append/3 approach that #gusbro presented (+1). Another way to do this is with an auxiliary predicate that carries the elements you want along. This is a minor variation on the problem of swapping the first and last elements of a list.
cmp([A|As], [B|Bs]) :-
cmp(As, A, B, Bs).
cmp([A], B, A, [B]).
cmp([_, A|As], X, Y, [_, B|Bs]) :-
cmp([A|As], X, Y, [B|Bs]).
| ?- cmp([a,b,c,d,e],[e,b,c,d,a]).
true ? a
no
| ?- cmp(A, B).
A = [C,D]
B = [D,C] ? ;
A = [C,_,D]
B = [D,_,C] ? ;
A = [C,_,_,D]
B = [D,_,_,C] ? ;
A = [C,_,_,_,D]
B = [D,_,_,_,C] ? ;
...
I would use append/3 to get the first and last item, and use it again with those items interchanged:
cmp(A, B):-
append([First|Tail], [Last], A),
append([Last|Tail], [First], B).