如何在Python中用逗号过滤列表并将值附加到动态变量

发布于 2025-01-12 02:14:31 字数 2068 浏览 0 评论 0原文

我有一个从数据库检索的列表,它有两个值:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2025-01-19 02:14:31

您可以这样创建变量

data = [('[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)]

for i,l in enumerate(data):
    vars()[f'variable{i + 1}'] = l

print(variable1, variable2)

You can create variables as such

data = [('[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)]

for i,l in enumerate(data):
    vars()[f'variable{i + 1}'] = l

print(variable1, variable2)
内心荒芜 2025-01-19 02:14:31

Python 的 globals() 函数返回一个包含当前全局符号表的字典,因此您可以添加具有自定义名称的变量:

data = [('[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)]

for iVariable, variable in enumerate(data):
    globals()["variable"+str(iVariable+1)] = variable
    
print("Variable 1 : ", variable1)
print("Variable 2 : ",variable2)

输出为:

Variable 1 :  ('[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"}')
Variable 2 :  ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)

如果您更喜欢使用字母(variableA、variableB、...),请替换 str (iVariable+1) by chr(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:

data = [('[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)]

for iVariable, variable in enumerate(data):
    globals()["variable"+str(iVariable+1)] = variable
    
print("Variable 1 : ", variable1)
print("Variable 2 : ",variable2)

The output is:

Variable 1 :  ('[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"}')
Variable 2 :  ('[email protected]', '[email protected]\n', '{ "header1": "Subject"}', '{ "condition1": "Contain"}', '{ "parameter1": "haha"}', None)

If you prefer to have letters (variableA, variableB, ...), replace str(iVariable+1) by chr(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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文