nested-lists

How to iterate nested lists with another list to create a dictionary of lists Python

≡放荡痞女 提交于 2020-01-06 19:45:16
问题 I'm using Mibian module to calculate call options. I have a list of three nested lists. Each nested list represent strike prices. Each nested list has their own respective days left to expiration, i.e. my_list[2] has 30 days left. import mibian as mb import pandas as pd my_list = [[20, 25, 30, 35, 40, 45], [50, 52, 54, 56, 58, 60, 77, 98, 101], [30, 40, 50, 60]] days_left = [5, 12, 30] my_list[2] [30, 40, 50, 60] days_left[2] 30 The structure of a Mibian Black-Scholes code for calculating the

__getitem__ with slices on a list of lists

风格不统一 提交于 2020-01-04 15:31:58
问题 I'm creating a class representing a list of lists. __getitem__ is giving me headaches. Everything goes swimmingly until I introduce slices as parameters. Demonstration code # Python version 2.7.5 class NestedLists: _Cells = [['.', '.', '.', '.', '.'], ['.', '.', 'N', '.', '.'], ['.', 'C', 'A', 'T', '.'], ['.', '.', 'P', '.', '.'], ['.', '.', '.', '.', '.']] def __getitem__(self, index): if isinstance(index, int): return self._Cells[index] elif isinstance(index, slice): return self._Cells

__getitem__ with slices on a list of lists

本小妞迷上赌 提交于 2020-01-04 15:31:08
问题 I'm creating a class representing a list of lists. __getitem__ is giving me headaches. Everything goes swimmingly until I introduce slices as parameters. Demonstration code # Python version 2.7.5 class NestedLists: _Cells = [['.', '.', '.', '.', '.'], ['.', '.', 'N', '.', '.'], ['.', 'C', 'A', 'T', '.'], ['.', '.', 'P', '.', '.'], ['.', '.', '.', '.', '.']] def __getitem__(self, index): if isinstance(index, int): return self._Cells[index] elif isinstance(index, slice): return self._Cells

How can I get the total number of elements in my arbitrarily nested list of lists?

允我心安 提交于 2020-01-02 00:58:09
问题 I have a list assigned to the variable my_list . The value of my_list is [[1,2,3],[3,5,[2,3]], [[3,2],[5,[4]]]] . I need to find the length of my_list , but len(my_list) only returns 3. I want it to return 11. Is there any Python functions that will return the full length of my_list nested lists and all. Example: Input [[1,2,3],[3,5,[2,3]], [[3,2],[5,[4]]]] Output 11 I would like if this worked for not only numbers, but strings also. 回答1: This function counts the length of a list, counting

Nested list based on flat/condensed name structure of named vector or list

笑着哭i 提交于 2019-12-31 03:02:07
问题 I'm looking for a simple way to create a nested list based on a "flat" or condensed name structure of either a named vector or a list E.g., input c("a/b/c" = TRUE) should result in: #> $a #> $a$b #> $a$b$c #> [1] TRUE I have a solution, but it feels pretty involved: library(magrittr) nested_list <- function(input) { nms <- names(input) ret <- lapply(1:length(input), function(level) { value <- input[[level]] name <- nms[level] %>% strsplit("/") %>% unlist() name_vec <- NULL ret <- list() #

replacing items in nested lists python

蹲街弑〆低调 提交于 2019-12-30 11:35:09
问题 I am trying to make a duplicate list of lists and change one element to another within the nested lists of the duplicate list but am having some trouble. How I made the duplicate list: order = [['yhjK', 'F'], 'gap', ['bcsA', 'F'], ['bcsB', 'F'], ['bcsZ', 'F'], 'gap', ['yhjK', 'R']] #order_1 = list(order) #this makes the duplicate list as well order_1 = [] for x in order: order_1.append(x) How I changed the elements: for item in order_1: for n,i in enumerate(item): if i=='R': item[n]='F' if i=

Replicate a list to create a list-of-lists

余生颓废 提交于 2019-12-30 08:13:20
问题 I am trying to create a list with the following (nested) structure: l <- list() for(i in seq(5)) l[[i]] <- list(a=NA,b=NA) > str(l) List of 5 $ :List of 2 ..$ a: logi NA ..$ b: logi NA $ :List of 2 ..$ a: logi NA ..$ b: logi NA $ :List of 2 ..$ a: logi NA ..$ b: logi NA $ :List of 2 ..$ a: logi NA ..$ b: logi NA $ :List of 2 ..$ a: logi NA ..$ b: logi NA I'd like to do this via rep or similar, as I'm creating a whole bunch of blank lists which I will later fill in. (I'm aware that I can just

Add two matrices in python

旧巷老猫 提交于 2019-12-30 05:50:26
问题 I'm trying to write a function that adds two matrices to pass the following doctests: >>> a = [[1, 2], [3, 4]] >>> b = [[2, 2], [2, 2]] >>> add_matrices(a, b) [[3, 4], [5, 6]] >>> c = [[8, 2], [3, 4], [5, 7]] >>> d = [[3, 2], [9, 2], [10, 12]] >>> add_matrices(c, d) [[11, 4], [12, 6], [15, 19]] So I wrote a function: def add(x, y): return x + y And then I wrote the following function: def add_matrices(c, d): for i in range(len(c)): print map(add, c[i], d[i]) And I sort of get the right answer

Consistent Styling for Nested Lists with Bootstrap

老子叫甜甜 提交于 2019-12-29 10:13:08
问题 Is there a way to make nested lists in twitter bootstrap look like a normal list, with the nested items simply indented (and have it work for an arbitrarily deep nesting)? By default, nested lists look like this: <ul class="list-group"> <li class="list-group-item">One</li> <li class="list-group-item">Two <ul class="list-group"> <li class="list-group-item">Item 2a</li> <li class="list-group-item">Item 2b</li> </ul> </li> <li class="list-group-item">Three <ul class="list-group"> <li class="list

Generating sublists using multiplication ( * ) unexpected behavior [duplicate]

拥有回忆 提交于 2019-12-27 11:27:09
问题 This question already has answers here : List of lists changes reflected across sublists unexpectedly (13 answers) Nested List Indices [duplicate] (2 answers) Closed 6 years ago . I'm sure this has been answered somewhere but I wasn't sure how to describe it. Let's say I want to create a list containing 3 empty lists, like so: lst = [[], [], []] I thought I was being all clever by doing this: lst = [[]] * 3 But I discovered, after debugging some weird behavior, that this caused an append