I'm trying to write a multidimensional numpy
array to a .csv
file. This array includes words, numbers, and a dictionary. When writing the file, my code prints the keys and values to the array. However, I need only the values to be written.
My code can be seen as follows:
import numpy as np
lowercase_letters = 'abcdefghijklmnopqrstuvwxyz'
words_array = np.genfromtxt('words_file.txt', dtype=str).reshape(300, 1)
lowercase_words = np.array([word[0].lower() for word in words_array])
words_array = np.append(words_array, lowercase_words.reshape(300, 1), axis=1)
def letter_frequency(word):
letter_frequency = {}
for letter in lowercase_letters:
letter_frequency[letter] = 0
for letter in word:
letter_frequency[letter] += 1
return letter_frequency
vectorized_frequencies = np.vectorize(letter_frequency)
frequencies = vectorized_frequencies(lowercase_words)
words_array = np.append(words_array, frequencies.reshape(300, 1), axis=1)
word_length = np.vectorize(len)
word_lengths = word_length(words_array[:, :1])
words_array = np.append(words_array, word_lengths.reshape(300, 1), axis=1)
np.savetxt('rocko_bishop_project2.csv', words_array, delimiter=',', fmt='%s',
header="original, lower, a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,length")
After writing the finalized array to the csv file (also required, I can't export as a different file type), it prints the full dictionary, with keys and values:
Abject,abject,{'a': 1, 'b': 1, 'c': 1, 'd': 0, 'e': 1, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 1, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 1, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0},6
However, I would like to to just have the values printed instead.
Abject,abject,1,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0
The parameters of the project prevent me from importing pandas
, or any other library other than numpy
.
What is the best course of action?
Assume, for lack of further information, that your array
is really a list
like:
In [338]: alist = ['Abjure','abjure',{'a': 1, 'b': 1, 'c': 0, 'd': 0, 'e': 1, 'f
...: ': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 1, 'k': 0, 'l': 0, 'm': 0, 'n': 0,
...: 'o': 0, 'p': 0, 'q': 0, 'r': 1, 's': 0, 't': 0, 'u': 1, 'v': 0, 'w': 0
...: , 'x': 0, 'y': 0, 'z': 0}]
If it isn't a list, it must be a object dtype array, in which case arr.tolist()
should produce the same list.
As first try, just print (with possible redirect to file), the elements:
In [341]: print(alist[:2], list(alist[2].values()))
['Abjure', 'abjure'] [1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0]
Here I use values()
to get the dictionary values.
Now we can clean up the print (or file write). This is plain Python. No need to invoke numpy
or any other package.
Expanding the sub lists:
In [343]: print(*alist[:2], *list(alist[2].values()))
Abjure abjure 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.