nested-lists

Adding data to a nested list in Python

此生再无相见时 提交于 2019-12-18 04:20:33
问题 I have a nested list e.g.: nlist = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ] Before I insert this list into a database, I would like to add a "column" to it with the same value in each row of the new column e.g: nlist = [ [a, 1, 2, 3], [a, 4, 5, 6], [a, 7, 8, 9], ] What's the best way to do this, when, for example, the original nested list might have hundreds of rows? 回答1: Why not change the original list (if that is all you want to do): for row in nlist: row.insert(0, a) 回答2: If you are looking

Repeat a list within a list X number of times

♀尐吖头ヾ 提交于 2019-12-17 21:14:20
问题 I'm working on a project and I need to repeat a list within a list a certain number of times. Obviously, L.append(L) just adds the elements again without creating separate lists. I'm just stumped on how to make the lists separate within the big list. In short form, this is what I have: L = [1,2,3,4,5] If I wanted to to repeat it, say, 3 times so I'd have: L = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]] How do I achieve this? I'm looking for lists within the big list. 回答1: No need for any functions:

Dead simple Collapsable List Function for deep and shallow nested UL/LI lists (JQuery)

邮差的信 提交于 2019-12-17 17:00:54
问题 I waded through a TON of terrible js "solutions" for how to simply make collapsible nested list(s). This is what I eventually came up with. I'm posting here in the hopes the next guy won't have to deal with all the rufuse out there. Otherwise feel free to add your own! maybe jquery free method if you can manage it? list must be in "proper" format to work 回答1: css: ul>li>ul { display: none; } js/jquery $('li').click(function(e){ e.stopPropagation(); if(this.getElementsByTagName("ul")[0].style

Nested numbering to array keys

此生再无相见时 提交于 2019-12-17 16:53:42
问题 I need to convert following data in csv to nested tree S.No Name 1 A 1.1 B 1.1.1 C 1.1.2 D 2 E 2.1 F 2.2 G Is there any way S.No can be used to make array keys like 1.1.1 to $test[1][1][1] and then I can store corresponding Name as value. or I should make parent child type array? What would be the best approach to convert this to tree/nested list? 回答1: You can use this function to set a nested value within an array: function set_nested_value(array &$array, $index, $value) { $node = &$array;

How do I parse a string representing a nested list into an actual list? [duplicate]

本小妞迷上赌 提交于 2019-12-17 16:44:36
问题 This question already has answers here : Convert string representation of list to list (16 answers) Closed 6 years ago . Say I have a string representing some nested lists and I want to convert it into the real thing. I could do this, I think: exec "myList = ['foo', ['cat', ['ant', 'bee'], 'dog'], 'bar', 'baz']" But in an environment where users might be supplying the string to execute this could/would be a bad idea. Does anybody have any ideas for a tidy parser that would accomplish the same

Split a list into nested lists on a value

时间秒杀一切 提交于 2019-12-17 03:41:48
问题 Say I have a list like so: [1, 4, None, 6, 9, None, 3, 9, 4 ] I decide to split this into nested lists on None , to get this: [ [ 1, 4 ], [ 6, 9 ], [ 3, 9, 4 ] ] Of course, I could have wanted to do this on (9, None) in which case, we would have got: [ [ 1, 4 ], [ 6 ], [ 3 ], [ 4 ] ] This is trivial to do using list append through iteration ( in a for loop ) I am interested to know whether this can be done in something faster - like a list comprehension? If not, why not ( for example, a list

Working across sub-lists with apply() functions

﹥>﹥吖頭↗ 提交于 2019-12-14 03:22:48
问题 I am trying to the bootstrap the proportional occurrence of diet items for 7 individuals and calculate a sd() Lets say there are 9 prey items on the menu. Diet <- c("Beaver","Bird", "Bobcat","Coyote", "Deer", "Elk", "Porcupine", "Raccoon", "SmMamm") And that these prey items are eaten by 7 different individuals of the same species Inds <- c("P01", "P02", "P03", "P04", "P05", "P06", "P07") My goal is the bootstrap the proportional occurrence of each diet item for each individual. The loop

CSS Nested lists items and alternate background

萝らか妹 提交于 2019-12-13 21:14:00
问题 I am searching for a way to have list items have alternating background colors. When there is a nested list the items keep alternating but the child is indented without having the background color of the parent flow down to its nested children. It is not possible to apply classes. Also the amount of items is variable. Preferably it should work for an infinite amount of nested lists. But if that is not possible a cap on 3 depths (as in picture) should be enough. If it is easier to do by using

Recursively counting occurrences in a nested list of numbers

假装没事ソ 提交于 2019-12-13 16:26:53
问题 I'm finally getting around to recursion in Python and trying to count the number of occurrences of a target number in a list . However, I'm running into issues with counting occurrences in a nested list of numbers. For example def count(lst, target): if lst == []: return 0 if lst[0] == target: return 1 + count(lst[1:], target) else: return 0 + count(lst[1:], target) Output >>> count( [1,2,3,[4,5,5],[[5,2,1],4,5],[3]], 1 ) Output: 1 Expected output: 2 Is there an easy way to flatten nested

Nested list doesn't work properly

心已入冬 提交于 2019-12-13 11:16:34
问题 import re def get_number(element): re_number = re.match("(\d+\.?\d*)", element) if re_number: return float(re_number.group(1)) else: return 1.0 def getvalues(equation): elements = re.findall("([a-z0-9.]+)", equation) return [get_number(element) for element in elements] eqn = [] eqn_no = int(raw_input("Enter the number of equations: ")) for i in range(eqn_no): eqn.append(getvalues(str(raw_input("Enter Equation %d: " % (i+1))))) print "Main Matrix: " for i in range((eqn_no)): for j in range(