动态数组附加/导出-Python

时间:2018-11-07 23:16:08

标签: python arrays dynamic save append

对于Python来说是相当新的知识,并且一直在努力创建一些东西来保存和将我的输出函数的新值附加到导出数组中。到目前为止,还不够优雅

merit=5 #example input
MeritHist=[] #starting empty list
MeritHist.append(merit)

MeritArray=np.asarray(MeritHist) #turns into array
np.savetxt('test.out', MeritArray, delimiter=',')   #exports array

第一次运行后,我对MeritHist = []行进行了哈希处理,以使它不会重新启动我的列表,然后在以后的运行中添加新值,如我所愿,但这感觉不正确...

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

Map<String, Set<String>> myMap;

for(foo f : arr1) {
    for(String s : f.someList) {
        Set<String> mySet = myMap.get(s);
        if(mySet == null) {
            myMap.put(s, new HashSet<String>(Arrays.asList(f.someString)));
        } else {
            mySet.add(f.someString);
        }
    }
}

List<String> myNewList = myMap.entrySet()
                              .stream()
                              .map(e -> 
                                    new foo(e.key, new ArrayList<String>(e.value)))
                              .collect(Collectors.toList());

答案 1 :(得分:0)

merit=5 #example input
MeritHist=[] #starting empty list
MeritHist.insert(-1,merit) ##insert to the beginning of the list 
print(MeritHist)


# or try this  
meritList= []
merit= input("Type a Number: ") 
meritList.append(merit)#aapend to the list 
print(meritList)