How to Extract Elements from a List with Mixed Elements

How to extract elements from a list with mixed elements

If you want to extract the third element of each list element you can do:

List <- list(c(1:3), c(4:6), c(7:9))
lapply(List, '[[', 3) # This returns a list with only the third element
unlist(lapply(List, '[[', 3)) # This returns a vector with the third element

Using your example and taking into account @GSee comment you can do:

yourList <- list(c("","668","12345_s_at","667", "4.899777748","49.53333333",
"10.10930207", "1.598228663","5.087437057"),
c("","376", "6789_at", "375", "4.899655078","136.3333333",
"27.82508792", "2.20223398", "5.087437057"),
c("", "19265", "12351_s_at", "19264", "4.897730912",
"889.3666667", "181.5874908","1.846451572","5.087437057" ))

sapply(yourList, '[[', 3)
[1] "12345_s_at" "6789_at" "12351_s_at"

Next time you can provide some data using dput on a portion of your dataset so we can reproduce your problem easily.

How do I extract mixed elements from a long list in pandas/ python

First of all, know your input. Your actual string looks like

Team A v Team Bn
13 Jan 20:00 +147 st
ScoreO (1.5)
1.142
ScoreU (1.5)
5.50
ScoreO (2.5)
1.48
ScoreU (2.5)
2.65
ScoreO (3.5)
2.15
ScoreU (3.5)
1.666
ScoreO (4.5)
3.60
ScoreU (4.5)
1.285
ScoreO (5.5)
6.50
ScoreU (5.5)
1.10

Now, you want to get a float numeric value that is located on lines below the lines equal to ScoreO (2.5) and ScoreU (2.5).

Thus, you can set the context with either Score[OU] \(2\.5\)\n or just \(2\.5\)\n since this is enough to find the context you need to start matching from, and then you may either use (.+) (you already know the value you need is a whole line) or \d+\.\d+ (since you know it is a float value).

So, you may use either of the two solutions below:

>>> df['Points'].str.findall(r'Score[OU] \(2\.5\)\n(\d+\.\d+)').str.join('\n')
0 1.48\n2.65
>>> df['Points'].str.findall(r'\(2\.5\)\n(.+)').str.join('\n')
0 1.48\n2.65

See the regex demo

Python get number from mixed list

You have a tuple with a list nested inside that contains a string '3' so you can format that however you want.

This is verified by:

count =('OK', ['3'])
print(type(count))
for item in count:
if isinstance(item, int):
print(item)
elif isinstance(item, list):
print(item)

resulting in:

<class 'tuple'>
['3']

To access it as mentioned in comments:

number = int(count[1][0])

combining elements in a list of lists in R

Another base R option using paste

do.call(paste, c(data.frame(t(list2DF(samples_ID))), sep = "_"))

or

do.call(paste, data.frame(do.call(rbind, samples_ID)), sep = "_"))

Extract specific elements from a multidimensional array

You can try

let p:[[Double]] = [[3,2],[4,1]]
let t:[[Int]] = [[0,0],[1,0]]
let res = t.map { p[$0.first!][$0.last!] }
print(res)

Extracting elements of nested list by contained value using purrr

The question does not define what the output is supposed to look like so we will assume that it should be a list of lists. Remove the first layer using flatten as shown and then filter its elements using keep.

library(purrr)
my_list %>%
flatten %>%
keep(~ .x$name == "Date")

In base R this could be written:

Filter(function(x) x$name == "Date", do.call("c", my_list))

Extract elements from list

There are lots of syntax errors in your code (mostly dealing with incorrect usage of brackets). As mentioned in the comments, you should spend some time familiarizing yourself with the syntax of the language before tackling any other problem. Having said that, this is what I believe you intended to do, study the following solution and pay attention to those brackets!

(define (extract-sym lst)                  ; don't use "list" as a parameter name
(cond ((null? lst) '()) ; if list is empty, return empty list
((eq? (car lst) 'sym) ; if current element is 'sym
(cons (car lst) ; add it to the output using cons
(extract-sym (cdr lst)))) ; and advance recursion
((pair? (car lst)) ; if current element is a list
(append (extract-sym (car lst)) ; recursively process and use append
(extract-sym (cdr lst)))) ; also, advance recursion
(else (extract-sym (cdr lst))))) ; otherwise just advance recursion

For example:

(extract-sym '(1 sym 2 (3 sym 4) (5) sym))
=> '(sym sym sym)

R : Extract elements of same index & depth level of a list

With purrr, you can use at_depth(2, ...) where 2 indicates the depth level, and ... is an extractor (name/integer) or function. Simplifying the structure afterwards,

library(purrr)

toy_list %>% at_depth(2, 2) %>% flatten()

## [[1]]
## [1] "a" "b" "c"
##
## [[2]]
## [1] "d" "e"
##
## [[3]]
## [1] "a" "b" "c" "d" "e" "f" "g" "h"

python: extract integers from mixed list

The best approach is not to use type, but to use a chain of isinstance calls. The pitfall of using type is that someone could subclass int in the future, and then your code won't work. Also, since you are using Python 2.x, you need to consider numbers greater than or equal to 2^31: these are not ints. You need to consider the long type:

def parseIntegers(mixedList):
return [x for x in testList if (isinstance(x, int) or isinstance(x, long)) and not isinstance(x, bool)]

Reason for needing to consider long:

>>> a = 2 ** 31
>>> isinstance(a, int)
False


Related Topics



Leave a reply



Submit