小编Ale*_*lex的帖子

是否有更好的方法使用Powershell将标头添加到CSV文件?

有没有更有效的方法将标头添加到CSV文件,而不是对其进行硬编码?

$Importfile = Import-csv $path$filename -header H1, H2, H3, H4, H5, H6, H7 #add headers to csv file

powershell

3
推荐指数
1
解决办法
69
查看次数

迭代并比较python中的两个列表

我对编码和 python 仍然很陌生,所以我不确定迭代和比较两个列表的最佳方法是什么。

  • 如果 list2 的名字、姓氏和 id 与 list1 不同,则附加到从 list2 到 list1 的行。
  • 如果 list2 的名字、姓氏和 id 与 list1 相同,则追加到现有行。

以下是我到目前为止所拥有的......我无法确定将新行附加到 list1 的部分。

list1 = [["John","Smith","ID100",1000.00,50000.00],
     ["Jane","Doe","ID200",2000.00,30000.00],
     ["Joe","Dirt","ID300",20000.00,300000.00]]

list2 = [["John","Smith","ID100",23222.00,123444.00],
     ["Jane","Doe","ID200",65000,70098.00],
     ["Dale","Wright","ID400",25000.00,126000.00],
     ["John","Clark","ID500",23002.00,12111.00]]

for i in range(len(list1)):
    for j in range(len(list2)):
        if list1[i][0] == list2[j][0] and list1[i][1] == list2[j][1] and list1[i][2] == list2[j][2]:       
            count4=3
            for k in range(len(list2[0][3:])):
                list1[i].append(list2[j][count4])
                count4+=1
        else:
            list1.append(list2[i])

print(list1)
Run Code Online (Sandbox Code Playgroud)

结果:

[['John', 'Smith', 'ID100', 1000.0, 50000.0, 23222.0, 123444.0], 
 ['Jane', 'Doe', 'ID200', 2000.0, 30000.0, 65000, 70098.0], …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

2
推荐指数
1
解决办法
67
查看次数

标签 统计

powershell ×1

python ×1

python-3.x ×1