带有 If Else 的 Python 嵌套列表理解 [英] Python Nested List Comprehension with If Else
问题描述
我试图使用列表理解来替换值列表中的多个可能的字符串值.
I was trying to use a list comprehension to replace multiple possible string values in a list of values.
我有一个列名列表,这些列名取自 cursor.description
;
I have a list of column names which are taken from a cursor.description
;
['UNIX_Time', 'col1_MCA', 'col2_MCA', 'col3_MCA', 'col1_MCB', 'col2_MCB', 'col3_MCB']
然后我有 header_replace
;
{'MCB': 'SourceA', 'MCA': 'SourceB'}
我想用这些值替换在列名中找到的 header_replace.keys()
的字符串值.
I would like to replace the string values for header_replace.keys()
found within the column names with the values.
我不得不使用以下循环;
I have had to use the following loop;
headers = []
for header in cursor.description:
replaced = False
for key in header_replace.keys():
if key in header[0]:
headers.append(str.replace(header[0], key, header_replace[key]))
replaced = True
break
if not replaced:
headers.append(header[0])
哪个给我正确的输出;
Which gives me the correct output;
['UNIX_Time', 'col1_SourceA', 'col2_SourceA', 'col3_SourceA', 'col1_SourceB', 'col2_SourceB', 'col3_SourceB']
我尝试使用此列表理解;
I tried using this list comprehension;
[str.replace(i[0],k,header_replace[k]) if k in i[0] else i[0] for k in header_replace.keys() for i in cursor.description]
但这意味着对于不匹配的密钥重复了一些项目,我会得到;
But it meant that items were duplicated for the unmatched keys and I would get;
['UNIX_Time', 'col1_MCA', 'col2_MCA', 'col3_MCA', 'col1_SourceA', 'col2_SourceA', 'col3_SourceA',
'UNIX_Time', 'col1_SourceB', 'col2_SourceB', 'col3_SourceB', 'col1_MCB', 'col2_MCB', 'col3_MCB']
但是,如果我使用;
[str.replace(i[0],k,header_replace[k]) for k in header_replace.keys() for i in cursor.description if k in i[0]]
@Bakuriu 固定语法
我会得到正确的替换,但然后放掉不需要替换字符串的所有项目.
I would get the correct replacement but then loose any items that didn't need to have an string replacement.
['col1_SourceA', 'col2_SourceA', 'col3_SourceA', 'col1_SourceB', 'col2_SourceB', 'col3_SourceB']
是否有执行此操作的pythonesque方式?我是否在扩展列表理解范围?我当然觉得它们很难阅读.
Is there a pythonesque way of doing this or am I over stretching list comprehensions? I certainly find them hard to read.
推荐答案
[str.replace(i[0],k,header_replace[k]) if k in i[0] for k in header_replace.keys() for i in cursor.description]
这是一个 SyntaxError
,因为 if
表达式必须包含 else
部分.你可能的意思是:
this is a SyntaxError
, because if
expressions must contain the else
part. You probably meant:
[i[0].replace(k, header_replace[k]) for k in header_replace for i in cursor.description if k in i[0]]
以 if
结尾.但是我必须说,嵌套循环的列表理解通常不是要走的路.我将使用扩展的 for
循环.实际上,我会改进它以除去 replaced
标志:
With the if
at the end. However I must say that list-comprehension with nested loops aren't usually the way to go.
I would use the expanded for
loop. In fact I'd improve it removing the replaced
flag:
headers = []
for header in cursor.description:
for key, repl in header_replace.items():
if key in header[0]:
headers.append(header[0].replace(key, repl))
break
else:
headers.append(header[0])
for
循环的 else
在迭代过程中未触发任何 break
时执行.
The else
of the for
loop is executed when no break
is triggered during the iterations.
我不明白为什么在您的代码中使用 str.replace(字符串,子字符串,替换)
而不是 string.replace(子字符串,替换)
.字符串具有 instance 方法,因此就这样使用它们,而不是像它们是该类的静态方法一样.
I don't understand why in your code you use str.replace(string, substring, replacement)
instead of string.replace(substring, replacement)
. Strings have instance methods, so you them as such and not as if they were static methods of the class.
这篇关于带有 If Else 的 Python 嵌套列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!