list-comprehension

How to use regex re.compile Match() or findall() in list comprehension

自闭症网瘾萝莉.ら 提交于 2020-07-09 12:04:17
问题 I am trying to use regex in list comprehension without needing to use the pandas extract() functions. I want to use regex because my code might need to change where I need to use more complex pattern matching. A kind user here suggested I use the str accessor functions but again it mainly works because the current pattern is simple enough. As of now, I need to return pandas rows that either contain nan or whose values under ODFS_FILE_CREATE_DATETIME are not 10 string numbers i.e.: does not

Python Tuple to Dict, with additional list of keys

风流意气都作罢 提交于 2020-07-05 06:57:37
问题 So I have this array of tuples: [(u'030944', u'20091123', 10, 30, 0), (u'030944', u'20100226', 10, 15, 0)] And I have this list of field names: ['id', 'date', 'hour', 'minute', 'interval'] I would like to, in one fell swoop if possible, to convert the list of tuples to a dict: [{ 'id': u'030944', 'date': u'20091123', 'hour': 10, 'min': 30, 'interval': 0, },{ 'id': u'030944', 'date': u'20100226', 'hour': 10, 'min': 15, 'interval': 0, }] 回答1: data = [(u'030944', u'20091123', 10, 30, 0), (u

Python Tuple to Dict, with additional list of keys

橙三吉。 提交于 2020-07-05 06:57:16
问题 So I have this array of tuples: [(u'030944', u'20091123', 10, 30, 0), (u'030944', u'20100226', 10, 15, 0)] And I have this list of field names: ['id', 'date', 'hour', 'minute', 'interval'] I would like to, in one fell swoop if possible, to convert the list of tuples to a dict: [{ 'id': u'030944', 'date': u'20091123', 'hour': 10, 'min': 30, 'interval': 0, },{ 'id': u'030944', 'date': u'20100226', 'hour': 10, 'min': 15, 'interval': 0, }] 回答1: data = [(u'030944', u'20091123', 10, 30, 0), (u

Python: Intertwining two lists

跟風遠走 提交于 2020-07-02 11:45:26
问题 What is the pythonic way of doing the following: I have two lists a and b of the same length n , and I want to form the list c = [a[0], b[0], a[1], b[1], ..., a[n-1], b[n-1]] 回答1: c = [item for pair in zip(a, b) for item in pair] Read documentation about zip. For comparison with Ignacio's answer see this question: How do I convert a tuple of tuples to a one-dimensional list using list comprehension? 回答2: c = list(itertools.chain.from_iterable(itertools.izip(a, b))) 回答3: c = [item for t in zip

Efficient solution to find list indices greater than elements in a second list

最后都变了- 提交于 2020-06-29 04:31:32
问题 This question is linked to this: First Python list index greater than x? I have a (sorted) list of floats, and I want to find the first index that exceeds each value of a second list e.g. l=[0.2,0.3,0.7,0.9] m=[0.25,0.6] if m were a float I would use this: bisect.bisect_left(l,m) But for the case where m is a list this fails, and I can only think to employ a list comprehension: [bisect.bisect_left(l,i) for i in m] which gives: [1, 2] which works, but I want to speed it up for large lists in

List comprehension with duplicated function call [duplicate]

血红的双手。 提交于 2020-06-24 11:23:10
问题 This question already has answers here : Python list comprehension - want to avoid repeated evaluation (12 answers) Closed 2 years ago . I want to transform a string such as following: ' 1 , 2 , , , 3 ' into a list of non-empty elements: ['1', '2', '3'] My solution is this list comprehension: print [el.strip() for el in mystring.split(",") if el.strip()] Just wonder, is there a nice, pythonic way to write this comprehension without calling el.strip() twice? 回答1: You can use a generator inside

List comprehension with duplicated function call [duplicate]

 ̄綄美尐妖づ 提交于 2020-06-24 11:23:05
问题 This question already has answers here : Python list comprehension - want to avoid repeated evaluation (12 answers) Closed 2 years ago . I want to transform a string such as following: ' 1 , 2 , , , 3 ' into a list of non-empty elements: ['1', '2', '3'] My solution is this list comprehension: print [el.strip() for el in mystring.split(",") if el.strip()] Just wonder, is there a nice, pythonic way to write this comprehension without calling el.strip() twice? 回答1: You can use a generator inside

Removing words from list in python

淺唱寂寞╮ 提交于 2020-06-16 17:01:32
问题 I have a list 'abc' (strings) and I am trying to remove some words present in list 'stop' from the list 'abc' and all the digits present in abc. abc=[ 'issues in performance 421', 'how are you doing', 'hey my name is abc, 143 what is your name', 'attention pleased', 'compliance installed 234'] stop=['attention', 'installed'] I am using list comprehension to remove it but this below code is not able to remove that word. new_word=[word for word in abc if word not in stop ] Result:(attention

Efficiently remove duplicates, order-independent, from list of unordered sets

为君一笑 提交于 2020-06-14 07:35:42
问题 The following list has duplicated sublists. However, they are in different order: l1 = [['The', 'quick', 'brown', 'fox'], ['hi', 'there'], ['jumps', 'over', 'the', 'lazy', 'dog'], ['there', 'hi'], ['jumps', 'dog', 'over','lazy', 'the']] How can I remove them in order to get: l1 = [['The', 'quick', 'brown', 'fox'], ['hi', 'there'], ['jumps', 'over', 'the', 'lazy', 'dog']] I tried to: [list(i) for i in set(map(tuple, l1))] Nevertheless, I do not know if this is the fastest way of doing it for

Memory efficient way for list comprehension of pandas dataframe using multiple columns

我的未来我决定 提交于 2020-06-09 04:06:39
问题 I want to run a function on rows of a pandas dataframe in list comprehension. Dataframe can have varying number of columns. How to make use these columns of dataframe? import pandas as pd df = {'chrom': ['chr1', 'chr1','chr1'], 'start': [10000, 10100, 12000], 'end':[10150,10120,12250], 'S1':[1, 1, 1],'S2':[2, 2, 2],'S3':[3, 3, 3] } df = pd.DataFrame(data=df) print(df) def func(row): print(row) [func(row) for row in zip(df['chrom'],df['start'],df['S1'],df['S2'],df['S3'])] How to do this in a