如何在Python中用逗号过滤列表并将值附加到动态变量
我有一个从数据库检索的列表,它有两个值:
list = [('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}'), ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)]
如您所见,两个不同的值用 ),
处的逗号区分。那么我如何分离这两个值并插入到每个列表值的变量中?
例如(预期输出):
variableA = ('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}')
variableB = ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)
如果数据库有第三个值,则变量将动态添加。例如,变量C将存储第三个值,如果有第四个值,则生成变量D。
I have a list that i retrieve from database and it has two value:
list = [('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}'), ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)]
As you can see, the two different value is differentiate with the comma at ),
. So how can i seperate these two value and insert to a variable for each list value?
For example (expected output):
variableA = ('[email protected]', '[email protected]\n', '{ "header1": "Subject", "header2": "Text"}', ' { "condition1": "Equal", "condition2": "Contain"}', '{ "parameter1": "hi1", "parameter2": "hi2" || "testNested"}', '{ "subjectP1": "WorkedDynamicWord","wordP1": "hi1","subjectP2": "WorkedDynamicWord2", "wordP2": "Dynamic word"}')
variableB = ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)
The variable will be add dynamically if the database has third value. For example, variableC will store the third value and variableD will be generate if there is fourth value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样创建变量
You can create variables as such
Python 的 globals() 函数返回一个包含当前全局符号表的字典,因此您可以添加具有自定义名称的变量:
输出为:
如果您更喜欢使用字母(variableA、variableB、...),请替换
str (iVariable+1)
bychr(ord('@')+iVariable+1)
会将数字转换为相应的字母字符。注意:不建议创建此类变量,您可以重新考虑是否需要它,请参阅以下链接了解更多详细信息和替代方案:
如何创建可变变量?
Python’s globals() function returns a dictionary containing the current global symbol table, so you can add your variables with custom names:
The output is:
If you prefer to have letters (variableA, variableB, ...), replace
str(iVariable+1)
bychr(ord('@')+iVariable+1)
that will convert the number to corresponding alphabetic character.Note: creating such variables is not recommended and you may reconsider your need for it, see the following link for more details and alternatives:
How do I create variable variables?