list

How to search for an object in List<object> having its field value

☆樱花仙子☆ 提交于 2021-02-09 11:52:11
问题 The question is how to get an object from List having its field value I have got list called f_objects filled with objects. private List<F_object> f_objects; i've got also a string with some value : string name = "something"; F_object got a method returning its field called name: public string GetName() { return this.name; } Is there a built in method for compare objects in list vs this field value ? Or should i make a loop and compare like this: foreach(F_object ob in f_objects) { if String

return first non NaN value in python list

社会主义新天地 提交于 2021-02-08 19:57:54
问题 What would be the best way to return the first non nan value from this list? testList = [nan, nan, 5.5, 5.0, 5.0, 5.5, 6.0, 6.5] edit: nan is a float 回答1: If you're doing it a lot, put it into a function to make it readable and easy: import math t = [float('nan'), float('nan'), 5.5, 5.0, 5.0, 5.5, 6.0, 6.5] def firstNonNan(listfloats): for item in listfloats: if math.isnan(item) == False: return item firstNonNan(t) 5.5 回答2: You can use next, a generator expression, and math.isnan: >>> from

What exactly are tuples in Python?

我们两清 提交于 2021-02-08 15:40:37
问题 I'm following a couple of Pythone exercises and I'm stumped at this one. # C. sort_last # Given a list of non-empty tuples, return a list sorted in increasing # order by the last element in each tuple. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use a custom key= function to extract the last element form each tuple. def sort_last(tuples): # +++your code here+++ return What is a Tuple? Do they mean a List of Lists? 回答1: A tuple and a list is

Iterate through two lists of different lengths

泄露秘密 提交于 2021-02-08 15:20:02
问题 I have the following two lists: nums = [1, 2, 3, 4, 5, 6, 7, 8] ltrs = ['a', 'b', 'c', 'd'] for x, y in nums, ltrs: print(x, y) With the following error c:\Python35\Scripts>python listtest.py Traceback (most recent call last): File "listtest.py", line 5, in <module> for x, y in nums, ltrs: ValueError: too many values to unpack (expected 2) I would like for the output to be: 1a, 2b, 3c, 4d, 5, 6, 7, 8 From what I have read the zip method will only work if the two lists are the same length,

Iterate through two lists of different lengths

依然范特西╮ 提交于 2021-02-08 15:19:21
问题 I have the following two lists: nums = [1, 2, 3, 4, 5, 6, 7, 8] ltrs = ['a', 'b', 'c', 'd'] for x, y in nums, ltrs: print(x, y) With the following error c:\Python35\Scripts>python listtest.py Traceback (most recent call last): File "listtest.py", line 5, in <module> for x, y in nums, ltrs: ValueError: too many values to unpack (expected 2) I would like for the output to be: 1a, 2b, 3c, 4d, 5, 6, 7, 8 From what I have read the zip method will only work if the two lists are the same length,

Walk through a list split function in Haskell

血红的双手。 提交于 2021-02-08 14:11:47
问题 This is a follow up to my previous question. I am trying to understand the list splitting example in Haskell from here: foldr (\a ~(x,y) -> (a:y,x)) ([],[]) I can read Haskell and know what foldr is but don't understand this code. Could you walk me through this code and explain it in more details ? 回答1: Let’s try running this function on a sample input list, say [1,2,3,4,5] : We start with foldr (\a ~(x,y) -> (a:y,x)) ([],[]) [1,2,3,4,5] . Here a is the first element of the list, and (x,y)

C# accuracy when checking float in List<float> with Contains method

点点圈 提交于 2021-02-08 13:46:14
问题 I have a list of float s and want to check if it already contains a particular value with the List.Contains() method. I know that for float equality tests you often can't use == but something like myFloat - value < 0.001 . My question is, does the Contains method account for this or I do I need to use a method that accounts for float precision errors for testing if the float is in the list? 回答1: From the docs for List(T).Contains: This method determines equality by using the default equality

How to get only specific field from the list

☆樱花仙子☆ 提交于 2021-02-08 13:34:06
问题 I have an IEnumerable of Lesson objects: IEnumerable<Lesson> filteredLessons I convert it to a List through the following method: ToList(); But I want the returned list to contain only the first property, lessonid , not all the Lesson properties. How can I get the data of specific property of the list instead of the objects? 回答1: You can select the value you want first, like this: filteredLessons.Select(l => l.lessonId).ToList(); And you'll get a list of ID's 回答2: If you want to get the the

Modify a large list without any loops in python

戏子无情 提交于 2021-02-08 13:14:17
问题 My list is: a=[1,2,3,4] Now I want my list to be: a=[-1,-2,-3,-4] How can I change my list this way without using any loops? Update: this may be a large list, on the order of 10000 elements. 回答1: Use Python's map functionality a[:] = map(lambda x: -x, a) Here's the description of the map function from the above link: map(function, iterable, ...) Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many

Modify a large list without any loops in python

旧时模样 提交于 2021-02-08 13:13:55
问题 My list is: a=[1,2,3,4] Now I want my list to be: a=[-1,-2,-3,-4] How can I change my list this way without using any loops? Update: this may be a large list, on the order of 10000 elements. 回答1: Use Python's map functionality a[:] = map(lambda x: -x, a) Here's the description of the map function from the above link: map(function, iterable, ...) Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many