reverse in between values of first and last index of list with list slicing - python-3.6

I tried to slicing in between values of first and last of list. Can anyone explain following code.
where index jump value effecting the list and giving output, if i don't mention index jump value its giving empty list.
l = [1, 2, 3, 4, 5, 6, 7]
l[5:-7:-1]
output :- [6, 5, 4, 3, 2] # This is the exact output what i want.
l[5:-7]
output :- []

In the above example, "-1" means in reverse direction (right-to-left).
So, l[5:-7:-1] is equivalent to l[5:0:-1]. (-7 from last index is 0)
Hence it prints all values from index 5 to index 0 in reverse direction with 1 jump.
Similarly, l[5:-7] is equivalent to l[5:0] (default jump is +1).
Since no item is present between index 5 and index 0 (left-to-right direction), hence empty list is returned.

Related

All subsets of a list in Perl 6

I wonder (remembering that Perl 6 has everything you could wish), whether there are some built-in instruments that can help to produce all the non-empty subsets (order doesn't matter) of a list.
E.g., I have a list:
my #a = 1, 2, 3;
I need a function f so that f(#a) will produce:
((1), (2), (3), (1, 2), (1, 3), (2, 3), (1, 2, 3))
#a.combinations(1..*)
will return the Seq you're looking for. Note that without the argument, an empty list would be generated as first element.

Julia DataFrames.jl - filter data with NA's (NAException)

I am not sure how to handle NA within Julia DataFrames.
For example with the following DataFrame:
> import DataFrames
> a = DataFrames.#data([1, 2, 3, 4, 5]);
> b = DataFrames.#data([3, 4, 5, 6, NA]);
> ndf = DataFrames.DataFrame(a=a, b=b)
I can successfully execute the following operation on column :a
> ndf[ndf[:a] .== 4, :]
but if I try the same operation on :b I get an error NAException("cannot index an array with a DataArray containing NA values").
> ndf[ndf[:b] .== 4, :]
NAException("cannot index an array with a DataArray containing NA values")
while loading In[108], in expression starting on line 1
in to_index at /Users/abisen/.julia/v0.3/DataArrays/src/indexing.jl:85
in getindex at /Users/abisen/.julia/v0.3/DataArrays/src/indexing.jl:210
in getindex at /Users/abisen/.julia/v0.3/DataFrames/src/dataframe/dataframe.jl:268
Which is because of the presence of NA value.
My question is how should DataFrames with NA should typically be handled? I can understand that > or < operation against NA would be undefined but == should work (no?).
What's your desired behavior here? If you want to do selections like this you can make the condition (not a NAN) AND (equal to 4). If the first test fails then the second one never happens.
using DataFrames
a = #data([1, 2, 3, 4, 5]);
b = #data([3, 4, 5, 6, NA]);
ndf = DataFrame(a=a, b=b)
ndf[(!isna(ndf[:b]))&(ndf[:b].==4),:]
In some cases you might just want to drop all rows with NAs in certain columns
ndf = ndf[!isna(ndf[:b]),:]
Regarding to this question I asked before, you can change this NA behavior directly in the modules sourcecode if you want. In the file indexing.jl there is a function named Base.to_index(A::DataArray) beginning at line 75, where you can alter the code to set NA's in the boolean array to false. For example you can do the following:
# Indexing with NA throws an error
function Base.to_index(A::DataArray)
A[A.na] = false
any(A.na) && throw(NAException("cannot index an array with a DataArray containing NA values"))
Base.to_index(A.data)
end
Ignoring NA's with isna() will cause a less readable sourcecode and in big formulas, a performance loss:
#timeit ndf[(!isna(ndf[:b])) & (ndf[:b] .== 4),:] #3.68 µs per loop
#timeit ndf[ndf[:b] .== 4, :] #2.32 µs per loop
## 71x179 2D Array
#timeit dm[(!isna(dm)) & (dm .< 3)] = 1 #14.55 µs per loop
#timeit dm[dm .< 3] = 1 #754.79 ns per loop
In many cases you want to treat NA as separate instances, i.e. assume that that everything that is NA is "equal" and everything else is different.
If this is the behaviour you want, current DataFrames API doesn't help you much, as both (NA == NA) and (NA == 1) returns NA instead of their expected boolean results.
This makes extremely tedious DataFrame filters using loops:
function filter(df,c)
for r in eachrow(df)
if (isna(c) && isna(r:[c])) || ( !isna(r[:c]) && r[:c] == c )
...
and breaks select-like functionalities in DataFramesMeta.jl and Query.jl when NA values are present or requested for..
One workaround is to use isequal(a,b) in place of a==b
test = #where(df, isequal.(:a,"cc"), isequal.(:b,NA) ) #from DataFramesMeta.jl
I think the new syntax in Julia is to use ismissing:
# drop NAs
df = DataFrame(col=[0,1,1,missing,0,1])
df = df[.!ismissing.(df[:col]),:]

Select elements in named vector

I'm a begginer with R and I can't figure out how to do this:
I have a named vector with player names and his score:
x <-c(3, 4, 6, 2, 3, 5, 0, 1, 1, 2)
names(x) <- c("ALBERTO", "ANTONIO", "PEPE", "JUAN", "ANDRES", "PEDRO", "MARCOS", "MATEO", "JAVIER", "FRANCISCO")
What I need is to get the scores for the players which name starts with letter "A".
Is it possible to set a condition on the element name?
Thank you!
One way is
x[grepl("^A", names(x))]
# ALBERTO ANTONIO ANDRES
# 3 4 3
^ stands for beginning of the string in regex. grepl will return a logical vector which will allow to index out of x
Or (as pointed in comments) you could avoid regex and do
x[substr(names(x), 1, 1) == 'A']

R subscript based on a vector

df <- data.frame(name=c('aa', 'bb', 'cc','dd'),
code=seq(1:4), value= seq(100, 400, by=100))
df
v <- c(1, 2, 2)
v
A <- df[df$code %in% v,]$value
A
str(A)
I tried to obtain the corresponding value based on the code. I was expecting A to be of length 3; but it actually returns a vector of 2. What can I do if I want A to be a vector of 3, that is c(100,200,200)?
%in% returns a logical vector, the same length as vector 1, that indicates whether each element of vector 1 occurs in vector 2.
In contrast, the match function returns, for each element of vector 1, the position in vector 2 where the element first appears (or NA if it doesn't exist in vector 2). Try the following:
df[match(v, df$code), 'value']
You could just use v as an argument if those were the lines whose "value"s you wanted:
> df[v,]$value
[1] 100 200 200
df[v,2] # minimum characters :)

igraph assign a vector as an attribute for a vertex

I am trying to assign a vector as an attribute for a vertex, but without any luck:
# assignment of a numeric value (everything is ok)
g<-set.vertex.attribute(g, 'checked', 2, 3)
V(g)$checked
.
# assignment of a vector (is not working)
g<-set.vertex.attribute(g, 'checked', 2, c(3, 1))
V(g)$checked
checking the manual, http://igraph.sourceforge.net/doc/R/attributes.html
it looks like this is not possible. Is there any workaround?
Up till now the only things I come up with are:
store this
information in another structure
convert vector to a string with delimiters and store as a string
This works fine:
## replace c(3,1) by list(c(3,1))
g <- set.vertex.attribute(g, 'checked', 2, list(c(3, 1)))
V(g)[2]$checked
[1] 3 1
EDIT Why this works?
When you use :
g<-set.vertex.attribute(g, 'checked', 2, c(3, 1))
You get this warning :
number of items to replace is not a multiple of replacement length
Indeed you try to put c(3,1) which has a length =2 in a variable with length =1. SO the idea is to replace c(3,1) with something similar but with length =1. For example:
length(list(c(3,1)))
[1] 1
> length(data.frame(c(3,1)))
[1] 1

Resources