- 1、js如何處理兩個json數組去除重複數據,然後合併數組,求大神幫忙解決下
- 2、python 合併兩個json文件
- 3、python如何合併多個txt文件刪除所有重複行並生成新文件
var c = a.concat(b),//合併成一個數組
temp = {},//用於id判斷重複
result = [];//最後的新數組
//遍歷c數組,將每個item.id在temp中是否存在值做判斷,如不存在則對應的item賦值給新數組,並將temp中item.id對應的key賦值,下次對相同值做判斷時便不會走此分支,達到判斷重複值的目的;
c.map((item,index)={
if(!temp[item.id]){
result.push(item);
temp[item.id] = true
}
})
console.log(result)
先用json.load解析各自的文件內容,
然後用第一個接觸出來的的列表,extend方法第二個列表,然後兩個列表就合二為一了。
把所有的這些txt文件放到一個文件夾里
打開cmd, 進入到這個放了多個txt的文件夾, 運行命令copy *.txt all.txt
在該文件夾下創建一個python腳本 1.py, 將下列代碼複製進去
# coding=utf-8
# using python27
file_path = ‘all.txt’
with open(file_path, ‘r’) as f:
card_informations = map(lambda x: x.strip().split(‘\t’), f.readlines())
for i in range(len(card_informations)):
number = card_informations[i][0]
if len(number)10:
card_informations[i][0] += ‘0’ # 給小於十位的加上0
# 剔除重複數據
result = []
for i in card_informatios:
if i not in result:
result.append(i)
# 寫入新文件
with open(‘result.txt’, ‘w+’) as f:
for i in range(len(result)):
f.write(result[i][0]+’\t’+result[i][1]+’\n)
4. 運行該腳本, 然後該文件夾下就會多出一個result.txt的文件, 裡面放的就是去重完之後的所有卡信息.
原創文章,作者:ZPPC1,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126281.html