empty-list

Is there an “Empty List” singleton in C#?

江枫思渺然 提交于 2019-12-02 18:40:43
In C# I use LINQ and IEnumerable a good bit. And all is well-and-good (or at least mostly so). However, in many cases I find myself that I need an empty IEnumerable<X> as a default. That is, I would like for (var x in xs) { ... } to work without needing a null-check. Now this is what I currently do, depending upon the larger context: var xs = f() ?? new X[0]; // when xs is assigned, sometimes for (var x in xs ?? new X[0]) { ... } // inline, sometimes Now, while the above is perfectly fine for me -- that is, if there is any "extra overhead" with creating the array object I just don't care -- I

modification of skipping empty list and continuing with function

只愿长相守 提交于 2019-12-02 10:18:14
Background The following code is slightly modified from skipping empty list and continuing with function import pandas as pd Names = [list(['Jon', 'Smith', 'jon', 'John']), list([]), list(['Bob', 'bobby', 'Bobs']), list([]), list([])] df = pd.DataFrame({'Text' : ['Jon J Smith is Here and jon John from ', 'get nothing from here', 'I like Bob and bobby and also Bobs diner ', 'nothing here too', 'same here' ], 'P_ID': [1,2,3, 4,5], 'P_Name' : Names }) #rearrange columns df = df[['Text', 'P_ID', 'P_Name']] df Text P_ID P_Name 0 Jon J Smith is Here and jon John from 1 [Jon, Smith, jon, John] 1 get

Selecting rows of pandas DataFrame where column is not empty list

六月ゝ 毕业季﹏ 提交于 2019-12-01 18:00:01
I have a pandas DataFrame where one column contains lists and would like to select the rows where the lists are not empty. Example data: df = pd.DataFrame({'letter': ["a", "b", "c", "d", "e"], 'my_list':[[0,1,2],[1,2],[],[],[0,1]]}) df letter my_list 0 a [0, 1, 2] 1 b [1, 2] 2 c [] 3 d [] 4 e [0, 1] What I'd like: df letter my_list 0 a [0, 1, 2] 1 b [1, 2] 4 e [0, 1] What I'm trying: df[df.my_list.map(lambda x: if len(x) !=0)] ... which returns an invalid syntax error. Any suggestions? Empty lists evaluate to False in a boolean context df[df.my_list.astype(bool)] letter my_list 0 a [0, 1, 2] 1

Android: set empty view to a list view

为君一笑 提交于 2019-11-30 05:05:16
<TextView android:id="@android:id/empty" style="@style/FacebookFont" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/no_result_found" android:textColor="@color/white" /> this is the xml code for empty view. I am setting empty view as follows: list.setAdapter(adapter); list.setEmptyView(findViewById(android.R.id.empty)); I have the items in the listview even then also before listview is populated the empty view is shown. I don't want that empty view to be shown when listview contains some item. Please help.

When does Python create new list objects for empty lists?

岁酱吖の 提交于 2019-11-29 03:38:35
The following makes sense to me: >>> [] is [] False Given that lists are mutable, I would expect [] to be a new empty list object every time it appears in an expression. Using this explanation however, the following surprises me: id([]) == id([]) True Why? What is the explanation? In the first example, [] is not [] precisely because the lists are mutable. If they weren't, they could safely map to the same one without issue. In the second example, id([]) creates a list, gets the id, and deallocates the list. The second time around it creates a list again , but "puts it in the same place"

Android: set empty view to a list view

走远了吗. 提交于 2019-11-29 02:27:22
问题 <TextView android:id="@android:id/empty" style="@style/FacebookFont" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/no_result_found" android:textColor="@color/white" /> this is the xml code for empty view. I am setting empty view as follows: list.setAdapter(adapter); list.setEmptyView(findViewById(android.R.id.empty)); I have the items in the listview even then also before listview is populated the empty view

How can I make multiple empty lists in python?

醉酒当歌 提交于 2019-11-27 01:06:48
问题 How can I make many empty lists without manually typing list1=[] , list2=[], list3=[] Is there a for loop that will make me 'n' number of such empty lists? 回答1: A list comprehension is easiest here: >>> n = 5 >>> lists = [[] for _ in range(n)] >>> lists [[], [], [], [], []] Be wary not to fall into the trap that is: >>> lists = [[]] * 5 >>> lists [[], [], [], [], []] >>> lists[0].append(1) >>> lists [[1], [1], [1], [1], [1]] 回答2: If you want to create different lists without a "list of lists"

Collections.emptyList() vs. new instance

别等时光非礼了梦想. 提交于 2019-11-26 18:10:24
In practice, is it better to return an empty list like this : return Collections.emptyList(); Or like this : return new ArrayList<Foo>(); Or is this completely dependent upon what you're going to do with the returned list? The main difference is that Collections.emptyList() returns an immutable list, i.e., a list to which you cannot add elements. (Same applies to the List.of() introduced in Java 9.) In the rare cases where you do want to modify the returned list, Collections.emptyList() and List.of() are thus not a good choices. I'd say that returning an immutable list is perfectly fine (and

Collections.emptyList() vs. new instance

你离开我真会死。 提交于 2019-11-26 05:59:10
问题 In practice, is it better to return an empty list like this: return Collections.emptyList(); Or like this: return new ArrayList<Foo>(); Or is this completely dependent upon what you\'re going to do with the returned list? 回答1: The main difference is that Collections.emptyList() returns an immutable list, i.e., a list to which you cannot add elements. (Same applies to the List.of() introduced in Java 9.) In the rare cases where you do want to modify the returned list, Collections.emptyList()