我已经使用python连接到SQL Server来执行查询。 我有一个显示以下示例的当前列表:
[('TEST', 'dbo', 'XXX', 'Practice', 1, 'char', 3, 3, None, None, 1, None, None, 1, None, 3, 1, 'YES', 39)
('TEST', 'dbo', 'XXX', 'ParPracticeCode', 1, 'char', 3, 3, None, None, 1, None, None, 1, None, 3, 2, 'YES', 39)
('TEST', 'dbo', 'XXX', 'PracOnlyName', 12, 'varchar', 100, 100, None, None, 1, None, None, 12, None, 100, 3, 'YES', 39)]
但是,我想拆分列表并通过仅包含第四个项目(例如Practice
,ParPracticeCode
和PracOnlyName
)来创建新列表。
变成new_list = [Practice, ParPracticeCode, PracOnlyName]
答案 0 :(得分:1)
sample = [('TEST', 'dbo', 'XXX', 'Practice', 1, 'char', 3, 3, None, None, 1, None, None, 1, None, 3, 1, 'YES', 39), ('TEST', 'dbo', 'XXX', 'ParPracticeCode', 1, 'char', 3, 3, None, None, 1, None, None, 1, None, 3, 2, 'YES', 39), ('TEST', 'dbo', 'XXX', 'PracOnlyName', 12, 'varchar', 100, 100, None, None, 1, None, None, 12, None, 100, 3, 'YES', 39)]
new_list = [x[3] for x in sample]
答案 1 :(得分:0)
x = [('TEST', 'dbo', 'XXX', 'Practice', 1, 'char', 3, 3, None, None, 1, None, None, 1, None, 3, 1, 'YES', 39),
('TEST', 'dbo', 'XXX', 'ParPracticeCode', 1, 'char', 3, 3, None, None, 1, None, None, 1, None, 3, 2, 'YES', 39),
('TEST', 'dbo', 'XXX', 'PracOnlyName', 12, 'varchar', 100, 100, None, None, 1, None, None, 12, None, 100, 3, 'YES', 39)]
编写一个for循环,以像二维矩阵一样访问列表中每个元素的值“ 4”
new_list = []
for i in range(len(x)):
new_list.append(x[i][3])
此实现将完成