I have to find some text in String using list comprehension but not getting my expected result
A = "BANANA"
[[b,a[b:]] for b in range(len(a)) if a[b:][0].lower() in ['a', 'e', 'i', 'o', 'u'] and 'ANA'.find(a[b:])>=0]
I am getting the following result -
[[3, 'ANA'], [5, 'A']]
But my expectation is as follows:
[[1, 'ANANA'], [3, 'ANA']]
Please help me where is am doing wrong.
Your list comprehension is very close, however the issue is with the if condition which checks for a character starting with A,E,I,O,U and has to check if ANA is also a part of the entire string.
In your condition if a[b:][0] in [....] evaluated to True hence there is an output of [5,'A']. Since you have a condition which translates as
Starts with a vowel in uppercase AND
Contains ANA in the string
Your comprehension should instead be as follows:
[[b,a[b:]] for b in range(len(a)) if a[b:][0] in ['A','E','I','O','U'] and 'ANA' in a[b:]]
The result for a='BANANA' with the above comprehension is
[[1, 'ANANA'], [3, 'ANA']]
Hope it helps.
Related
I know that the following breaks up a list and put it into two lists depending on whether an index is even or odd. But not sure how [n::2] for n in [1,0] works.
[[1,2,3,4,5][n::2] for n in [1, 0] ]
[[2, 4], [1, 3, 5]]
Can you point me to post that explain and walk me through the logic behind it? Thank you.
The left part happens with each value of n. n indicates the starting position to take the 2nd element including the n'th one.
print([[1,2,3,4,5][n::2] for n in [1, 0] ] )
print([[1,2,3,4,5][1::2]]) # outputs [[2, 4]]
print([[1,2,3,4,5][0::2]]) # outputs [[1, 3, 5]]
print([[1,2,3,4,5][4::2]]) # outputs [[5]]
Why does prolog answer false to: member([5], [2, 5]).?
Is there a way to work around it?
Any help will be greatly appreciated.
Why does Prolog answer false to: member([5], [2, 5]).?
Let's ask Prolog why! Download library(diadem) into your working directory and:
?- use_module(diadem).
true.
?- member([5], [2,5]).? Expl.
Expl = member([_|_], [2, 5])
; ... .
Not only does it fail but also a generalization fails:instead of [5] that is a list with a single element 5, we have now simply at least one element - no matter which. So we can take the value for Expl as a query which still fails. Something in the remaining goal must thus be the culprit.
?- member([_|_], [2, 5]).
false.
Note also what was not generalized away: The two elements are still here! If they would be variables, the query would succeed! Generalize the query a bit:
?- member([5], [2, Any]).
Any = [5].
Now it succeeds!
As described in the SWI Prolog documentation for member/2:
member(?Elem, ?List )
True if Elem is a member of List.
The predicate is member. It is not subset or sublist or subsequence. It succeeds if the first argument is a member (that is, an element) of the list given in the second argument. The element [5] is not a member of the list [2, 5] since the element [5] isn't 2 and it isn't 5. However, [5] would be a member of the list [2, [5]], so member([5], [2, [5]]) would succeed.
It could be that you are looking for another predicate?
From the SWI Prolog documentation for subset/2:
subset( +SubSet, +Set )
True if all elements of SubSet belong to Set as well.
It works as expected:
Welcome to SWI-Prolog (threaded, 64 bits, version 7.5.5)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
?- subset([5], [2, 5]).
true.
?- subset([5, 3], [2, 3, 5, 7]).
true.
?- subset([5, 3], [2, 5]).
false.
weekinfo=[]
for k in sats:
weekinfo.append(get_weekly(k, satdict))
yearend=get_weekly('end_year', satdict)
weektuples=zip(sats, weekinfo)
Sats is a list with 52 items. [ date, date, date, etc]
Weekinfo is a nested list (thus 52 lists in a list) with in each nested list with in each list 100 tuples.
I am trying to make a tuples (weektuples) in the last line via zip(sats, weekinfo). However, the output are not tuples but a lists.
Is there a solution to create tuples with Sats and Weekinfo?
Try:
x=52
sats = np.linspace(1,x,x)
print sats
weekinfo=[]
satdict= ['m', 't', 'w', 'u', 'f', 'sa', 'su']
for k in range(x):
weekinfo.append((sats[int(k)], satdict[int(k)%7]))
print weekinfo
Not sure that's the exact answer you're looking for, but something along those lines might work
I am trying to map !! to a list i.e. have a list of positions and a list where I am trying to return the elements in these positions, so far I have:
map $ flip listOfThings!!listOfPositions
I think this is wrong, but cannot work out how to fix it! Any help would be appreciated.
Is this what you mean?
> map (['a', 'b', 'c']!!) [2, 1]
['c', 'b']
I have the following question for homework
Define a function append lists that
takes a list of lists and returns a
new list containing the sublist
values. For example, append lists([[1,
2], [3, 4], [5]]) should return the
list [1, 2, 3, 4, 5] and append
lists([[1, 2], [3], [[4, 5]]]) should
return the list [1, 2, 3, [4, 5]].
I've tried various ways of creating this function in order to append the list so it gives the desired output to no avail so I came here looking for some help. I've found a few other ways of going about this online, but they use extensive methods that we haven't even dabbled in as of yet in my CPSC 121 class. We're limited to the basics in what we've learned.
Any help would be much appreciated!
By now, it is likely that the assignment is gone, but here is a solution:
def append_lists(lists):
output = []
for l in lists:
for e in l:
output.append(e)
return output
This appends each element of each list to the output of the function, which eliminates exactly one level of nesting in the elements.