list-comprehension

Python: Referring to a list comprehension in the list comprehension itself?

╄→гoц情女王★ 提交于 2020-12-16 07:19:47
问题 This thought just came to my mind. Say for whatever reason you wanted to get the unique elements of a list via a list comprehension in Python. [i if i in {created_comprehension} else 0 for i in [1, 2, 1, 2, 3] [1, 2, 0, 0, 3] I dunno, I don't really have a purpose for this but it'd be cool if it was possible to refer to the comprehension as it's being created. (e.g. How to remove duplicate items from a list using list comprehension? is a similar question) 回答1: Disclaimer: this is purely

Checking for all Elements in a Set in Haskell using syntactic sugar

回眸只為那壹抹淺笑 提交于 2020-12-13 05:50:11
问题 I try to remove the Integer duplicates of a List of (String, Int) , where I am guaranteed that there is no String duplicate. Is it possible to evaluate something like this in Haskell: I tried: [(a,b) | (a,b) <- bs, (c,k) <- bs, ((k == b) <= (a == c))] but this does not yet work. Edit: I am well aware, that you can achieve that using more complex syntax. For example by recursively searching the List for each elements duplicates... 回答1: (NB: this is a completely new version of this answer.

Checking for all Elements in a Set in Haskell using syntactic sugar

白昼怎懂夜的黑 提交于 2020-12-13 05:48:03
问题 I try to remove the Integer duplicates of a List of (String, Int) , where I am guaranteed that there is no String duplicate. Is it possible to evaluate something like this in Haskell: I tried: [(a,b) | (a,b) <- bs, (c,k) <- bs, ((k == b) <= (a == c))] but this does not yet work. Edit: I am well aware, that you can achieve that using more complex syntax. For example by recursively searching the List for each elements duplicates... 回答1: (NB: this is a completely new version of this answer.

Using 'while' loops in a list comprehension

非 Y 不嫁゛ 提交于 2020-12-08 06:07:49
问题 Say I have a function: x=[] i=5 while i<=20: x.append(i) i=i+10 return x Is there a way to convert it to a list comprehension like this? newList = [i=05 while i<=20 i=i+10] I get a syntax error. 回答1: You don't need a list comprehension for that. range will just do: list(range(5, 21, 10)) # [5, 15] A while loop is not possible inside of a list comprehension. Instead, you could do something like this: def your_while_generator(): i = 5 while i <= 20: yield i i += 10 [i for i in your_while

Using 'while' loops in a list comprehension

你说的曾经没有我的故事 提交于 2020-12-08 06:07:36
问题 Say I have a function: x=[] i=5 while i<=20: x.append(i) i=i+10 return x Is there a way to convert it to a list comprehension like this? newList = [i=05 while i<=20 i=i+10] I get a syntax error. 回答1: You don't need a list comprehension for that. range will just do: list(range(5, 21, 10)) # [5, 15] A while loop is not possible inside of a list comprehension. Instead, you could do something like this: def your_while_generator(): i = 5 while i <= 20: yield i i += 10 [i for i in your_while

Compare two lists to find date and time overlaps in Elixir

本秂侑毒 提交于 2020-12-02 00:20:42
问题 As part of some code that runs a booking system, we have a list of time_slots , which are tuples containing {start_time, end_time} . These are the available time slots that can be booked: time_slots = [ {~T[09:00:00], ~T[13:00:00]}, {~T[09:00:00], ~T[17:00:00]}, {~T[09:00:00], ~T[21:00:00]}, {~T[13:00:00], ~T[17:00:00]}, {~T[13:00:00], ~T[21:00:00]}, {~T[17:00:00], ~T[21:00:00]} ] Then we also have a list of bookings, which contains lists of tuples containing each {booking_start, booking_end}

Compare two lists to find date and time overlaps in Elixir

╄→гoц情女王★ 提交于 2020-12-02 00:18:37
问题 As part of some code that runs a booking system, we have a list of time_slots , which are tuples containing {start_time, end_time} . These are the available time slots that can be booked: time_slots = [ {~T[09:00:00], ~T[13:00:00]}, {~T[09:00:00], ~T[17:00:00]}, {~T[09:00:00], ~T[21:00:00]}, {~T[13:00:00], ~T[17:00:00]}, {~T[13:00:00], ~T[21:00:00]}, {~T[17:00:00], ~T[21:00:00]} ] Then we also have a list of bookings, which contains lists of tuples containing each {booking_start, booking_end}

Compare two lists to find date and time overlaps in Elixir

假如想象 提交于 2020-12-02 00:12:45
问题 As part of some code that runs a booking system, we have a list of time_slots , which are tuples containing {start_time, end_time} . These are the available time slots that can be booked: time_slots = [ {~T[09:00:00], ~T[13:00:00]}, {~T[09:00:00], ~T[17:00:00]}, {~T[09:00:00], ~T[21:00:00]}, {~T[13:00:00], ~T[17:00:00]}, {~T[13:00:00], ~T[21:00:00]}, {~T[17:00:00], ~T[21:00:00]} ] Then we also have a list of bookings, which contains lists of tuples containing each {booking_start, booking_end}

Compare two lists to find date and time overlaps in Elixir

会有一股神秘感。 提交于 2020-12-02 00:12:36
问题 As part of some code that runs a booking system, we have a list of time_slots , which are tuples containing {start_time, end_time} . These are the available time slots that can be booked: time_slots = [ {~T[09:00:00], ~T[13:00:00]}, {~T[09:00:00], ~T[17:00:00]}, {~T[09:00:00], ~T[21:00:00]}, {~T[13:00:00], ~T[17:00:00]}, {~T[13:00:00], ~T[21:00:00]}, {~T[17:00:00], ~T[21:00:00]} ] Then we also have a list of bookings, which contains lists of tuples containing each {booking_start, booking_end}

Compare two lists to find date and time overlaps in Elixir

帅比萌擦擦* 提交于 2020-12-02 00:07:54
问题 As part of some code that runs a booking system, we have a list of time_slots , which are tuples containing {start_time, end_time} . These are the available time slots that can be booked: time_slots = [ {~T[09:00:00], ~T[13:00:00]}, {~T[09:00:00], ~T[17:00:00]}, {~T[09:00:00], ~T[21:00:00]}, {~T[13:00:00], ~T[17:00:00]}, {~T[13:00:00], ~T[21:00:00]}, {~T[17:00:00], ~T[21:00:00]} ] Then we also have a list of bookings, which contains lists of tuples containing each {booking_start, booking_end}