nested-lists

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

跟風遠走 提交于 2019-11-28 01:01:52
This question already has an answer here: Convert string representation of list to list 16 answers 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 thing? >>> import ast >>> mylist = ast.literal_eval("['foo', ['cat', ['ant', 'bee'], 'dog'], 'bar', 'baz']") >>> mylist [

“Deep copy” nested list without using the deepcopy function

风格不统一 提交于 2019-11-28 00:21:47
I am trying to copy the nested list a , but do not know how to do it without using the copy.deepcopy function. a = [[1, 2], [3, 4]] I used: b = a[:] and b = a[:][:] But they all turn out to be shallow copy. Any hints? My entry to simulate copy.deepcopy : def deepcopy(obj): if isinstance(obj, dict): return {deepcopy(key): deepcopy(value) for key, value in obj.items()} if hasattr(obj, '__iter__'): return type(obj)(deepcopy(item) for item in obj) return obj The strategy: iterate across each element of the passed-in object, recursively descending into elements that are also iterable and making new

How to create a number of empty nested lists in python [duplicate]

放肆的年华 提交于 2019-11-27 21:40:52
This question already has an answer here: List of lists changes reflected across sublists unexpectedly 13 answers I want to have a variable that is a nested list of a number of empty lists that I can fill in later. Something that looks like: my_variable=[[], [], [], []] However, I do not know beforehand how many lists I will need, only at the creation step, therefore I need a variable a to determine it. I thought about simple my_variable=[[]]*a , but that creates copies of lists and it is not what I want to have. I could do: my_variable=[] for x in range(a): my_variable.append([]) but I'm

How to convert nested list of lists into a list of tuples in python 3.3?

强颜欢笑 提交于 2019-11-27 21:09:44
I am trying to convert a nested list of lists into a list of tuples in Python 3.3. However, it seems that I don't have the logic to do that. The input looks as below: >>> nested_lst = [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']] And the desired ouptput should look as exactly as follows: nested_lst_of_tuples = [('tom', 'cat'), ('jerry', 'mouse'), ('spark', 'dog')] Just use a list comprehension: nested_lst_of_tuples = [tuple(l) for l in nested_lst] Demo: >>> nested_lst = [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']] >>> [tuple(l) for l in nested_lst] [('tom', 'cat'), ('jerry',

How does the min/max function on a nested list work?

元气小坏坏 提交于 2019-11-27 20:29:46
Lets say, there is a nested list, like: my_list = [[1, 2, 21], [1, 3], [1, 2]] When the function min() is called on this: min(my_list) The output received is [1, 2] Why and How does it work? What are some use cases of it? Bhargav Rao How are lists and other sequences compared in Python? Lists (and other sequences) in Python are compared lexicographically and not based on any other parameter. Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the

is there a way to pass nested initializer lists in C++11 to construct a 2D matrix?

十年热恋 提交于 2019-11-27 17:29:20
问题 Imagine you have a simple matrix class template <typename T = double> class Matrix { T* data; size_t row, col; public: Matrix(size_t m, size_t n) : row(m), col(n), data(new T[m*n]) {} //... friend std::ostream& operator<<(std::ostream& os, const Matrix& m) { for (int i=0; i<m.row; ++i) { for (int j=0; j<m.col; ++j) os<<" "<<m.data[i + j*m.row]; os<<endl; } return os; } }; Is there a way that I can initialize this matrix with an initializer list? I mean to obtain the sizes of the matrix and

How to use <ui:repeat> to iterate over a nested list?

こ雲淡風輕ζ 提交于 2019-11-27 16:05:52
Using JSF 2.0, I need to display a table wherein each row contains a link which opens a popup. I have two models: A which has id and List<B> properties and B which has id and name properties. In my backing bean, I have a List<A> property. In my view, I am using <ui:repeat> to iterate over List<A> . The requirement is, depending on the row that the user clicks, the corresponding List<B> of A needs to be displayed. However, the <ui:repeat> does not accept a nested list to be assigned in the var attribute. Hence, I need to do a lot of workarounds which is not efficient. How do I efficiently solve

Python: Return 2 ints for index in 2D lists given item

一曲冷凌霜 提交于 2019-11-27 16:03:41
I've been tinkering in python this week and I got stuck on something. If I had a 2D list like this: myList = [[1,2],[3,4],[5,6]] and I did this >>>myList.index([3,4]) it would return 1 However, I want the index of something in side one of the lists, like this >>>myList.index(3) and it would return 1, 0 Is there anything that can do this? Cheers Try this: def index_2d(myList, v): for i, x in enumerate(myList): if v in x: return (i, x.index(v)) Usage: >>> index_2d(myList, 3) (1, 0) If you are doing many lookups you could create a mapping. >>> myList = [[1,2],[3,4],[5,6]] >>> d = dict( (j,(x, y))

Print a nested list line by line - Python

﹥>﹥吖頭↗ 提交于 2019-11-27 15:36:21
问题 A = [[1, 2, 3], [2, 3, 4], [4, 5, 6]] I am trying my best to print A of the form: 1 2 3 2 3 4 4 5 6 That is in different lines, but I am unable to do so without all the elements in different lines. This is my code so far: for r in A: for t in r: print(t,) print This is my output: 1 2 3 2 3 4 4 5 6 It seems really simple, and I think a minor change would do it. Thanks! 回答1: Use a simple for loop and " ".join() mapping each int in the nested list to a str with map() . Example: >>> ys = [[1, 2,

Most elegant way to modify elements of nested lists in place

为君一笑 提交于 2019-11-27 12:03:51
问题 I have a 2D list that looks like this: table = [['donkey', '2', '1', '0'], ['goat', '5', '3', '2']] I want to change the last three elements to integers, but the code below feels very ugly: for row in table: for i in range(len(row)-1): row[i+1] = int(row[i+1]) But I'd rather have something that looks like: for row in table: for col in row[1:]: col = int(col) I think there should be a way to write the code above, but the slice creates an iterator/new list that's separate from the original, so