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']
Related
Is there a preferred and type-safe way without forced typecast to remove nils from Array?
[1, nil].select{|x| !!x}
// => Array(Int32 | Nil)
Something like special select?
P.S.
The use case when I hit this issue - I want to calculate median and the sort won't work:
[1, nil].select{|x| !!x}.sort
Map with zeros [1, nil].map{|x| x || 0} won't work as unlike let's say sum for some operations the length does matter (median for example).
Array#compact will remove nils from the Array:
[1, nil].compact # => [1] (Array(Int32))
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]]
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.
In short i want to number chars in order from 1 to n without changing the position of the chars in a list.
Suppose I have a list called key = ['c', 'a', 't'] How would i go about
assigning a number to each letter depending on where it is situated in the alphabet with respect to the other letters. Starting at 1 and going until len(key) such that our key becomes [ 2, 1, 3]
I'm really stumped. I have a way to convert them to numbers but very unsure as to how to compare them such that the above happens any help, tips, ideas or explanations would be appreciated.
this is what i have so far...
import string
key = list(input("enter key: ").upper())
num = []
for i in key:
num.append(string.ascii_uppercase.index(i)+1)
This solution assumes that duplicate entries should be assigned the same number, so that
# ['c','a','t'] -> [2, 1, 3]
# ['c','a','t','c','a','t'] -> [2, 1, 3, 2, 1, 3]
You can write a simple function like this:
def get_alphabet_pos(lst):
uniques = sorted(set(lst)) # set() to filter uniques, then order by value
numbers = {letter: i+1 for i, letter in enumerate(uniques)} # build a lookup dict
return [numbers[key] for key in lst]
get_alphabet_pos('cat') # [2, 1, 3]
So here's what happens in the function:
In line 1 of the function we convert your list to a set to remove any duplicate values. From the docs # https://docs.python.org/3/tutorial/datastructures.html#sets:
A set is an unordered collection with no duplicate elements.
Still in line 1 we sort the set and convert it back into a list. Thanks to #StefanPochmann for pointing out that sorted() takes care of the list conversion.
In line 2, we use enumerate() so we can iterate over the indices and values of our list of unique values: https://docs.python.org/3/library/functions.html#enumerate
The rest of line 2 is a simple dict comprehension to build a dictionary of letter -> number mappings. We use the dictionary in line 3 to look up the numbers for each letter in our input dict.
You might have to modify this slightly depending on how you want to handle duplicates :)
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.