第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將文本文件中的輸入格式化為python中的defaultdict

如何將文本文件中的輸入格式化為python中的defaultdict

呼喚遠方 2022-11-01 15:18:42
具有這種格式的文本文件有超過 50K 行M:org.apache.mahout.common.RandomUtilsTest:testHashDouble():['(O)java.lang.Double:<init>(double)', '(M)java.lang.Double:hashCode()', '(S)org.apache.mahout.common.RandomUtils:hashDouble(double)', '(S)org.apache.mahout.common.RandomUtilsTest:assertEquals(long,long)', '(O)java.lang.Double:<init>(double)']M:org.apache.mahout.common.RandomUtilsTest:testHashFloat():['(M)java.util.Random:nextLong()', '(M)java.util.Random:nextLong()', '(M)java.util.Random:nextLong()', '(S)org.apache.mahout.common.RandomUtilsTest:assertEquals(java.lang.String,long,long)']M:org.apache.mahout.math.AbstractVectorTest:testAssignBinaryFunction():['(I)org.apache.mahout.math.Vector:assign(org.apache.mahout.math.Vector,org.apache.mahout.math.function.DoubleDoubleFunction)', '(O)java.lang.StringBuilder:<init>()', '(I)org.apache.mahout.math.Vector:getQuick(int)', '(S)org.apache.mahout.math.AbstractVectorTest:assertEquals(java.lang.String,double,double,double)']M:org.apache.mahout.math.AbstractVectorTest:testAssignBinaryFunction2():['(S)org.apache.mahout.math.function.Functions:plus(double)', '(I)org.apache.mahout.math.Vector:assign(org.apache.mahout.math.function.DoubleFunction)', '(S)org.apache.mahout.math.AbstractVectorTest:assertEquals(java.lang.String,double,double,double)']如何讀取這些數(shù)據并將其格式化為字典,以便 [] 中的所有方法都是單獨的值,而 [ (測試方法)之前的字符串是鍵?在將它們作為值存儲在字典中之前,我將如何刪除它們?#Python這是用于填充文本文件的代碼。現(xiàn)在我正在嘗試獲取該 txt 文件數(shù)據并將其讀/解析回另一個字典。    d = {}    with open("filtered.txt") as input:        for line in input:            (key, val) = line.strip().split(" ")            if str(key) in d:                d[str(key)].append(val)            else:                d[str(key)] = [val]    keys = []    for key in d:        keys.append(key)    keys.sort()    input.close()    with open('mahout-coverage.txt', 'w') as outfile:        for key in keys:            outfile.writelines('{}:{}'.format(key, d[key]) + "\n")
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

json 模塊可用于將 python 字典存儲到文件中,然后加載文件并在將其寫入文件之前將其解析為相同的數(shù)據類型。


d = {}

with open('filtered.txt') as input:

    for line in input:

        key, value = line.strip().split("():")

        key = "{}()".format(key)

        d[key] = value


print(d)


# It would be better and easy if you write the data to the file using json module

import json


with open('data.txt', 'w') as json_file:

  json.dump(d, json_file)


# Later you can read the file using the json module itself

with open('data.txt') as f:

  # this data would be a dicitonay which can be easily managed.

  data = json.load(f)

參考:json.dump()json.load()


查看完整回答
反對 回復 2022-11-01
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

使用ast.literal_eval您可以將字符串列表轉換為list


from collections import defaultdict

import ast

with open('tst.txt') as fp:

    d = defaultdict(list)

    for line in fp:

        k, v = line[: line.index('):') + 1], ast.literal_eval(line[line.index(':[') + 1:])

        d[k] += v

print(dict(d))

輸出:


{

M:org.apache.mahout.common.RandomUtilsTest:testHashDoubl :  ['(O)java.lang.Double:<init>(double)', '(M)java.lang.Double:hashCode()', '(S)org.apache.mahout.common.RandomUtils:hashDouble(double)', '(S)org.apache.mahout.common.RandomUtilsTest:assertEquals(long,long)', '(O)java.lang.Double:<init>(double)']

M:org.apache.mahout.common.RandomUtilsTest:testHashFloa :  ['(M)java.util.Random:nextLong()', '(M)java.util.Random:nextLong()', '(M)java.util.Random:nextLong()', '(S)org.apache.mahout.common.RandomUtilsTest:assertEquals(java.lang.String,long,long)']

M:org.apache.mahout.math.AbstractVectorTest:testAssignBinaryFunctio :  ['(I)org.apache.mahout.math.Vector:assign(org.apache.mahout.math.Vector,org.apache.mahout.math.function.DoubleDoubleFunction)', '(O)java.lang.StringBuilder:<init>()', '(I)org.apache.mahout.math.Vector:getQuick(int)', '(S)org.apache.mahout.math.AbstractVectorTest:assertEquals(java.lang.String,double,double,double)']

M:org.apache.mahout.math.AbstractVectorTest:testAssignBinaryFunction :  ['(S)org.apache.mahout.math.function.Functions:plus(double)', '(I)org.apache.mahout.math.Vector:assign(org.apache.mahout.math.function.DoubleFunction)', '(S)org.apache.mahout.math.AbstractVectorTest:assertEquals(java.lang.String,double,double,double)']

}


查看完整回答
反對 回復 2022-11-01
  • 2 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號