Related
I have create this of character
list1 = [['20']*3,['35']*2,['40']*4,['10']*2,['15']*3]
result :
[['20', '20', '20'], ['35', '35'], ['40', '40', '40', '40'], ['10', '10'], ['15', '15', '15']]
I can convert it into a single list using list comprehension
charlist = [x for sublist in list1 for x in sublist]
print(charlist)
['20', '20', '20', '35', '35', '40', '40', '40', '40', '10', '10', '15', '15', '15']
I was wondering how to do that with numpy
listNP=np.array(list1)
gives as output :
array([list(['20', '20', '20']), list(['35', '35']),
list(['40', '40', '40', '40']), list(['10', '10']),
list(['15', '15', '15'])], dtype=object)
The fact is that listNP.flatten() gives as an output the same result. Probably I missed a step when converting the list into an numpy array
You can bypass all the extra operations and use np.repeat:
>>> np.repeat(['20', '35', '40', '10', '15'], [3, 2, 4, 2, 3])
array(['20', '20', '20', '35', '35', '40', '40', '40', '40',
'10', '10', '15', '15', '15'], dtype='<U2')
If you need dtype=object, make the first argument into an array first:
arr1 = np.array(['20', '35', '40', '10', '15'], dtype=object)
np.repeat(arr1, [3, 2, 4, 2, 3])
Use hstack()
import numpy as np
list1 = [['20']*3,['35']*2,['40']*4,['10']*2,['15']*3]
flatlist = np.hstack(list1)
print(flatlist)
['20' '20' '20' '35' '35' '40' '40' '40' '40' '10' '10' '15' '15' '15']
In trying to construct your ListNP with np.array as you do in the OP, I got a warning about jagged arrays and having to use dtype=object, but letting hstack construct it directly doesn't evoke a warning (thanks #Michael Delgado in the comments)
The Data that I have is like :
A = [1,2,3,4,5,6,7,8,~9,10,11,12,13,14,15,16,~17,18,19,20,21,22,23,24,~GV=15,ld=34,gain=15,c=12,ld=45,bpm=12,#*#31,32,33,34,35,36,37,38,~39,40,41,42,43,44,45,46,~47,48,49,50,51,52,53,54,~GV=15,ld=34,gain=15,c=12,ld=100,bpm=130]
I need them to be separated like:
B = [
1,2,3,4,5,6,7,8,
9,10,11,12,13,14,15,16,
17,18,19,20,21,22,23,24,
31,32,33,34,35,36,37,38,
39,40,41,42,43,44,45,46,
47,48,49,50,51,52,53,54
]
C = [
GV=15,ld=34,gain=15,c=12,ld=45,bpm=12
GV=15,ld=34,gain=15,c=12,ld=100,bpm=130
]
What should I do to get an output like this in python?!
A_str = "1,2,3,4,5,6,7,8,~9,10,11,12,13,14,15,16,~17,18,19,20,21,22,23,24,~GV=15,ld=34,gain=15,c=12,ld=45,bpm=12,#*#31,32,33,34,35,36,37,38,~39,40,41,42,43,44,45,46,~47,48,49,50,51,52,53,54,~GV=15,ld=34,gain=15,c=12,ld=100,bpm=130"
A = A_str.split(',')
new_A = []
for elem in A:
if elem[0] == '~':
new_A.append(elem[1:])
elif len(elem) > 3 and elem[0:3] == "#*#":
new_A.append(elem[3:])
else:
new_A.append(elem)
A = new_A
B = []
C = []
for elem in A:
try:
B.append(int(elem))
except ValueError:
C.append(elem)
print(B)
print(C)
You can use python list comprehension for pulling out the elements that match your pattern. I don't exactly know what your pattern is, but it looks like if it has an "=" you'd like to assign it to C, otherwise you'd like to assign it to B. Additionally, you have cleaned some other characters. I'm going to assume you don't care about data types and are happy to have lists of strings.
# you'll need the re library to help you clean up the data
import re
# store your data as a string
A = "1,2,3,4,5,6,7,8,~9,10,11,12,13,14,15,16,~17,18,19,20,21,22,23,24,~GV=15,ld=34,gain=15,c=12,ld=45,bpm=12,#*#31,32,33,34,35,36,37,38,~39,40,41,42,43,44,45,46,~47,48,49,50,51,52,53,54,~GV=15,ld=34,gain=15,c=12,ld=100,bpm=130"
# convert to a list
x = A.split(",")
# create B output based on elements that don't have "=" and keep only the numbers
B = [re.search(r'[0-9]+', i).group(0) for i in x if "=" not in i]
# ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54']
# create C output based on elements that have "=" and remove "~"
C = [i.replace("~","") for i in x if "=" in i]
# ['GV=15', 'ld=34', 'gain=15', 'c=12', 'ld=45', 'bpm=12', 'GV=15', 'ld=34', 'gain=15', 'c=12', 'ld=100', 'bpm=130']
edit: of course, if you do care about data types you can intergerise those strings pretty safely. C has to be strings really.
B = [int(re.search(r'[0-9]+', i).group(0)) for i in x if "=" not in i]
#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54]
edit2: The data structures you've illustrated in the question are not something that exist in python but it looks like a list of lists. So this would be the solution where you want lists of lists where we include the delimiters "~" and "#*#"
import re
# store your data as a string
A = "1,2,3,4,5,6,7,8,~9,10,11,12,13,14,15,16,~17,18,19,20,21,22,23,24,~GV=15,ld=34,gain=15,c=12,ld=45,bpm=12,#*#31,32,33,34,35,36,37,38,~39,40,41,42,43,44,45,46,~47,48,49,50,51,52,53,54,~GV=15,ld=34,gain=15,c=12,ld=100,bpm=130"
# convert to a list
x = re.split('~|#\*#', A)
B = [ list(filter(None, i.split(","))) for i in x if "=" not in i]
C = [list(filter(None, i.split(","))) for i in x if "=" in i]
print(B)
#[['1', '2', '3', '4', '5', '6', '7', '8'], ['9', '10', '11', '12', '13', '14', '15', '16'], ['17', '18', '19', '20', '21', '22', '23', '24'], ['31', '32', '33', '34', '35', '36', '37', '38'], ['39', '40', '41', '42', '43', '44', '45', '46'], ['47', '48', '49', '50', '51', '52', '53', '54']]
print(C)
#[['GV=15', 'ld=34', 'gain=15', 'c=12', 'ld=45', 'bpm=12'], ['GV=15', 'ld=34', 'gain=15', 'c=12', 'ld=100', 'bpm=130']]
def open_csv():
import csv
with open('//Users//samuel//Desktop//L8 More File Processing (1)//students.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
print(open_csv()[1])
This is the output for open_csv():
['Name', 'Gender', 'Test 1', 'Test 2', 'Test 3', 'Test 4', 'Test 5', 'Test 6']
['Aisha', 'F', '0', '33', '67', '27', '12', '14']
['Alex', 'M', '12', '90', '34', '56', '93', '39']
['Bala', 'M', '13', '25', '58', '17', '49', '29']
['Denise', 'F', '13', '93', '84', '53', '65', '62']
['Farhan', 'M', '15', '5', '10', '62', '34', '11']
['Gopi', 'M', '21', '61', '39', '32', '91', '32']
['Irfan', 'M', '26', '36', '3', '95', '36', '39']
['Jun Ming', 'M', '29', '86', '77', '6', '91', '61']
['Lily', 'F', '30', '34', '46', '96', '100', '44']
['Mei Ling', 'F', '39', '58', '9', '61', '32', '46']
['Muthu', 'M', '39', '60', '13', '69', '55', '100']
['Nurul', 'F', '50', '35', '4', '27', '11', '97']
['Priya', 'F', '50', '25', '47', '15', '35', '86']
['Siti', 'F', '58', '71', '13', '19', '58', '30']
['Elisa', 'F', '59', '22', '73', '52', '77', '49']
['Dennis', 'M', '65', '94', '83', '67', '37', '22']
['Harry', 'M', '74', '75', '76', '82', '57', '1']
['Gary', 'M', '90', '12', '70', '86', '50', '59']
['Terry', 'M', '93', '84', '26', '99', '90', '72']
['Corinne', 'F', '100', '17', '88', '14', '33', '9']
When I run this code, I get TypeError:"NoneType" object is not subscriptable. Why is this so and how do I fix it?
If you want to print the second row in your CSV file, you could modify your function as follows:
def open_csv():
import csv
with open('//Users//samuel//Desktop//L8 More File Processing (1)//students.csv', 'r', newline='') as f:
return list(csv.reader(f))
print(open_csv()[1])
This will return all the data as a list of rows, by adding [1] you will be displaying the second row (with [0] being the first row). The reason you were getting the error is that your code did not have a return statement in the function. The default is for Python to return None. So in effect your code was doing:
print(None[0])
Don't forget to add newline='' when using it for a csv.reader()
Turn this function into a generator.
def open_csv():
...
for row in reader:
print(row) // change this to yield(row)
list(open_csv())[0]
This question already has answers here:
Convert all strings in a list to integers
(11 answers)
Closed 7 years ago.
I have a list empty2 which when prints gives the values:
[]
['0', '0', '0', '16', '25', '31', '36', '45', '46', '42']
['0', '0', '0', '0', '0', '0', '0', '4', '11', '23']
['88', '84', '84', '74', '66', '58', '54', '44', '36', '26']
['14', '15', '15', '8', '9', '5', '6', '6', '7', '7']
The function im calling when trying to call items within this list (the individual lists) wants the values in int format.
But I can't seem to find a way to convert each list to int, without destroying the list structure within the lists, which needs to be kept. Any help would great.
x=[[],
['0', '0', '0', '16', '25', '31', '36', '45', '46', '42'],
['0', '0', '0', '0', '0', '0', '0', '4', '11', '23'],
['88', '84', '84', '74', '66', '58', '54', '44', '36', '26'],
['14', '15', '15', '8', '9', '5', '6', '6', '7', '7']]
print [map(int,i) for i in x ]
This should do it for you.
Output:[[], [0, 0, 0, 16, 25, 31, 36, 45, 46, 42], [0, 0, 0, 0, 0, 0, 0, 4, 11, 23], [88, 84, 84, 74, 66, 58, 54, 44, 36, 26], [14, 15, 15, 8, 9, 5, 6, 6, 7, 7]]
I like to move each item in this list to another nested list could someone help me?
a = [['AAA', '1', '1', '10', '92'], ['BBB', '262', '56', '238', '142'], ['CCC', '86', '84', '149', '30'], ['DDD', '48', '362', '205', '237'], ['EEE', '8', '33', '96', '336'], ['FFF', '39', '82', '89', '140'], ['GGG', '170', '296', '223', '210'], ['HHH', '16', '40', '65', '50'], ['III', '4', '3', '5', '2']]
On the end I will make list like this:
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF'.....],
['1', '262', '86', '48', '8', '39', ...],
['1', '56', '84', '362', '33', '82', ...],
['10', '238', '149', '205', '96', '89', ...],
...
...]
Use zip with * and map:
>>> map(list, zip(*a))
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III'],
['1', '262', '86', '48', '8', '39', '170', '16', '4'],
['1', '56', '84', '362', '33', '82', '296', '40', '3'],
['10', '238', '149', '205', '96', '89', '223', '65', '5'],
['92', '142', '30', '237', '336', '140', '210', '50', '2']]
Note that map returns a map object in Python 3, so there you would need list(map(list, zip(*a)))
Using a list comprehension with zip(*...), this would work as is in both Python 2 and 3.
[list(x) for x in zip(*a)]
NumPy way:
>>> import numpy as np
>>> np.array(a).T.tolist()
[['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH', 'III'],
['1', '262', '86', '48', '8', '39', '170', '16', '4'],
['1', '56', '84', '362', '33', '82', '296', '40', '3'],
['10', '238', '149', '205', '96', '89', '223', '65', '5'],
['92', '142', '30', '237', '336', '140', '210', '50', '2']]
Through list comprehension:
[[x[i] for x in mylist] for i in range(len(mylist[0]))]
You can also use
a= np.array(a).transpose().tolist()
You could also do:
row1 = [1,2,3]
row2 = [4,5,6]
row3 = [7,8,9]
matrix = [row1, row2, row3]
trmatrix = [[row[0] for row in matrix],[row[1] for row in matrix], [row[2] for row in matrix]]