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.
Related
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.
I am working on a list of .csv data which I have read in and keep the variables that i need for study. During this process, I have build multiple data set with name xxx_101(PY_101, vB_101_FG_101, etc.) which store in global environment. Now I want to put every new data set with ending _101 into a list. Is it a clever way to build that list other than type them in one by one? Once I read them in to a list, I would like to rename the each list with their original data name. Is there a easy way to do that?
I could do it one by one, but just feel there should be a better way to do. Thanks.
We can use mget with ls and specify the pattern with "_101" as the end ($) of the object name. It would get the values of all those objects into a list
lst1 <- mget(ls(pattern = "_101$"))
ive created a lot of character objects in R that i would like to put into a list (storing all their information).
the object looks like this and the pattern is "TMC"
str(TMCS09g10086933)
chr [1:10] "TMCS09g1008699" "TMCS09g1008610 "TMCS09g10086101" "TMCS09g10086104" "TMCS09g100864343" "TMCS09g10086434343" "TMCS09g10086994111" ...
i have hundreds of these objects. Could someone tell me how to do this?
You can use the function objects with the argument pattern to list them.
Then, you can call the function get to fetch them. If you do this with an lapply, you will get a list returned right away.
TMClist <- lapply(objects(pattern = "^TMC"), get)
First you need to find the objects, which you can do with a regex search through the list of the objects in your environment grep("^TMC", ls(), value = TRUE), then you need to get the objects using the character vector of their names. For that you use mget.
your_list <- mget(grep("^TMC", ls(), value = TRUE))
I am attempting to count all the instances across a list of data frames where a certain variable is over a given value. I have tried to do it as so:
for (name in myList){
nrow(subset(myList[[name]], var >=6))
}
as I found here: http://www.statisticsblog.com/2010/03/r-tip-iterating-over-list/
However, I get the following error:
Error in myList[[name]] : invalid subscript type 'list'
I know that nrow works because I have used it on a specific list item outside of the loop and it succeeded. I can't seem to figure out why the error is arising. The list names are set up as so:
myList$`i.j.k`
with i, j, and k each taking on a different numerical value. I generated the list as so from a data frame read in from a .csv file:
myList <- split(data, f=list(data$i, data$j, data$k))
What is causing the error? Or, is there a better way to do a conditional count across all list elements (there are 2000+ of them, so any non-loop way would be ideal). Thanks!
I figured it out thanks to the comment from #PoGibas:
Rather than
for (name in myList){
nrow(subset(myList[[name]], var >=6))
}
it should be:
for (name in myList){
nrow(subset(name, var >=6))
}
I'm attempting to scrape a page that has about 10 columns using Ruby and Nokogiri, with most of the columns being pretty straightforward by having unique class names. However, some of them have class ids that seem to have long number strings appended to what would be the standard class name.
For example, gametimes are all picked up with .eventLine-time, team names with .team-name, but this particular one has, for example:
<div class="eventLine-book-value" id="eventLineOpener-118079-19-1522-1">-3 -120</div>
.eventLine-book-value is not specific to this column, so it's not useful. The 13 digits are different for every game, and trying something like:
def nodes_by_selector(filename,selector)
file = open(filename)
doc = Nokogiri::HTML(file)
doc.css(^selector)
end
Has left me with errors. I've seen ^ and ~ be used in other languages, but I'm new to this and I have tried searching for ways to pick up all data under id=eventLineOpener-XXXX to no avail.
To pick up all data under id=eventLineOpener-XXXX, you need to pass 'div[id*=eventLineOpener]' as the selector:
def nodes_by_selector(filename,selector)
file = open(filename)
doc = Nokogiri::HTML(file)
doc.css(selector) #doc.css('div[id*=eventLineOpener]')
end
The above method will return you an array of Nokogiri::XML::Element objects having id=eventLineOpener-XXXX.
Further, to extract the content of each of these Nokogiri::XML::Element objects, you need to iterate over each of these objects and use the text method on those objects. For example:
doc.css('div[id*=eventLineOpener]')[0].text