Transpose block of text [duplicate] - python

This question already has answers here:
Transposing a text file in Python
(3 answers)
Closed 8 years ago.
I have a block of ones and zeroes, in string:
1111110000111111
1110110110110111
1101010110101011
1011100110011101
0001111111111011
1000110111110111
0100010011110000
0110000001111110
0111000000110110
0000100010010100
1110110011000111
1101111111100011
1011100110000011
1101010111100001
1110110110111101
1111110000111111
I want to transpose it, as if it was a matrix - but keep it in string.
Before I start writing nested for loops, is there an easier way?

s = """1111110000111111
1110110110110111
1101010110101011
1011100110011101
0001111111111011
1000110111110111
0100010011110000
0110000001111110
0111000000110110
0000100010010100
1110110011000111
1101111111100011
1011100110000011
1101010111100001
1110110110111101
1111110000111111"""
>>> [''.join(i) for i in zip(*s.split())]
['1111010000111111',
'1110001110110111',
'1101000110101011',
'1011100010011101',
'1101110001111011',
'1110111000110111',
'0000100000010000',
'0111110000011110',
'0111111001111110',
'0000111100110100',
'1110111110010111',
'1101111111000011',
'1011100100000011',
'1101010111100011',
'1110110110111001',
'1111110000111111']
Edit
If you indeed want a single string as your output, add one more join
>>> '\n'.join(''.join(i) for i in zip(*s.split()))
'1111010000111111\n1110001110110111\n1101000110101011\n1011100010011101\n1101110001111011\n1110111000110111\n0000100000010000\n0111110000011110\n0111111001111110\n0000111100110100\n1110111110010111\n1101111111000011\n1011100100000011\n1101010111100011\n1110110110111001\n1111110000111111'

Related

Why does remove() method does not work in certain instances? [duplicate]

This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
How to remove items from a list while iterating?
(25 answers)
Closed 4 months ago.
mepstrip = ['AE38', 'AL29', 'AL30', 'AL35', 'AL41', 'BA37', 'BB37', 'BC37', 'CH24', 'CO26', 'GD29', 'GD30', 'GD35', 'GD38', 'GD41', 'GD46', 'PM29', 'SA24', 'T2V1', 'TDJ3']
indexlist = ['AE38', 'AE38C', 'AE38D', 'AL29', 'AL29C', 'AL29D', 'AL30', 'AL30C', 'AL30D', 'AL35', 'AL35C', 'AL35D', 'AL41', 'AL41C', 'AL41D', 'AY24C', 'BA37D', 'BA37E', 'BAY23', 'BB37D', 'BB37E', 'BC37D', 'BDC24', 'BDC28', 'CEDI', 'CH24D', 'CO23', 'CO26', 'CO26D', 'CUAP', 'DICP', 'DIP0', 'FORM3', 'GD29', 'GD29C', 'GD29D', 'GD30', 'GD30C', 'GD30D', 'GD35', 'GD35C', 'GD35D', 'GD38', 'GD38C', 'GD38D', 'GD41', 'GD41C', 'GD41D', 'GD46', 'GD46C', 'GD46D', 'PAP0', 'PARP', 'PBA25', 'PBY22', 'PM29D', 'PMM29', 'PMY24', 'PR13', 'PUL26', 'SA24D', 'SARH', 'SFA23', 'T2V1C', 'T2V1D', 'T2V2', 'T2V3', 'T2X3', 'T2X4', 'TB23P', 'TB24', 'TC23', 'TC25P', 'TDF24', 'TDJ23', 'TDJ3D', 'TDL23', 'TDS23', 'TFU27', 'TO23', 'TO26', 'TV23', 'TV24', 'TX22', 'TX23', 'TX24', 'TX25', 'TX26', 'TX28', 'TY22P', 'TY27P']
for i in mepstrip:
if i not in indexlist:
mepstrip.remove(i)
# ['BB37', 'CH24', 'SA24', 'TDJ3'] remains in mepstrip after for loop despite not in indexlist
You can't remove items from a list while iterating over it. Instead, you could make a copy of the list. And remove the items from that.
new_mepstrip = mepstrip.copy()
for i in mepstrip:
if i not in indexlist:
new_mepstrip.remove(i)

Removing line breaks from numpy array [duplicate]

This question already has answers here:
How do I print the full NumPy array, without truncation?
(22 answers)
Closed 3 years ago.
I have a function to calculate the average vector for each name which is made of many words, this function is returning numpy.ndarray with shape of (100,). The resulting vector is as the following:
[ 0.00127441 0.0002633 0.00039622 0.00055501 0.00070984 -0.00089766
-0.00073814 -0.00224919 0.00233035 -0.00037628 0.00125402 -0.00052623
0.00114087 -0.00070441 -0.00419099 0.00031204 -0.0002703 -0.00290918
...(13 lines)
0.00260704 -0.00000406 -0.00160876 0.00134342]
As upon receiving the numpy array, I am removing line breaks as follows:
temp = ["%.8f" % number for number in name_avg_vector]
temp=re.sub('\s+', ' ', temp)
name_avg_vector= np.array(list(temp))
but I am getting the following error:
---> 79 temp=re.sub('\s+', ' ', name_avg_vector)
TypeError: cannot use a string pattern on a bytes-like object
I also tried changing the printoptions, but I continue having the break line in the file storing the numpy array values:
import sys
np.set_printoptions(threshold=sys.maxsize)
np.set_printoptions(threshold=np.inf)
After, I tried with array_repr to remove the break line:
name_avg_vector = np.array_repr(name_avg_vector).replace('\n', '')
but it saves as:
['array([-0.00849786, 0.00113221, -0.00643946, 0.00437448, -0.00740928, 0.00381133, 0.00178376, -0.00065115, -0.00050142, -0.0001178 , 0.00029183, 0.00015484, -0.00001569, 0.0006973 , 0.00051486, 0.00006652, -0.00099618, -0.00049231, 0.0003479 , 0.00135821, 0.00078396, 0.00038927, 0.00040825, -0.00093267, 0.00025755, -0.00012063, -0.00074733, 0.00120466, 0.00041425, -0.00062592, 0.00098112, 0.00101578, -0.00048335, 0.00079251, -0.00112981,
...
-0.00050014, 0.00133685, -0.00020537, -0.00082505])']
As stated by Anoyz in here, converting to list gets rid of break lines such as name_avg_vector.tolist().
Thanks
Your numpy array appears to have dtype float so it doesn't actually contain any new lines. I assume what you are seeing are linebreaks when you do something like print(name_avg_vector). One way to solve the problem is to write your own loop to print the values in the format you want.

How to convert a tuple list, after an SQL query, into an array to make operations simple [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
Good morning I have the following query, I make an SQL query and I get a list of tuples in response, the question is that I want to convert that list of tuples into an array to facilitate operations, since after my code requires using the array values to UPDATE in the Database
cursor = db_equipo.cursor()
sql_interface="SELECT id_interface FROM Interface WHERE id_EquipoOrigen_id=%s"
cursor.execute(sql_interface,(id_equipo,))
z=cursor.fetchall()
print(z)
((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))
At first, think about making a loop with two indexes so that you could have an index for the list, and another for the tuple, something like z [x] [y], but it is poorly optimized:
z[0][0]=3027
Z[1][0]=3028
.
.
And I would like something like:
[3027,3028,3029,3030 ...]
You can use a list comprehension:
[datum[0] for datum in z]
Or, if you want your code to be a bit fancy:
next(zip(*z))
You can use a list comprehension:
tup = ((3027,), (3028,), (3029,), (3030,), (3031,), (3032,), (3033,), (3034,), (3036,), (3037,), (3038,), (3039,), (3040,), (3041,), (3042,), (3043,), (3044,), (3045,), (3046,), (3047,), (3048,), (3049,), (3050,), (3051,), (3052,), (3053,), (3054,), (3055,), (3056,), (3057,), (3058,), (3059,), (3060,), (3061,), (3062,), (3063,), (3064,), (3065,), (3066,), (3067,), (3068,), (3069,), (3070,), (3071,), (3072,), (3073,))
wanted_array = [entry[0] for entry in tup]

Python - insert variable when defining a name [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I have a relatively stupid question which I am not able to formulate very well (and I think it explains why I am not finding any answer)
I would like to calculate the mean, minimum, and maximum of a panda series in my dataframe for many variable (let's say age and weight)
dataframe.age.min()
dataframe.age.max()
dataframe.age.mean()
dataframe.weight.min()
dataframe.weight.max()
dataframe.weight.mean()
I would like to create some kind of loop, which would do something like:
list = ['age','weight']
for x in list:
min-"x" = dataframe.x.min()
max-"x" = dataframe.x.max()
mean-"x" = dataframe.x.mean()
I would like to have variables called min-age, max-age, mean-age
I don't understand how to define a function, and how to insert in the name min-"x" the name of my variable (x)...
Use describe on you dataframe then manipulate the index.
dfd = df.describe().stack()
dfd.index = dfd.index.to_series().str.join('-')
count-age 10.000000
count-weight 10.000000
mean-age -0.200662
mean-weight 0.298352
std-age 1.175323
std-weight 0.901915
min-age -1.778043
min-weight -0.860798
25%-age -1.144173
25%-weight -0.488076
50%-age -0.092748
50%-weight 0.294160
75%-age 0.276348
75%-weight 0.892405
max-age 1.670823
max-weight 1.680473
dtype: float64

Python CodeLab dictionary-traversal [duplicate]

This question already has answers here:
How do I merge two dictionaries in a single expression in Python?
(43 answers)
Closed 7 years ago.
The question is
This is what I have so far:
dict(nafta_capitals) = canadian_capitals, mexican_capitals, us_capitals
Given three dictionaries, associated with the variables , canadian_capitals, mexican_capitals, and us_capitals, that map provinces or states to their respective capitals, create a new dictionary that combines these three dictionaries, and associate it with a variable , nafta_capitals.
You may need to use defaultdict-
Here nafta is used as key to the three ( canadian_capitals, mexican_capitals, us_capitals) as below-
>>>dic = defaultdict(list)
>>>lst = ['nafta1', 'canadian_capitals1', 'mexican_capitals1', 'us_capitals1', 'nafta2', 'canadian_capitals2', 'mexican_capitals2', 'us_capitals2']
>>>grouped_lst = [lst[i:i+4] for i in range(0,len(lst),4)]
>>>[['nafta1', 'canadian_capitals1', 'mexican_capitals1', 'us_capitals1'], ['nafta2', 'canadian_capitals2', 'mexican_capitals2', 'us_capitals2']]
>>>for i in grouped_lst:dic[i[0]]=i[1:]
>>>dic.items()
>>>[('nafta1', ['canadian_capitals1', 'mexican_capitals1', 'us_capitals1']), ('nafta2', ['canadian_capitals2', 'mexican_capitals2', 'us_capitals2'])]
>>>for i in dic.keys():print dic[i]
>>>['canadian_capitals1', 'mexican_capitals1', 'us_capitals1']
['canadian_capitals2', 'mexican_capitals2', 'us_capitals2']

Categories