This question already has answers here:
How can I get 2.x-like sorting behaviour in Python 3.x?
(10 answers)
Closed 7 months ago.
Below code is working in python 2 but its breaking in python 3, could you please help
destObj={('SVC_157', None):'05-04-2022', ('SVC_157', 'V1.0'):'06-07-2022'}
for (rel, ver) in sorted(destObj.keys()):
print((rel, ver))
Please note: position of None is not fixed, it may vary if release is having more components for ex like below,
('SVC_157', 'SMU', 'Windows 10 ARM64', None )
FYI: Its not duplicate of Sorting list by an attribute that can be None
Use the key parameter as below, to mimic the Python 2 behavior:
destObj = {('SVC_157', None): '05-04-2022', ('SVC_157', 'V1.0'): '06-07-2022'}
def key(t):
return tuple(e or "" for e in t)
for (rel, ver) in sorted(destObj.keys(), key=key):
print((rel, ver))
Output
('SVC_157', None)
('SVC_157', 'V1.0')
If you have tuples of multiple sizes, the unpacking in the for loop is not going to work, do the following instead:
destObj = {('SVC_157', 'SMU', 'Windows 10 ARM64', None): '05-04-2022', ('SVC_157', 'V1.0'): '06-07-2022'}
def key(t):
return tuple(e or "" for e in t)
for tup in sorted(destObj.keys(), key=key):
print(tup)
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]
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']
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'