'set' object has no attribute 'rstrip' - python

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.

Related

I have problem with my loop list. cant get the expected result

here's my code :
UserList = [['person1', '25yo','70kg','170cm'],[ 'person2','21yo','54kg','164cm']]
ListStrUser = []
for ListStrUser in UserList:
ListStrUser = GetNum(UserList)
def GetNum(anyList):
for i in range(1,len(anyList)):
anyList[i] = re.sub (r'\D',"", str(anyList[i]))
return anyList
print(ListStrUser)
########
expected result :
[['person1', '25','70','170'],[ 'person2','21','54','164']]
You were not far off Asif. But I cannot add much more to Ethan's answer which is why I'm confused that it was down voted. If you want a function that can handle all the work without the need for another for loop then this function below will do just that:
import re
UserList = [['person1', '25yo','70kg','170cm'],[ 'person2','21yo','54kg','164cm']]
def get_num(any_list):
# includes the for loop to iterate through the list of lists
list_str_user = []
for inner_list in any_list:
temp_list = [inner_list[0]]
for i in range(1,len(inner_list)):
temp_list.append(re.sub(r'\D', '', str(inner_list[i])))
list_str_user.append(temp_list)
return list_str_user
print(get_num(UserList))
Output:
[['person1', '25', '70', '170'], ['person2', '21', '54', '164']]
So no need for the for loop outside the function.
import re
def GetNum(anyList):
for i in range(1, len(anyList)):
anyList[i] = re.sub(r'\D[^0-9]',"",str(anyList[i]))
return anyList
userList = [['person1','25yo','70kg','170cm'],['person2','21yo','54kg','164cm']]
for ListStrUser in userList: ListStrUser = GetNum(ListStrUser)
print("Output : ", userList)
output: [['person1', '25', '70', '170'], ['person2', '21', '54', '164']]
from #Guy 's comment:
UserList = [['person1', '25yo','70kg','170cm'],[ 'person2','21yo','54kg','164cm']]
import re
def GetNum(anyList):
for i in range(1,len(anyList)):
anyList[i] = re.sub (r'\D',"", str(anyList[i]))
return anyList
ListStrUser = []
for ListStr in UserList:
ListStrUser.append(GetNum(ListStr))
print(ListStrUser)
gives
[['person1', '25', '70', '170'], ['person2', '21', '54', '164']]
Try the following code:
user_list = [['person1', '25yo','70kg','170cm'],[ 'person2','21yo','54kg','164cm']]
list_str_user = []
def get_num(any_list):
updated_list = [any_list[0]]
for i in range(1,len(any_list)):
updated_list.append(re.sub(r'\D',"", str(any_list[i])))
return updated_list
for user in user_list:
list_str_user.append(get_num(user))
print(list_str_user)
Notice I also updated the naming of your variables and functions and the spacing between functions to be compliant with pep8. Keep this in mind when writing Python code.
Also functions should be defined before you use them otherwise Python won't find them.
I also created the updated_list variable in get_num, it's never a bad idea to not mutate parameters in functions.

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 to save the list in to .txt?

I have to save 'Record' into text file. So I have tried to use name_error_record.write( record[0]+" "+record[1]+" "+record[2]+" "+record[3]) but it doesn't work...
I have to align as 'No Menu_name Num Total' and save it into text file. please help..
The list that I have to save is [['No.', 'Menu_name', 'Num', 'Total'], ['4', 'Smoothie_queen', '4', '12000'], ['10', 'Amorparty', '2', '4000'], ['24', 'Plane_yougurt', '14', '49000'], ['36', 'Cofe_latte', '18', '45000'], ['51', 'Cofe_latte', '16', '40000'], ['53', 'Berry_smudie', '17', '51000'], ['54', 'American_air', '4', '8000']]
and also it has to be aligned by columns as 'No Menu name Num Total'.
I think I am missing something but I can't find them.. help T.T
def error_check(date):
#========= STEP 3 ==========
Record = []
errormenu = []
recordfile = open("ledger_"+date+".txt","r")
errormenufile = open("menu.txt", "r")
for error in errormenufile:
menu = error.split()
errormenu.append(menu[0])
errormenufile.close()
for line in recordfile:
record = line.split()
Record.append(record)
Record = [x for x in Record if errormenu[0] not in x]
Record = [x for x in Record if errormenu[1] not in x]
Record = [x for x in Record if errormenu[2] not in x]
Record = [x for x in Record if errormenu[3] not in x]
Record = [x for x in Record if errormenu[4] not in x]
name_error_record = open("ledger_"+date+"_name_error.txt","r")
for record in Record:
name_error_record.write( record[0]+" "+record[1]+" "+record[2]+" "+record[3])
name_error_record.close()
#========= STEP 3 ==========
Simply change 'r' to 'w' in:
open("ledger_"+date+"_name_error.txt","r")
open("ledger_"+date+"_name_error.txt","w")
by setting 'w' you open file for writing
you can add '\n' at the end of saved line in:
name_error_record.write( record[0]+" "+record[1]+" "+record[2]+" "+record[3]+'\n')
to save data line by line
l=[['No.', 'Menu_name', 'Num', 'Total'], ['4', 'Smoothie_queen', '4', '12000'], ['10', 'Amorparty', '2', '4000'], ['24', 'Plane_yougurt', '14', '49000'], ['36', 'Cofe_latte', '18', '45000'], ['51', 'Cofe_latte', '16', '40000'], ['53', 'Berry_smudie', '17', '51000'], ['54', 'American_air', '4', '8000']]
filename='' # add filenmae
with open(filename, 'w+') as file:
for i in l:
file.write('{} {} {} {}\n'.format(*i))

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

How do you append multiple keys to a value in a list?

It works, but how can I add multiple values to a key?
This is my code:
for x in range(number_of_wall):
wall = 1
wall_area = input("What is the area of wall {}" .format(wall) + " in " + room_name + ": ")
dimention.append([room_name, wall_area])
wall = wall + 1
print(dimention)
And this is what it comes up with:
[['Lounge', '13'], ['Lounge', '13'], ['Lounge', '13'], ['Lounge', '13'], ['Bedroom', '14'], ['Bedroom', '14'], ['Bedroom', '14'], ['Bedroom', '14']]
How can I modify my code so it comes up with:
[['Lounge': '13', '13', '13', '13'], ['Bedroom': '14', '14', '14', '14']]
Or something along those lines. Thanks.
You have to use dictionary in place of list for dimention:
dimention = {}
for x in range(number_of_wall):
wall = 1
wall_area = input("What is the area of wall {}" .format(wall) + " in " + room_name + ": ")
if room_name not in dimention:
dimention[room_name] = [wall_area]
else:
dimention[room_name].append(wall_area)
wall = wall + 1
print(dimention)
In this case, check for room_name present in the dimention if not add room_name as key and assign first value of wall_area; otherwise append wall_area to the existing room_name list.
You can also use set instead of dictionary.

Categories