Related
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']]
newlist=['21', '6', '13', '6', '11', '5', '6', '10', '11', '11', '21', '17', '23', '10', '36', '4', '4', '7', '23', '6', '12', '2', '7', '5', '14', '3', '10', '5', '9', '43', '38']
Now what I want to do with this list is take out the ' ' surrounding each integer, the problem is that the split() function or the strip() function wont work. So I don't know how to remove it. Help.
AttributeError: 'list' object has no attribute 'split' is given when I run.
Also After that I want to find the sum of each 7 integers in the list, and that I also don't know where to start. Any help would be appreciated.
That doesn't work because it is a list of strings rather than integers.
x = [int(x) for x in newlist]
This will cast every value in the list to be an int.
Also After that I want to find the sum of each 7 integers in the list
^ Can you clarify what this means? Is this the sum of every seven consecutive numbers until the end of the list? If so,
sums = []
index = 0
for i in range(0, len(newlist), 7):
sums[index] = sum(newlist[i : i + 7)])
index += 1
EDIT:
Full code:
x = [int(x) for x in newlist]
sums = []
for i in range(0, len(x), 7):
sums.append(sum(x[i : i + 7]))
What you have is a list of strings (hence the ''s), and you want to convert them to integers. The boring solution to the problem is a simple for loop:
for i in range(len(newlist)):
newlist[i] = int(newlist[i]
The more compact method is a list comprehension, which you can read about here: List Comprehensions:
newlist = [int(num) for num in newlist]
The two functions you mentioned operate only on single strings.
>>> "Hi my name is Bob".split(" ")
["Hi", "my", "name", "is", "Bob"]
>>> "GARBAGE This string is surrounded by GARBAGE".strip("GARBAGE")
" This string is surrounded by "
As #Tomoko Sakurayama mentioned, you can sum simply enough with another loop. If you're feeling fancy, though, you can use another list comprehension (or even stack it on the old one, though that's not very Pythonic :).
[sum(newlist[i:i+7]) for i in range(0, len(newlist) - 6, 7)] + [sum(newlist[-(len(newlist) % 7):])]
You are probably looking for python's map function:
oldlist = ['21', '6', '13', '6', '11', '5', '6', '10', '11', '11', '21', '17', '23', '10', '36', '4', '4', '7', '23', '6', '12', '2', '7', '5', '14', '3', '10', '5', '9', '43', '38']
newlist = list(map(int, oldlist))
print(type(newlist[0]))
print(newlist)
And output:
<class 'int'>
[21, 6, 13, 6, 11, 5, 6, 10, 11, 11, 21, 17, 23, 10, 36, 4, 4, 7, 23, 6, 12, 2, 7, 5, 14, 3, 10, 5, 9, 43, 38]
You can use map for this case;
new_list = list(map(int, newlist))
print(new_list) == [21, 6, 13, 6, 11, 5, 6, 10, 11, 11, 21, 17, 23, 10, 36, 4, 4, 7, 23, 6, 12, 2, 7, 5, 14, 3, 10, 5, 9, 43, 38]
This question already has answers here:
Element-wise string concatenation in numpy
(6 answers)
Closed 4 years ago.
I have following code:
a = np.arange(25).reshape(5,5)
b = np.arange(25).reshape(5,5)
c = a.astype( str )
d = b.astype( str )
Matrix a and b are:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
Matrix c and d are:
array([['0', '1', '2', '3', '4'],
['5', '6', '7', '8', '9'],
['10', '11', '12', '13', '14'],
['15', '16', '17', '18', '19'],
['20', '21', '22', '23', '24']], dtype='|S11')
I want to get the following matrix by using c and d manipulation.
array([['00', '11', '22', '33', '44'],
['55', '66', '77', '88', '99'],
['1010', '1110', '1212', '1313', '1414'],
['1515', '1616', '1717', '1818', '1919'],
['2020', '2121', '2222', '2323', '2424']], dtype='|S11')
How to do?
Why not do
>>> np.core.defchararray.add(a.astype(str), b.astype(str))
array([['00', '11', '22', '33', '44'],
['55', '66', '77', '88', '99'],
['1010', '1111', '1212', '1313', '1414'],
['1515', '1616', '1717', '1818', '1919'],
['2020', '2121', '2222', '2323', '2424']], dtype='<U22')
Reproducibility material
import numpy as np
a = np.array(
[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]
)
b = np.array(
[['0', '1', '2', '3', '4'],
['5', '6', '7', '8', '9'],
['10', '11', '12', '13', '14'],
['15', '16', '17', '18', '19'],
['20', '21', '22', '23', '24']],
dtype='|S11'
)
I have a list of strings that look like this, and I need to add the first, second, and third entry of each to a integer list a b and c accordingly
print cuts gives me
[['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'], ['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'], ['8', '11', '40'], ['7', '10', '30'], []]
such that
a = [3,2,7, ... , 7]
b = [5, 8, 9, ... , 10]
c = [10, 15, ... , 30]
(Also want these to be ints not strings)
I tried to delimit by
cuts[i] = cuts[i].strip(",")
thinking that it would give me [[3] [5] [10] ] which would let me add with a for loop but python told me lists don't have a strip attribute
Here are list comprehensions in basic form:
data = [['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'],
['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'],
['8', '11', '40'], ['7', '10', '30'], []]
a = [int(x[0]) for x in data if len(x) >= 1]
b = [int(x[1]) for x in data if len(x) >= 2]
c = [int(x[2]) for x in data if len(x) >= 3]
print a
print b
print c
Or, to keep them in a 2-dimensional list:
abc = [[int(x[i]) for x in data if len(x) >= i+1] for i in range(len(data[0]))]
print abc
Output:
[[3, 2, 7, 4, 9, 5, 3, 6, 8, 7],
[5, 8, 9, 6, 12, 7, 8, 9, 11, 10],
[10, 15, 25, 20, 50, 22, 17, 24, 40, 30]]
This can be done using the built in zip function.
a, b, c = zip(*cuts)
You can read about zip here. The gist is here -
this function returns a list of tuples, where the i-th tuple contains
the i-th element from each of the argument sequences or iterables
If you then want your lists to hold numbers instead of strings, you can map them over the int function.
a = map(int, a)
b = map(int, b)
c = map(int, c)
I'm assuming that the empty list at the end of cuts is a typo -- otherwise your requirements don't quite make sense since there isn't a first, second and third element of an empty list. You could always use
cuts = [cut for cut in cuts if len(cut) > 0]
if you want to cut cuts down to size.
In any event, once that empty list is removed, what you are asking for is a sort of zip and can be done like thus:
>>> cuts = [['3', '5', '10'], ['2', '8', '15'], ['7', '9', '25'], ['4', '6', '20'], ['9', '12', '50'], ['5', '7', '22'], ['3', '8', '17'], ['6', '9', '24'], ['8', '11', '40'], ['7', '10', '30']]
>>> a,b,c = ([int(s) for s in strings] for strings in zip(*cuts))
>>> a
[3, 2, 7, 4, 9, 5, 3, 6, 8, 7]
>>> b
[5, 8, 9, 6, 12, 7, 8, 9, 11, 10]
>>> c
[10, 15, 25, 20, 50, 22, 17, 24, 40, 30]
I have a list of integers in string representation, similar to the following:
L1 = ['11', '10', '13', '12',
'15', '14', '1', '3',
'2', '5', '4', '7',
'6', '9', '8']
I need to make it a list of integers like:
L2 = [11, 10, 13, 12, 15, 14, 1, 3, 2, 5, 4, 7, 6, 9, 8]
Finally I will sort it like below:
L3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] # by L2.sort()
Please let me know what is the best way to get from L1 to L3?
You could do it in one step like this:
L3 = sorted(map(int, L1))
In more detail, here are the steps:
>>> L1 = ['11', '10', '13', '12', '15', '14', '1', '3', '2', '5', '4', '7', '6', '9', '8']
>>> L1
['11', '10', '13', '12', '15', '14', '1', '3', '2', '5', '4', '7', '6', '9', '8']
>>> map(int, L1)
[11, 10, 13, 12, 15, 14, 1, 3, 2, 5, 4, 7, 6, 9, 8]
>>> sorted(_)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>>
>>> L1 = ['11', '10', '13', '12', '15', '14', '1', '3', '2', '5', '4', '7', '6', '9', '8']
>>> L1 = [int(x) for x in L1]
>>> L1
[11, 10, 13, 12, 15, 14, 1, 3, 2, 5, 4, 7, 6, 9, 8]
>>> L1.sort()
>>> L1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> L3 = L1
L3 = sorted(int(x) for x in L1)