How to find in a list what thats number python3 - python-3.4

Im trying to find out the number of where X is in the list e.g:
if i had a list like: ['a','b','c','d'] and i have 'c' how would i find where it is in the list, so that it would print '2' (as thats where it is in the list)
thanks

Use the built-in method list.index
If you wanna know where is 'c':
l = ['a','b','c']
l.index('c')

Related

Is there a way to print out single values from a multidimentional list? in python please

Imagine i have a multidimensional List like this vals = [['John', '20'], ['Derron', '5'], ['Mike', '43']], what can i do to print out only the names e.g: John, Derron, Mike
In which language you want to do this? Using JavaScript you can try below solution.
function print(val) {
console.log(val[0])
}
vals.forEach(print)
You can use a nested for loop. In python For example:
for sublist in vals:
print(sublist[0])
The first line of this code will loop through each sublist. For this question, ['John','20'] is a sublist. The second line will print out the first element (aka name) of each of these sublists.

R - Why does frameex[ind, ] needs a ", " to display rows

I am new to R and I have troubles understanding how displaying an index works.
# Find indices of NAs in Max.Gust.SpeedMPH
ind <- which(is.na(weather6$Max.Gust.SpeedMPH))
# Look at the full rows for records missing Max.Gust.SpeedMPH
weather6[ind, ]
My code here works, no problem but I don't understand why weather6[ind] won't display the same thing as weather6[ind, ] . I got very lucky and mistyped the first time.
I apologize in advance that the question might have been posted somewhere else, I searched and couldn't find a proper answer.
So [ is a function just like any other function in R, but we call it strangely. Another way to write it in this case would be:
'[.data.frame'(weather6,ind,)
or the other way:
'[.data.frame'(weather6,ind)
The first three arguments to the function are named x, i and j. If you look at the code, early on it branches with the line:
if (Narg < 3L)
Putting the extra comma tells R that you've called the function with 3 arguments, but that the j argument is "missing". Otherwise, without the comma, you have only 2 arguments, and the function code moves on the the next [ method for lists, in which it will extract the first column instead.

named Element-wise operations in R

I am a beginner in R and apologize in advance for asking a basic question, but I couldn't find answer anywhere on Google (maybe because the question is so basic that I didn't even know how to correctly search for it.. :D)
So if I do the following in R:
v = c(50, 25)
names(v) = c("First", "Last")
v["First"]/v["Last"]
I get the output as:
First
2
Why is it that the name, "First" appears in the output and how to get rid of it?
From help("Extract"), this is because
Subsetting (except by an empty index) will drop all attributes except names, dim and dimnames.
and
The usual form of indexing is [. [[ can be used to select a single element dropping names, whereas [ keeps them, e.g., in c(abc = 123)[1].
Since we are selecting single elements, you can switch to double-bracket indexing [[ and names will be dropped.
v[["First"]] / v[["Last"]]
# [1] 2
As for which name is preserved when using single bracket indexing, it looks like it's always the first (at least with the / operator). We'd have to go digging into the C source for further explanation. If we switch the order, we still get the first name on the result.
v["Last"] / v["First"]
# Last
# 0.5

How to split all elements of list in python?

I have a list of form:
list = ['1.25','5.26','8.55']
I want this list to get splitted as :
list = [1.25],[5.26],[8.55]
so, could you please help how can i have above stated splitted list using python.
I am new to python So, apologies if its something very common. need help.
list = ['1.25','5.26','8.55']
newList = []
for i in list:
newList.append([float(i)])
list=newList
The list is now a list-of-lists.
Also, try not to name your variables as list, tuple, etc. as they are reserved words. Give your list a new name, a meaningful one would be self-documenting and will further help you in the long-run.

python 3 convert list slice into string

I have a list with numbers
['1111', '1112', '1113', '1114', '1115']
and need to pull one slice and convert to string
Im using
str(list[0:1])
but that outputs ['1111'] when I only need the numbers
There is some info on the site regarding .join but I've only found posts talking about the entire list not just one piece of it.
Ive also tried using the .strip command but cant seem to get that to work.
A slice of a list is a list, thus you can join a slice just the same way as you would join a list.
>>> lst = ['1111', '1112', '1113', '1114', '1115']
>>> ''.join(lst[0:1])
'1111'
Note that when using slice-notation, the to index is exclusive, thus the slice [0:1] has just the zeroth element. In case you wanted the zeroth and first element, use [0:2]:
>>> ''.join(lst[0:2])
'11111112'
Of course, if you really just want one element from the list, there's no need for a slice (and for join); just use the index:
>>> lst[0]
'1111'
Is
list[0]
what you're looking for?

Resources