list-comprehension

f-string with float formatting in list-comprehension

£可爱£侵袭症+ 提交于 2020-01-24 08:59:25
问题 The [f'str'] for string formatting was recently introduced in python 3.6. link. I'm trying to compare the .format() and f'{expr} methods. f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... ' Below is a list comprehension that converts Fahrenheit to Celsius. Using the .format() method it prints the results as float to two decimal points and adds the string Celsius: Fahrenheit = [32, 60, 102] F_to_C = ['{:.2f} Celsius'.format((x - 32) * (5/9)) for x in

Create a list of tuples with adjacent list elements if a condition is true

不想你离开。 提交于 2020-01-22 17:02:27
问题 I am trying to create a list of tuples where the tuple contents are the number 9 and the number before it in the list. Input List: myList = [1, 8, 9, 2, 4, 9, 6, 7, 9, 8] Desired Output: sets = [(8, 9), (4, 9), (7, 9)] Code: sets = [list(zip(myList[i:i], myList[-1:])) for i in myList if i==9] Current Result: [[], [], []] 回答1: Cleaner Pythonic approach: >>> [(x,y) for x,y in zip(myList, myList[1:]) if y == 9] [(8, 9), (4, 9), (7, 9)] What is the code above doing: zip(some_list, some_list[1:])

List comprehension: making lists of lists

泪湿孤枕 提交于 2020-01-22 14:07:56
问题 hi im trying to make a function in haskell that takes a number a makes a partion of it using lists i.e. for number 4 it would create [[1,1,1,1],[1,1,2],[1,3],[2,2],[4]] . I was thinking of using list comprehension for this where it would create list x and then create further lists using numbers from [1...n] (n being the partition number I would want) where the sum of the list created would be equal to n. The code I have created so far is- partions (n:xs) = [[x|x<-[1...n], sum[x]==n]]|xs<-[1..

List comprehension: making lists of lists

淺唱寂寞╮ 提交于 2020-01-22 14:07:05
问题 hi im trying to make a function in haskell that takes a number a makes a partion of it using lists i.e. for number 4 it would create [[1,1,1,1],[1,1,2],[1,3],[2,2],[4]] . I was thinking of using list comprehension for this where it would create list x and then create further lists using numbers from [1...n] (n being the partition number I would want) where the sum of the list created would be equal to n. The code I have created so far is- partions (n:xs) = [[x|x<-[1...n], sum[x]==n]]|xs<-[1..

Remove the first N items that match a condition in a Python list

强颜欢笑 提交于 2020-01-22 04:31:22
问题 If I have a function matchCondition(x) , how can I remove the first n items in a Python list that match that condition? One solution is to iterate over each item, mark it for deletion (e.g., by setting it to None ), and then filter the list with a comprehension. This requires iterating over the list twice and mutates the data. Is there a more idiomatic or efficient way to do this? n = 3 def condition(x): return x < 5 data = [1, 10, 2, 9, 3, 8, 4, 7] out = do_remove(data, n, condition) print

Replacing list item with contents of another list

巧了我就是萌 提交于 2020-01-20 08:30:08
问题 Similar to this question, but instead of replacing one item with another, I'd like to replace any occurrences of one item with the contents of a list. orig = [ 'a', 'b', 'c', 'd', 'c' ] repl = [ 'x', 'y', 'z' ] desired = [ 'a', 'b', 'x', 'y', 'z', 'd', 'x', 'y', 'z' ] # these are all incorrect, or fail to compile [ repl if x == 'c' else x for x in orig ] [ [a for a in orig] if x == 'c' else x for x in orig ] [ (a for a in orig) if x == 'c' else x for x in orig ] [ a for a in orig if x == 'c'

How do I efficiently filter computed values within a Python list comprehension?

ⅰ亾dé卋堺 提交于 2020-01-20 02:31:13
问题 The Python list comprehension syntax makes it easy to filter values within a comprehension. For example: result = [x**2 for x in mylist if type(x) is int] Will return a list of the squares of integers in mylist. However, what if the test involves some (costly) computation and you want to filter on the result? One option is: result = [expensive(x) for x in mylist if expensive(x)] This will result in a list of non-"false" expensive(x) values, however expensive() is called twice for each x. Is

Does this benchmark seem relevant?

萝らか妹 提交于 2020-01-16 18:41:18
问题 I am trying to benchmark a few method of itertools against generators and list comprehensions. The idea is that I want to build an iterator by filtering some entries from a base list. Here is the code I came up with(Edited after accepted answer): from itertools import ifilter import collections import random import os from timeit import Timer os.system('cls') # define large arrays listArrays = [xrange(100), xrange(1000), xrange(10000), xrange(100000)] #Number of element to be filtered out nb

Does this benchmark seem relevant?

自古美人都是妖i 提交于 2020-01-16 18:39:44
问题 I am trying to benchmark a few method of itertools against generators and list comprehensions. The idea is that I want to build an iterator by filtering some entries from a base list. Here is the code I came up with(Edited after accepted answer): from itertools import ifilter import collections import random import os from timeit import Timer os.system('cls') # define large arrays listArrays = [xrange(100), xrange(1000), xrange(10000), xrange(100000)] #Number of element to be filtered out nb

Find Max Frequency for every Sequence_ID

无人久伴 提交于 2020-01-16 09:49:11
问题 I have a Dataframe Like: Time Frq_1 Seq_1 Frq_2 Seq_2 Frq_3 Seq_3 12:43:04 - 30,668 - 30,670 4,620 30,671 12:46:05 - 30,699 - 30,699 3,280 30,700 12:46:17 4,200 30,700 - 30,704 - 30,704 12:46:18 3,060 30,700 4,200 30,700 - 30,700 12:46:18 3,060 30,700 4,200 30,700 - 30,700 12:46:19 3,060 30,700 4,220 30,700 - 30,700 12:46:20 3,060 30,700 4,240 30,700 - 30,700 12:46:37 - 30,698 - 30,699 3,060 30,700 12:46:38 - 30,699 3,060 30,700 4,600 30,700 12:47:19 - 30,668 - 30,669 - 30,669 12:47:20 - 30