How to get subsequent keys from a dictionary in python? - python

so I'm working on this referencing problem in which I need to get a key from a dictionary and its child keys(if any)
for example, I have a list of dictionary keys as such:
dict_keys(['1.', '1.1', '1.2', '1.2.1', '1.2.2', '1.2.2(a)', '1.2.2(b)', '1.2.2(c)', '1.2.2(d)', '1.2.3', '1.2.4', '1.2.5', '1.2.6', '1.2.7', '1.2.8', '1.2.9', '1.2.10', '1.2.11', '2.', '2.1', '3.', '3.1', '3.1.1', '3.1.2', '3.2', '3.3', '3.4', '3.5', '3.5.1', '3.5.2', '3.5.2(a)', '3.5.2(b)', '3.5.2(c)', '3.6', '3.7', '3.8', '3.9', '3.9.1', '3.9.2', '3.9.3', '3.10', '3.11', '3.11.1', '3.11.2', '4.', '4.1', '4.1.1', '4.1.2', '4.1.3', '4.1.4', '4.1.5', '4.1.6', '4.1.7', '4.1.8', '4.1.9', '4.1.10', '4.2', '5.', '5.1', '5.1.1', '5.1.2', '5.1.3', '5.1.4', '5.1.4(a)', '5.1.4(b)', '5.1.4(c)', '5.1.4(d)', '5.1.4(e)', '5.1.4(f)', '5.1.4(g)', '5.1.4(h)', '5.1.4(i)', '5.1.4(j)', '5.1.4(k)', '5.2', '5.3', '5.4', '6.', '6.1', '6.2', '7.', '7.1', '7.2', '7.3', '7.3.1', '7.3.2', '7.3.3', '7.4', '7.5', '7.6', '7.6.1', '7.6.2', '7.6.3', '7.6.4', '8.', '8.1', '8.2', '8.2.1', '8.2.2', '8.2.3', '8.2.4', '8.2.5', '8.2.6', '8.2.7', '8.3', '8.3.1', '8.3.2', '8.3.3', '8.3.4', '8.3.5', '8.3.6', '8.4', '9.', '9.1', '9.1.1', '9.1.2', '9.2', '9.3', '9.4', '9.5', '10.', '10.1', '10.2', '10.3', '10.4', '11.', '11.1', '11.2', '11.2.1', '11.2.2', '11.2.3', '11.3', '11.4', '11.5', '11.6', '11.7', '11.8', '11.9', '11.10', '12.', '12.1', '12.2', '12.2.1', '12.2.2', '12.3', '12.4', '12.4.1', '12.4.2', '12.4.3', '12.5', '12.6', '12.7', '12.8', '12.9', '12.10', '12.11', '13.', '13.1', '13.2', '13.3', '13.3.1', '13.3.2', '13.3.3', '13.3.4', '13.4', '13.4.1', '13.4.2', '13.4.3', '14.', '14.1', '14.2', '14.3', '15.', '15.1', '15.2', '15.2.1', '15.2.2', '15.2.3', '15.2.4', '16.', '16.1', '16.2', '16.3', '16.4', '16.5', '16.6', '16.7', '16.7.1', '16.7.2', '17.', '17.1', '17.1.1', '17.1.2', '17.1.3', '17.1.4', '17.2', '17.2.1', '17.2.2', '17.2.3', '17.2.4', '17.3', '17.4', '17.4.1', '17.4.2', '17.5', '17.5.1', '17.5.2', '17.5.3', '18.', '18.1', '18.2', '18.2.1', '18.2.1(a)', '18.2.1(b)', '18.2.1(c)', '18.2.2', '18.2.3', '18.2.4', '18.2.5', '18.2.5(a)', '18.2.5(b)', '18.2.6', '18.2.7', '19.', '19.1', '19.2', '19.3', '19.4', '20.', '21.', '21.1', '21.2', '21.3', '21.4', '22.', '22.1', '22.1.1', '22.1.2', '22.1.3', '22.1.4', '22.2', '22.2.1', '22.2.2', '22.3', '22.4', '22.5', '22.6', '22.6.1', '22.6.2', '22.7', '23.', '23.1', '23.1.1', '23.1.1(a)', '23.1.1(b)', '23.1.1(c)', '23.1.2', '23.1.3', '23.1.3(a)', '23.1.3(b)', '23.1.4', '23.2', '23.2.1', '23.2.2', '23.3', '23.4', '23.5', '24.', '24.1', '24.1.1', '24.1.2', '24.1.3', '24.1.4', '24.1.5', '24.1.6', '24.2', '24.3', '25.', '25.1', '25.1.1', '25.1.2', '25.1.3', '25.1.4', '25.1.5', '25.1.6', '25.2', '25.2.1', '25.2.2', '25.2.3', '25.2.4', '25.2.5', '25.3', '26.', '27.', '27.1', '27.2', '27.2.1', '27.2.2', '27.2.3', '27.3', '28.', '28.1', '28.2', '28.2.1', '28.2.2', '28.2.3', '28.2.4', '28.2.5', '28.3', '29.', '29.1', '29.1.1', '29.1.2', '29.1.3', '29.2', '30.', '30.1', '30.2', '30.2.1', '30.2.2', '30.2.3', '30.3', '30.4', '31.', '31.1', '31.2', '31.3', '31.4', '31.5', '31.5.1', '31.5.2', '31.6', '31.7', '31.8', '31.8.1', '31.8.2', '31.8.3', '31.9', '31.10', '31.11'])
in this, if I have something referred in key 3.1, I also want to extract the data for keys 3.1.1 and 3.1.2(child keys). so for this I used str.startwith() method which fails in cases as such.
It will not only return me the child clauses but also other clauses like 3.10, 3.11... which also start with 3.1, so this will result in some false positives and leakages.
The caveat however being that I don't know for sure that what numbering system will the user be using in the agreement.
for example:
it could be anything like:
['(1.)', '(1.)a.', '(2.)'] or ['1.', '1.1.', '1.1.1.', '1.1.1.a',] or ['1.', '1.1', '1.1.1', '1.1.1a']
so I'm trying to figure out a function using which I can cover all these ways.
How can I do that?
Thanks in advance! :)

You're much more familiar with your dataset and how robust you need to be, but I set that set of dict_keys to keys and used the following:
if key.count('.') < 2:
child_keys = [k for k in keys if k.startswith(key + '.')]
else:
child_keys = [k for k in keys if k.startswith(key + '(')]
If key = '1.2.2', then child_keys = ['1.2.2(a)', '1.2.2(b)', '1.2.2(c)', '1.2.2(d)']. If key = '3.1', then child_keys = ['3.1.1', '3.1.2'].

def get_subsequent_keys(key):
subsequent_keys = []
total_keys = structure.keys()
try:
if key[-1] == '.' or key[-1] == ')':
if key in total_keys:
pass
elif key[:-1] in total_keys:
subsequent_keys.append(key[:-1])
subsequent_keys += [x for x in total_keys if x.startswith(key)]
else:
key_pat = fr"({key}[a-z]+)"
subsequent_keys.append(key)
subsequent_keys += [x for x in total_keys if x.startswith(key + '.')]
subsequent_keys += [x for x in total_keys if x.startswith(key + '(')]
subsequent_keys += re.findall(key_pat, ' '.join(total_keys))
except Exception as exception:
logger.debug(exception)
finally:
return subsequent_keys
So I used this way to cover all the possible cases.
The problem with my use case is that I don't really have a definite way by which the user numbers the clauses in the agreement. Hence I needed a generic function to cover all ground. Also, thanks to Ava and Hayley for their help! :D

Related

Sorting a multidimensional array using merge sort?

I am trying to sort this multidimensional array after the number on the first index using the merge sort algorithm, but I am very unsure on how to do so.
This is the multidimensional array I am trying to sort:
Orders_db = [[1347517405, 54413, '78'], [1347517413, 54421, '86'], [1347517454, 54462, '127'], [1347517460, 54468, '133'], [1347517461, 54469, '134'], [1347517426, 54434, '99'], [1347517464, 54472, '137'], [1347517394, 54402, '67'], [1347517445, 54453, '118'], [1347517375, 54383, '48'], [1347517377, 54385, '50'], [1347517392, 54400, '65'], [1347517450, 54458, '123'], [1347517404, 54412, '77'], [1347517389, 54397, '62'], [1347517393, 54401, '66'], [1347517440, 54448, '113'], [1347517457, 54465, '130'], [1347517444, 54452, '117'], [1347517400, 54408, '73'], [1347517412, 54420, '85'], [1347517371, 54379, '44'], [1347517415, 54423, '88'], [1347517441, 54449, '114'], [1347517435, 54443, '108'], [1347517409, 54417, '82'], [1347517398, 54406, '71'], [1347517422, 54430, '95'], [1347517468, 54476, '141'], [1347517402, 54410, '75'], [1347517437, 54445, '110'], [1347517446, 54454, '119'], [1347517382, 54390, '55'], [1347517399, 54407, '72'], [1347517438, 54446, '111'], [1347517416, 54424, '89'], [1347517380, 54388, '53'], [1347517425, 54433, '98'], [1347517406, 54414, '79'], [1347517449, 54457, '122'], [1347517388, 54396, '61'], [1347517430, 54438, '103'], [1347517455, 54463, '128'], [1347517458, 54466, '131'], [1347517452, 54460, '125'], [1347517396, 54404, '69'], [1347517423, 54431, '96'], [1347517465, 54473, '138'], [1347517397, 54405, '70'], [1347517459, 54467, '132'], [1347517395, 54403, '68'], [1347517381, 54389, '54'], [1347517424, 54432, '97'], [1347517436, 54444, '109'], [1347517434, 54442, '107'], [1347517401, 54409, '74'], [1347517376, 54384, '49'], [1347517467, 54475, '140'], [1347517456, 54464, '129'], [1347517427, 54435, '100'], [1347517383, 54391, '56'], [1347517451, 54459, '124'], [1347517433, 54441, '106'], [1347517414, 54422, '87'], [1347517417, 54425, '90'], [1347517453, 54461, '126'], [1347517378, 54386, '51'], [1347517432, 54440, '105'], [1347517403, 54411, '76'], [1347517439, 54447, '112'], [1347517448, 54456, '121'], [1347517410, 54418, '83'], [1347517391, 54399, '64'], [1347517447, 54455, '120'], [1347517421, 54429, '94'], [1347517379, 54387, '52'], [1347517411, 54419, '84'], [1347517386, 54394, '59'], [1347517384, 54392, '57'], [1347517374, 54382, '47'], [1347517462, 54470, '135'], [1347517431, 54439, '104'], [1347517419, 54427, '92'], [1347517428, 54436, '101'], [1347517466, 54474, '139'], [1347517443, 54451, '116'], [1347517463, 54471, '136'], [1347517385, 54393, '58'], [1347517387, 54395, '60'], [1347517373, 54381, '46'], [1347517372, 54380, '45'], [1347517418, 54426, '91'], [1347517420, 54428, '93'], [1347517469, 54477, '142]'], [1347517442, 54450, '115'], [1347517408, 54416, '81'], [1347517390, 54398, '63'], [1347517407, 54415, '80'], [1347517429, 54437, '102']]
And I can implement a general merge sort algorithm, but i cannot do it in a way where I sort after the number in the array on the first index.
My implementation of merge sort is:
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
merge_sort(left)
merge_sort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1
return arr
How can I fit the general merge sort algorithm to sort this array?
The preferred answer is that it returns the array with the highest number in the end.
Try below:
def merge(left, right):
if not len(left) or not len(right):
return left or right
result = []
i, j = 0, 0
while (len(result) < len(left) + len(right)):
if left[i][0] < right[j][0]:
result.append(left[i])
i+= 1
else:
result.append(right[j])
j+= 1
if i == len(left) or j == len(right):
result.extend(left[i:] or right[j:])
break
return result
def mergesort(list):
if len(list) < 2:
return list
middle = len(list)/2
left = mergesort(list[:middle])
right = mergesort(list[middle:])
return merge(left, right)
seq = [[1347517405, 54413, '78'], [1347517413, 54421, '86'], [1347517454, 54462, '127'], [1347517460, 54468, '133'], [1347517461, 54469, '134'], [1347517426, 54434, '99'], [1347517464, 54472, '137'], [1347517394, 54402, '67'], [1347517445, 54453, '118'], [1347517375, 54383, '48'], [1347517377, 54385, '50'], [1347517392, 54400, '65'], [1347517450, 54458, '123'], [1347517404, 54412, '77'], [1347517389, 54397, '62'], [1347517393, 54401, '66'], [1347517440, 54448, '113'], [1347517457, 54465, '130'], [1347517444, 54452, '117'], [1347517400, 54408, '73'], [1347517412, 54420, '85'], [1347517371, 54379, '44'], [1347517415, 54423, '88'], [1347517441, 54449, '114'], [1347517435, 54443, '108'], [1347517409, 54417, '82'], [1347517398, 54406, '71'], [1347517422, 54430, '95'], [1347517468, 54476, '141'], [1347517402, 54410, '75'], [1347517437, 54445, '110'], [1347517446, 54454, '119'], [1347517382, 54390, '55'], [1347517399, 54407, '72'], [1347517438, 54446, '111'], [1347517416, 54424, '89'], [1347517380, 54388, '53'], [1347517425, 54433, '98'], [1347517406, 54414, '79'], [1347517449, 54457, '122'], [1347517388, 54396, '61'], [1347517430, 54438, '103'], [1347517455, 54463, '128'], [1347517458, 54466, '131'], [1347517452, 54460, '125'], [1347517396, 54404, '69'], [1347517423, 54431, '96'], [1347517465, 54473, '138'], [1347517397, 54405, '70'], [1347517459, 54467, '132'], [1347517395, 54403, '68'], [1347517381, 54389, '54'], [1347517424, 54432, '97'], [1347517436, 54444, '109'], [1347517434, 54442, '107'], [1347517401, 54409, '74'], [1347517376, 54384, '49'], [1347517467, 54475, '140'], [1347517456, 54464, '129'], [1347517427, 54435, '100'], [1347517383, 54391, '56'], [1347517451, 54459, '124'], [1347517433, 54441, '106'], [1347517414, 54422, '87'], [1347517417, 54425, '90'], [1347517453, 54461, '126'], [1347517378, 54386, '51'], [1347517432, 54440, '105'], [1347517403, 54411, '76'], [1347517439, 54447, '112'], [1347517448, 54456, '121'], [1347517410, 54418, '83'], [1347517391, 54399, '64'], [1347517447, 54455, '120'], [1347517421, 54429, '94'], [1347517379, 54387, '52'], [1347517411, 54419, '84'], [1347517386, 54394, '59'], [1347517384, 54392, '57'], [1347517374, 54382, '47'], [1347517462, 54470, '135'], [1347517431, 54439, '104'], [1347517419, 54427, '92'], [1347517428, 54436, '101'], [1347517466, 54474, '139'], [1347517443, 54451, '116'], [1347517463, 54471, '136'], [1347517385, 54393, '58'], [1347517387, 54395, '60'], [1347517373, 54381, '46'], [1347517372, 54380, '45'], [1347517418, 54426, '91'], [1347517420, 54428, '93'], [1347517469, 54477, '142]'], [1347517442, 54450, '115'], [1347517408, 54416, '81'], [1347517390, 54398, '63'], [1347517407, 54415, '80'], [1347517429, 54437, '102']]
print("Given array is")
print(seq);
print("\n")
print("Sorted array is")
print(mergesort(seq))
If you chage left[i][0] < right[j][0] to left[i][1] < right[j][1] then it will sort accrding to the second element in the inner array.

String Replace turn result to integers python

line = ['R2', 'R5', 'R6', 'R7', 'R10', 'R12', 'R13', 'R15', 'R18', 'R20', 'R21', 'R22', 'R23', 'R24', 'R26', 'R30', 'R31', 'R32', 'R33', 'R34', 'R35', 'R36', 'R37', 'R38', 'R44', 'R45', 'R46', 'R47', 'R48', 'R53', 'R55', 'R56', 'R63', 'R66', 'R68', 'R69', 'R70', 'R74', 'R78', 'R80', 'R81', 'R82', 'R83', 'R84', 'R90', 'R91', 'R92', 'R97', 'R98', 'R99', 'R100', 'R101', 'R102', 'R103', 'R108', 'R109', 'R111', 'R115', 'R116', 'R117', 'R118', 'R120', 'R121', 'R123', 'R124', 'R126', 'R130', 'R131', 'R132', 'R136', 'R138', 'R141', 'R142', 'R151', 'R152', 'R153', 'R154', 'R155', 'R156', 'R158', 'R160', 'R161', 'R163', 'R164', 'R167', 'R169', 'R171', 'R172', 'R177', 'R178', 'R179', 'R180', 'R181', 'R182', 'R183', 'R184', 'R186', 'R190', 'R192', 'R195', 'R196', 'R199', 'R201', 'R203', 'R204', 'R205', 'R206', 'R207', 'R209', 'R210', 'R211', 'R213', 'R214', 'R215', 'R216', 'R217', 'R223', 'R225', 'R227', 'R228', 'R229', 'R231', 'R233', 'R237', 'R238', 'R239', 'R240', 'R245', 'R246', 'R247', 'R248', 'R249', 'R250', 'R252', 'R253', 'R254', 'R255', 'R257', 'R258', 'R265', 'R266', 'R267', 'R268', 'R270', 'R271', 'R273', 'R275', 'R276', 'R277', 'R279', 'R280', 'R281', 'R284', 'R287', 'R290', 'R291', 'R292', 'R293', 'R294', 'R296', 'R299', 'R300', 'R301', 'R303', 'R304', 'R305', 'R306', 'R307', 'R308', 'R309', 'R310', 'R312', 'R313', 'R314', 'R315', 'R317', 'R320', 'R321', 'R323', 'R325', 'R327', 'R329', 'R330', 'R331', 'R334', 'R335', 'R336', 'R339', 'R340', 'R341', 'R343', 'R344', 'R345', 'R347', 'R349', 'R350', 'R352', 'R354', 'R356', 'R358', 'R359', 'R360', 'R361', 'R362', 'R363', 'R365', 'R367', 'R372', 'R373', 'R376', 'R378', 'R379', 'R381', 'R384', 'R386', 'R388', 'R393', 'R397', 'R398', 'R399', 'R401', 'R402', 'R403', 'R405', 'R406', 'R407', 'R410', 'R411', 'R413', 'R414', 'R416', 'R419', 'R420', 'R421', 'R422', 'R427', 'R430', 'R433', 'R434', 'R435', 'R438', 'R439', 'R443', 'R444', 'R445', 'R453', 'R454', 'R455', 'R456', 'R458', 'R459', 'R460', 'R461', 'R462', 'R463', 'R464', 'R466', 'R467', 'R468', 'R469', 'R470', 'R473', 'R475', 'R476', 'R478', 'R481', 'R482', 'R483', 'R485', 'R487', 'R488', 'R489', 'R490', 'R492', 'R494', 'R496', 'R497', 'R499', 'R500', 'R502', 'R503', 'R506', 'R508', 'R509', 'R511', 'R512', 'R516', 'R517', 'R519', 'R520', 'R528', 'R530', 'R532', 'R537', 'R539', 'R540', 'R541', 'R542', 'R544', 'R545', 'R547', 'R549', 'R550', 'R552', 'R555', 'R558', 'R560', 'R561', 'R562', 'R563', 'R564', 'R565', 'R566', 'R568', 'R571', 'R572', 'R573', 'R574', 'R576', 'R579', 'R581', 'R583', 'R584', 'R585', 'R586', 'R588', 'R590', 'R592', 'R593', 'R595', 'R597', 'R598', 'R600', 'R601', 'R606', 'R607', 'R609', 'R610', 'R612', 'R613', 'R614', 'R615', 'R617', 'R618', 'R619', 'R620', 'R622', 'R624', 'R625', 'R626', 'R627', 'R628', 'R629', 'R630', 'R631', 'R632', 'R633', 'R634', 'R635', 'R636', 'R638', 'R642', 'R644', 'R645', 'R646', 'R647', 'R648', 'R649', 'R650', 'R652', 'R654', 'R657', 'R658', 'R660', 'R662', 'R668', 'R669', 'R671', 'R672', 'R673', 'R674', 'R676', 'R677', 'R678', 'R683', 'R684', 'R686', 'R689', 'R691', 'R693', 'R694', 'R696', 'R697', 'R698', 'R700', 'R701', 'R704', 'R706', 'R707', 'R710', 'R712', 'R713', 'R715', 'R716', 'R718', 'R719', 'R721', 'R724', 'R725', 'R728', 'R729', 'R730', 'R731', 'R733', 'R734', 'R737', 'R738', 'R739', 'R740', 'R743', 'R744', 'R745', 'R747', 'R748', 'R751', 'R755', 'R756', 'R757', 'R758', 'R759', 'R761', 'R762', 'R763', 'R767', 'R768', 'R770', 'R773', 'R774', 'R775', 'R776', 'R777', 'R779', 'R783', 'R785', 'R786', 'R789', 'R791', 'R795', 'R796', 'R797', 'R798', 'R800', 'R801', 'R802', 'R804', 'R805', 'R806', 'R808', 'R818', 'R819', 'R820', 'R822', 'R824', 'R826', 'R827', 'R829', 'R830', 'R831', 'R833', 'R835', 'R836', 'R838', 'R840', 'R841', 'R843', 'R844', 'R845', 'R847', 'R848', 'R849', 'R850', 'R851', 'R852', 'R853', 'R855', 'R858', 'R859', 'R860', 'R862', 'R863', 'R865', 'R867', 'R868', 'R869', 'R870', 'R874', 'R875', 'R876', 'R880', 'R881', 'R882', 'R884', 'R885', 'R886', 'R889', 'R891', 'R892', 'R893', 'R894', 'R895', 'R897', 'R900', 'R906', 'R908', 'R909', 'R911', 'R912', 'R914', 'R917', 'R918', 'R921', 'R922', 'R924', 'R925', 'R926', 'R927', 'R928', 'R929', 'R930', 'R932', 'R935', 'R937', 'R943', 'R944', 'R945', 'R946', 'R947', 'R948', 'R949', 'R951', 'R952', 'R953', 'R956', 'R957', 'R958', 'R959', 'R961', 'R964', 'R965', 'R966', 'R967', 'R968', 'R969', 'R971', 'R972', 'R974', 'R975', 'R976', 'R979', 'R981', 'R982', 'R983', 'R986', 'R987', 'R988', 'R990', 'R993', 'R994', 'R997']
ref = str(line).replace("R","")
print(ref)
it prints ('2', '5', '6', '7', '10', '12', '13', '15', '18', '20', '21', '22', '23', '24', '26', '30', '31', '32', '33', ...... and so on
Would like to turn to integers(2,5,6,7,10,12,13,15,18... and so on. I have tried but no good results.
Use list comprehension
line = [int(l.replace("R", "")) for l in line]
If "R" is always the first character, you can also do:
line = [int(l[1:]) for l in line]
Per the data provided, both solution above will work solve the original question, if you may have invalid data, for example R32R the above will fail and raise a ValueError exception.
To prevent this you can discard any value that are not an integer by using:
item.isnumeric()
line = [int(l[1:]) for l in line if l[1:].isnumeric()]
You can also achieve the same with a regex expression
re.search('\d+', line[0]).group(0)
line = [int(re.search('\d+', l).group(0)) for l in line if re.search('\d+', l) is not None]
You will need the following import:
import re

How do i replace a specific combination of integer and string?

I'm trying to replace a python list with a deck of cards with symbols.
I have tried using Pythons replace function, but i assume the best solution for this is probably based on regular expression replacement.
The desired result would be this:
"Ah" => "A♥"
"5h" => "5♥"
etc.
Currently the list features items like this:
[Player name], [Player wallet], [1st player card], [2nd player card]
This could be i.e.:
["Don Johnson", 100, "Ks", "5d"]
["Davey Jones", 100, "4c", "3h"]
Any help for this would be greatly appreciated. Thanks.
(Edited for clarification on request - Thanks for all the input so far!)
Here, we can simply use four simple expressions and make that replacement that we wish:
([AKJQ0-9]{1,2})h
([AKJQ0-9]{1,2})d
and similarly the other two.
Demo
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"([AKJQ0-9]{1,2})h"
test_str = ("Ah\n"
"10h")
subst = "\\1♥"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
If you had just a list of cards then it would probably look something like this:
cards = ['2h', '2s', '2c', '2d', '3h', '3s', '3c', '3d', '4h', '4s', '4c', '4d', '5h', '5s', '5c', '5d', '6h', '6s', '6c', '6d', '7h', '7s', '7c', '7d', '8h', '8s', '8c', '8d', '9h', '9s', '9c', '9d', '10h', '10s', '10c', '10d', 'Ah', 'As', 'Ac', 'Ad', 'Kh', 'Ks', 'Kc', 'Kd', 'Jh', 'Js', 'Jc', 'Jd', 'Qh', 'Qs', 'Qc', 'Qd']
If so then just use a dict and a comprehension:
suits = {'h': '♥', 's': '♠', 'c': '♣', 'd': '♦'}
new_cards = [''.join(rank)+suits[suit] for *rank, suit in cards]
Output for this is:
['2♥', '2♠', '2♣', '2♦', '3♥', '3♠', '3♣', '3♦', '4♥', '4♠', '4♣', '4♦', '5♥', '5♠', '5♣', '5♦', '6♥', '6♠', '6♣', '6♦', '7♥', '7♠', '7♣', '7♦', '8♥', '8♠', '8♣', '8♦', '9♥', '9♠', '9♣', '9♦', '10♥', '10♠', '10♣', '10♦', 'A♥', 'A♠', 'A♣', 'A♦', 'K♥', 'K♠', 'K♣', 'K♦', 'J♥', 'J♠', 'J♣', 'J♦', 'Q♥', 'Q♠', 'Q♣', 'Q♦']
For your solution you could define a function that corrects the card:
def fix_card(card):
suits = {'h': '♥', 's': '♠', 'c': '♣', 'd': '♦'}
*rank, suit = card
return ''.join(rank)+suits[suit]
Then just use it like this:
player = ["Don Johnson", 100, "Ks", "5d"]
player[2] = fix_card(player[2])
player[3] = fix_card(player[3])
print(player)
#["Don Johnson", 100, "K♣", "5♦"]
No, regex is not needed for a simple replacement like this. Just use str.replace:
>>> cards = ['Ah', '5h']
>>> [s.replace('h', '♥') for s in cards]
['A♥', '5♥']

For loop vs. class-object... How can I solve it?

today I tried to create a class.
I wrote this.
from first_names_class import firstnames
from last_names_class1 import lastnames
from object_name_list import my_new_list
class MyClass:
"""A Class with automatated full names."""
# Automatically create the instances of the class named user_indexfrom the rangeiterator
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
for i in range(0, 49):
my_new_list[i] = MyClass(firstnames[i], lastnames[i])
print(user1.first_name)
However, when I run it it says:
Traceback (most recent call last):
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50']
File "/Users/myname/Documents/University/Programming for Quantitive Analysis/MyFirstClass.py", line 15, in
print(user1.first_name)
NameError: name 'user1' is not defined
['user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'user7', 'user8', 'user9', 'user10', 'user11', 'user12', 'user13', 'user14', 'user15', 'user16', 'user17', 'user18', 'user19', 'user20', 'user21', 'user22', 'user23', 'user24', 'user25', 'user26', 'user27', 'user28', 'user29', 'user30', 'user31', 'user32', 'user33', 'user34', 'user35', 'user36', 'user37', 'user38', 'user39', 'user40', 'user41', 'user42', 'user43', 'user44', 'user45', 'user46', 'user47', 'user48', 'user49', 'user50']
My thoughts on this:
I don't know if I'm correct but I have read in another thread that the object/instance of the class should not be indented as it will be read "as content of the class".
On the other hand, the for-loop requires me to have this text indented.
How can I solve this problem, if that what I said is exactly the problem.
If what I said, is wrong, how can I then solve this NameError.
Maybe it has something to do with the list from which I take the object-name. As they are strings, maybe that is the trouble-maker.
I'd be thankful for any hint on how I can solve this.
Access elements of your list using index notation:
print(my_new_list[0].first_name)
And there is nothing wrong with your indentation.
You never defined the symbol user1... You only have my_new_list which is of type list, which you can access the elements of using [id].
I used some pseudo lists to demonstrate but with this setup you would access the values like so
first = ['vash', 'billy']
last = ['stampede', 'stack']
class MyClass:
def __init__(self, f_name, l_name):
self.first_name = f_name
self.last_name = l_name
self.full_name = f_name + ' ' + l_name
user = [MyClass(first[idx], last[idx]) for idx, item in enumerate(first)]
print(user[0].first_name)
print(user[1].first_name)
[print(user[idx].full_name) for idx, item in enumerate(user)]
~/python/stackoverflow/9.23$ python3.7 dicta.py
vash
billy
vash stampede
billy stack

'set' object has no attribute 'rstrip'

from random import randint
import os
path = os.path.abspath('')
dosya = open((path + "\\asaldb.txt"), "r")
asalsayilar = dosya.readlines()
mindeger = int(input("Minumum Değer Kaç ? "))
maxdeger = int(input("Maximum Değer Kaç ? "))
min=0
max=0
minasallar = []
maxasallar = []
ortakasal = []
ortakasal2 = []
def maxasal():
global max
if maxdeger>=int(asalsayilar[max]):
maxasallar.append((asalsayilar[max]).rstrip('\n'))
max=max+1
maxasal()
def minasal():
global min
if int(asalsayilar[min])>=mindeger:
try:
(asalsayilar[max]).rstrip('\n')
minasallar.append((asalsayilar[min]).rstrip('\n'))
min=min+1
minasal()
except:
print("Maximum Asallar :")"""
maxasal()
else:
min=min+1
minasal()
minasal()
def common_member(a, b):
a_set = set(a)
b_set = set(b)
if (a_set & b_set):
ortakasal.append((a_set & b_set))
else:
print("No common elements")
common_member(minasallar, maxasallar)
ortakasal.sort()
print((ortakasal[0]).rstrip('{ }'))
Traceback:
Traceback (most recent call last):
File "asal.py", line 54, in <module>
print((ortakasal[0]).rstrip('{ }'))
AttributeError: 'set' object has no attribute 'rstrip'
If I do print (asalortak), it becomes like this: [{'83', '97', '53', '79', '89', '67', '61', '73', '59' }]
If I do print (asalortak [0]), it looks like this:
{'83', '97', '53', '79', '89', '67', '61', '73', '59', '71'}, but I need to separate my numbers.
I do print (asalortak [0]), it looks like this: {'83', '97', '53', '79', '89', '67', '61', '73', '59', '71'}
that's because asalortak [0] is a python set which is confirmed by this error.
print((ortakasal[0]).rstrip('{ }'))
AttributeError: 'set' object has no attribute 'rstrip'
The elements are already "separated". Just iterate on the elements to print them:
for e in ortakasal[0]:
print(e)
in a set, the order isn't stable/fixed. Used sorted to generate a sorted list out of it (sorted alpha, not numerical since numbers are strings)
convert to list:
lst = list(ortakasal[0])
convert to sorted list:
lst = sorted(ortakasal[0])
you can use random.sample(ortakasal[0],2) to select 2 elements at random.

Categories