Rename List Items

Renaming items in python list

With speed in mind and for a lot of elements:

%%timeit
arr = ['item1', 'item2', 'item3']
arr = np.char.replace(arr, 'item', 'person')

16.4 µs ± 1.07 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%%timeit
arr = ['item1', 'item2', 'item3']
arr = [x.replace('item', 'person') for x in arr]

1.42 µs ± 174 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Even for 100.000 Elements it seems to be slower to use numpy:

Numpy: 177 ms ± 15.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

ListComprehension: 35.7 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Even Pandas.Series is slower on my tests:

%%timeit
series.str.replace('item', 'person')

144 ms ± 4.47 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Rename list elements which has names starting with specified characters using purrr package

You can simply use setNames() or set_names():

list.old <- list(
x.height=1, x.weight=2, y.height=3, y.length=4, z.weight=5, z.price=6
)

list.old %>%
keep(startsWith(names(.), prefix)) %>%
set_names(str_replace(names(.), prefix, ""))
# $height
# [1] 1
#
# $weight
# [1] 2

And to apply to many prefixes, use the previous code as a function:

prefix_list <- c("x","y","z")

map(prefix_list,
function(prefix) list.old %>%
keep(startsWith(names(.), prefix)) %>%
set_names(str_replace(names(.), prefix, ""))
) %>%
set_names(prefix_list)
# $x
# $x$.height
# [1] 1
#
# $x$.weight
# [1] 2
#
#
# $y
# $y$.height
# [1] 3
#
# $y$.length
# [1] 4
#
#
# $z
# $z$.weight
# [1] 5
#
# $z$.price
# [1] 6

How to rename elements inside a list of lists in the an efficient manner in R?

for(i in seq_along(List.5000))
names(List.5000[[i]])[names(List.5000[[i]]) == 'B'] <- 'Z'

Or, if you prefer lapply/map:

List.5000 <- lapply(List.5000, 
function(x) {names(x)[names(x) == 'B'] <- 'Z'; x})

library(purrr)
List.5000 <- map(List.5000, ~{names(.)[names(.) == 'B'] <- 'Z'; .})

If the names really are all the same you could just do

ind <- names(List.5000[[1]]) == 'B'
for(i in seq_along(List.5000))
names(List.5000[[i]])[ind] <- 'Z'

The bracket syntax is a little faster:

x <- List.5000[[1]]
microbenchmark(
sub = names(x) <- sub("^B$", "Z", names(x))
, ifelse = names(x) <- ifelse(names(x) == 'B', 'Z', names(x))
, stringi = names(x) <- str_replace(names(x), "^B$", "Z")
, replace = names(x) <- replace(names(x), names(x) == 'B', 'Z')
, bracket = names(x)[names(x) == 'B'] <- 'Z'
)
# Unit: microseconds
# expr min lq mean median uq max neval
# sub 22.041 31.2265 58.24097 46.9650 78.5075 373.637 100
# ifelse 13.309 22.4110 44.00665 30.2235 65.1395 113.693 100
# stringi 153.880 313.0400 346.41543 358.4795 383.4130 631.354 100
# replace 4.067 6.3205 13.09022 8.1760 11.9280 54.075 100
# bracket 3.246 4.5265 10.38177 5.9180 7.9925 55.278 100

Still a little unsatisfying, as none of these methods modify the list in place.

Rename items on python list by list index

Does this suffice?

myList = ['id', 'brandName', 'EngagedUsers_Week_47', 'EngagedUsers_Week_48', 'EngagedUsers_Week_49',
'EngagedUsers_Week_50', 'Week_47_VS_Week_48', 'Week_48_VS_Week_49', 'Week_49_VS_Week_50']

myNewList = []
for item in myList:
if item[:2] == 'id':
myNewList.append(item)
elif item == 'brandName':
myNewList.append('name')
elif item[:12] == 'EngagedUsers':
myNewList.append(item[13:17]+item[-2:])
elif item[:4] == 'Week':
myNewList.append(item[5:7]+item[8:10]+item[-2:])

print(myNewList)
['id', 'name', 'Week47', 'Week48', 'Week49', 'Week50', '47VS48', '48VS49', '49VS50']

How do you rename the elements of a list when they are nested within another list using R's purrr package?

You can just use lapply to rename each list.

lapply(list_old, \(x) {
names(x) <- c("start", "end")
x
}
)
#> [[1]]
#> [[1]]$start
#> [1] ""
#>
#> [[1]]$end
#> [1] "1854-12-01"
#>
#>
#> [[2]]
#> [[2]]$start
#> [1] "1857-06-01"
#>
#> [[2]]$end
#> [1] "1858-12-01"

You could use purrr::map() if you'd rather a tidy solution. If using purrr, from @Donald Seinen's comment, can also use set_names()

library(purrr)

map(
list_old,
set_names,
c("start", "end")
)
#> [[1]]
#> [[1]]$start
#> [1] ""
#>
#> [[1]]$end
#> [1] "1854-12-01"
#>
#>
#> [[2]]
#> [[2]]$start
#> [1] "1857-06-01"
#>
#> [[2]]$end
#> [1] "1858-12-01"

Rename list of lists using a named list

This recursive function should do the trick :

# you will do this but I couldn't install your packages
# nodeD <- nodeData(g, nodes(g), "label")

nodeD <- list(`SO:0001968` = "coding_transcript_variant",
`SO:0001969` = "coding_transcript_intron_variant",
`SO:0001622` = "UTR_variant",
`SO:0001623` = "5_prime_UTR_variant",
`SO:0001624` = "3_prime_UTR_variant",
`SO:0002090` = "3_prime_UTR_intron_variant",
`SO:0002091` = "5_prime_UTR_intron_variant")

rename_items <- function(item){
if (is.list(item)){
item <- lapply(item,rename_items)
names(item) <- unname(nodeD[names(item)])
}
item
}

tree2 <- rename_items(tree)

Result

# $coding_transcript_variant
# $coding_transcript_variant$UTR_variant
# $coding_transcript_variant$UTR_variant$`3_prime_UTR_variant`
# $coding_transcript_variant$UTR_variant$`3_prime_UTR_variant`$`3_prime_UTR_intron_variant`
# [1] 1
#
#
# $coding_transcript_variant$UTR_variant$`5_prime_UTR_variant`
# $coding_transcript_variant$UTR_variant$`5_prime_UTR_variant`$`5_prime_UTR_intron_variant`
# [1] 1
#
#
#
# $coding_transcript_variant$coding_transcript_intron_variant
# $coding_transcript_variant$coding_transcript_intron_variant$`3_prime_UTR_intron_variant`
# [1] 1
#
# $coding_transcript_variant$coding_transcript_intron_variant$`5_prime_UTR_intron_variant`
# [1] 1


Related Topics



Leave a reply



Submit