[英]Python list comprehension modify the elements based on string match
I have python code which return the list comprehension as shown below in return code.我有 python 代码,它返回列表理解,如下面的返回代码所示。
return [str(source.get(field)) for field in fields]
# one of the list output
['3', 'checkuser', '=checkuser', 'management', '', 'checkuser@gmail.com', '', 'True', "['apiusermanagement']", 'False']
Requirement要求
I need to check each element in list and if element first character is starting with any of ('=', '@', '|', '%') these characters then I need to append the single double quote (") in starting of that list element.我需要检查列表中的每个元素,如果元素的第一个字符以 ('=', '@', '|', '%') 这些字符中的任何一个开头,那么我需要 append 中的单双引号 (")该列表元素的开始。
For example, 3rd element '=checkuser' in example output started with '=' so this element should be modified as '"=checkuser' and same should be part of returned list output例如,示例 output 中的第 3 个元素 '=checkuser' 以 '=' 开头,因此该元素应修改为 '"=checkuser' 并且应该是返回列表 output 的一部分
# Expected output
['3', 'checkuser', '"=checkuser', 'management', '', 'checkuser@gmail.com', '', 'True', "['apiusermanagement']", 'False']
I am beginner in Python and try to achieve this with minimal changes & good performance output.我是 Python 的初学者,并尝试以最小的更改和良好的性能 output 来实现这一目标。 Any help or pointer would be much appreciated任何帮助或指针将不胜感激
One option would be to put this logic into a function:一种选择是将此逻辑放入 function 中:
def fetch_and_map(field):
result = str(source.get(field))
if result.startswith("="):
return '"' + result
else:
return result
return [fetch_and_map(field) for field in fields]
You could also do this inline as:你也可以这样内联:
return ['"' + str(source.get(field)) if str(source.get(field)).startswith("=") else str(source.get(field)) for field in fields]
Note that if source
is a dict of strings, you can make this simpler by skipping the str()
call:请注意,如果source
是字符串的字典,则可以通过跳过str()
调用来简化:
return ['"' + source.get(field) if source.get(field).startswith("=") else source.get(field) for field in fields]
You can use this code:您可以使用以下代码:
output = ['3', 'checkuser', '=checkuser', 'management', '', 'checkuser@gmail.com', '', 'True', "['apiusermanagement']", 'False']
output_modified = []
for string in output:
if string != "" and string[0] in ['=', '@', '|', '%']:
output_modified.append('"' + string)
else:
output_modified.append(string)
This will iterate over each string in output
.这将遍历output
中的每个字符串。 If the string is not empty and begins with '='
, '@'
, '|'
如果字符串不为空且以'='
、 '@'
、 '|'
开头, or '%'
, it will add '"'
to the beginning of string
and append it to output_modified
. If not, it will simply append it to output_modified
as-is. , 或'%'
,它将在string
的开头添加'"'
并将 append 添加到output_modified
。如果没有,它会简单地将 append 原样添加到output_modified
。
Put the list in a variable.将列表放入变量中。 Then loop over the variable, adding the double quote where needed.然后循环变量,在需要的地方添加双引号。
result = [str(source.get(field)) for field in fields]
for i, val in enumerate(result):
if len(val) > 0 and val[0] in ('=', '@', '|', '%'):
result[i] = '"' + val
return result
something like this像这样的东西
lst = ['3', 'checkuser', '=checkuser', 'management', '', 'checkuser@gmail.com', '', 'True', "['apiusermanagement']",
'False']
lst_ex = [f'"{x}' if x and x[0] in {'=', '@', '|', '%'} else x for x in lst]
print(lst_ex)
output output
['3', 'checkuser', '"=checkuser', 'management', '', 'checkuser@gmail.com', '', 'True', "['apiusermanagement']", 'False']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.