nested-lists

How to create nested lists in python?

末鹿安然 提交于 2019-12-01 03:46:41
问题 I know you can create easily nested lists in python like this: [[1,2],[3,4]] But how to create a 3x3x3 matrix of zeroes? [[[0] * 3 for i in range(0, 3)] for j in range (0,3)] or [[[0]*3]*3]*3 Doesn't seem right. There is no way to create it just passing a list of dimensions to a method? Ex: CreateArray([3,3,3]) 回答1: In case a matrix is actually what you are looking for, consider the numpy package. http://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html#numpy.zeros This will give

sum of first value in nested list

╄→尐↘猪︶ㄣ 提交于 2019-12-01 03:13:26
问题 In traditional python, the sum function gives the sum of a list : sum([0,1,2,3,4])=10 On the other hand, what if you have a nested list as so: sum([[1,2,3],[4,5,6],[7,8,9]]) We find the error: Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'list' In addition to this, how could we find the sum of the first values (index 0) in a nested list? Such as: something([[1,2,3],[4,5,6],[7,8,9]])=12 回答1: To get the sum of all

Python: merge nested lists

送分小仙女□ 提交于 2019-11-30 22:02:28
beginner to python here. I have 2 nested lists that I want to merge: list1 = ['a', (b, c), (d, e), (f, g, h) ] list2 = [(p,q), (r, s), (t), (u, v, w) ] the output I am looking for is: list3 = [(a, p, q), (b, c, r, s), (d, e, t), (f, g, h, u, v, w) ] Can this be done without any external libraries? note: len(list1) = len(list2) Use the power of the zip function and list comprehensions : list1 = [('a', ), ('b', 'c'), ('d', 'e'), ('f', 'g', 'h') ] list2 = [('p', 'q'), ('r', 's'), ('t', ), ('u', 'v', 'w') ] print [a + b for a, b in zip(list1, list2)] from operator import add list3 = map(add, list1

Add two matrices in python

十年热恋 提交于 2019-11-30 17:37:28
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. Matrix library You can use the numpy module, which has support for this. >>> import numpy as np >>> a

Python: merge nested lists

本秂侑毒 提交于 2019-11-30 15:49:02
问题 beginner to python here. I have 2 nested lists that I want to merge: list1 = ['a', (b, c), (d, e), (f, g, h) ] list2 = [(p,q), (r, s), (t), (u, v, w) ] the output I am looking for is: list3 = [(a, p, q), (b, c, r, s), (d, e, t), (f, g, h, u, v, w) ] Can this be done without any external libraries? note: len(list1) = len(list2) 回答1: Use the power of the zip function and list comprehensions: list1 = [('a', ), ('b', 'c'), ('d', 'e'), ('f', 'g', 'h') ] list2 = [('p', 'q'), ('r', 's'), ('t', ), (

How to separate styles in a nested list styling?

天大地大妈咪最大 提交于 2019-11-30 13:03:16
I have a list and list also has list in it. I set styles on parent list but I want different styles for parent and child list but they are mixed somehow I can't separate them. HTML file: <ul id="accountNavigation"> <li><a href="#">Something</a></li> <li id="userNavigation"> <img src="https://si0.twimg.com/profile_images/135460415/UAB_dragon_head_normal.png" alt=""/> <a href="#">Username</a> <div class="showme"> <ul id="userNavigationSubMenu"> <li>test</li> <li>test</li> <li>test</li> <li>test</li> <li>test</li> <li>test</li> <li>test</li> </ul> </div> </li> </ul> CSS file: body{background:

What is an idiomatic way to add lists in Haskell?

我与影子孤独终老i 提交于 2019-11-30 03:20:11
问题 Suppose I want to add two lists in Haskell. What is the most usual way to do this? Here's what I did: addLists :: (Integral a) => [a] -> [a] -> [a] addLists xs ys = map add $ zip xs ys where add (x, y) = x+y 回答1: There is a zipWith library function that combines two lists by using a supplied function. It does exactly what you want here and you get: addLists = zipWith (+) This uses (+) to combine the elements of lists given as further arguments. 回答2: Applicative Functor style: import Control

Convert nested list to 2d array

♀尐吖头ヾ 提交于 2019-11-30 03:00:31
问题 I'm trying to convert a nested list into a 2d array. List<List<String>> list = new ArrayList<>(); list.add(Arrays.asList("a", "b", "c")); list.add(Arrays.asList("dd")); list.add(Arrays.asList("eee", "fff")); I want to make this a String[][] . I've tried the following: String[][] array = (String[][]) list.toArray(); // ClassCastException String[][] array = list.toArray(new String[3][3]); // ArrayStoreException String[][] array = (String[][]) list.stream() // ClassCastException .map(sublist ->

Consistent Styling for Nested Lists with Bootstrap

江枫思渺然 提交于 2019-11-29 19:25:38
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-group-item">Item 3a</li> <li class="list-group-item">Item 3b</li> </ul> </li> </ul> How can I make

How to separate styles in a nested list styling?

≡放荡痞女 提交于 2019-11-29 18:33:40
问题 I have a list and list also has list in it. I set styles on parent list but I want different styles for parent and child list but they are mixed somehow I can't separate them. HTML file: <ul id="accountNavigation"> <li><a href="#">Something</a></li> <li id="userNavigation"> <img src="https://si0.twimg.com/profile_images/135460415/UAB_dragon_head_normal.png" alt=""/> <a href="#">Username</a> <div class="showme"> <ul id="userNavigationSubMenu"> <li>test</li> <li>test</li> <li>test</li> <li>test