Re: generating unique variable name via loops
4 Kasım 2014 Salı 13:29:34 UTC+2 tarihinde Fatih Güven yazdı: Editted: Grammar revision. Hi, I want to generate a unique variable name for list using python. list1=... list2=... . . . listx=... where x is a number. You can remember it from saving a file in a directory. If you have already created a new file, save dialog suggest that new file is already exist, do you want to save it as new file (1)? so I want to apply it for list names. Do you have any idea about this. -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
Fatih Güven wrote: I want to generate a unique variable name for list using python. list1=... list2=... . . . listx=... where x is a number. You can remember it from saving a file in a directory. If you have already created a new file, save dialog sugget that new file is already exist, do you want to save it as new file (1)? so I want to apply it for list names. Do you any idea about this. Do you really want to use list1, list2, list3, ... as variable names? You shouldn't. Instead use a dict or a list of lists: list_of_lists = [] list_of_lists.append([1, 2, 3]) list_of_lists.append([a, b, c]) list_of_lists.append([foo, bar, baz]) You can then access the second list with letters = list_of_lists[1] print(letters) ['a', 'b', 'c'] On the other hand if you just want to generate names an easy to understand approach is to increment a global variable every time you invoke the name- generating function: _index = 1 def next_name(): ... global _index ... name = list{}.format(_index) ... _index += 1 ... return name ... next_name() 'list1' next_name() 'list2' next_name() 'list3' -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
Fatih Güven wrote: 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?: I want to generate a unique variable name for list using python. list1=... list2=... for x in range(1,10): exec(list%d = [] % x) -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
Veek M wrote: Fatih Güven wrote: 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?: I want to generate a unique variable name for list using python. list1=... list2=... for x in range(1,10): exec(list%d = [] % x) Why would you do this? -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
4 Kasım 2014 Salı 15:19:20 UTC+2 tarihinde Veek M yazdı: Fatih Güven wrote: 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?: I want to generate a unique variable name for list using python. list1=... list2=... for x in range(1,10): exec(list%d = [] % x) This is okay but i can't use the method .append for example list1.append(abc) -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
4 Kasım 2014 Salı 15:37:59 UTC+2 tarihinde Peter Otten yazdı: Veek M wrote: Fatih Güven wrote: 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?: I want to generate a unique variable name for list using python. list1=... list2=... for x in range(1,10): exec(list%d = [] % x) Why would you do this? I have a structured and repetitive data. I want to read a .txt file line by line and classified it to call easily. For example employee1 has a name, a salary, shift, age etc. and employee2 and other 101 employee have all of it. Call employee1.name or employee2.salary and assign it to a new variable, something etc. -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
Fatih Güven wrote: This is okay but i can't use the method .append for example list1.append(abc) works for me -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
- Original Message - From: Fatih Güven mfthgu...@gmail.com I have a structured and repetitive data. I want to read a .txt file line by line and classified it to call easily. For example employee1 has a name, a salary, shift, age etc. and employee2 and other 101 employee have all of it. Call employee1.name or employee2.salary and assign it to a new variable, something etc. Some python 2.7 pseudo code to give you some leads employes = {} with open('file.txt') as f_: for line in f_: name, age, phone = line.split(',') employes[name] = (age, phone) print employes If your file is a csv format, it could even be easier using the csv module. JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
Fatih Güven wrote: 4 Kasım 2014 Salı 15:37:59 UTC+2 tarihinde Peter Otten yazdı: Veek M wrote: Fatih Güven wrote: 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?: I want to generate a unique variable name for list using python. list1=... list2=... for x in range(1,10): exec(list%d = [] % x) Why would you do this? I have a structured and repetitive data. I was actually asking Veek M. I want to read a .txt file line by line and classified it to call easily. For example employee1 has a name, a salary, shift, age etc. and employee2 and other 101 employee have all of it. Call employee1.name or employee2.salary and assign it to a new variable, something etc. I can only repeat my previous advice. Instead of creating variables for employee1, employee2, and so on make a list of employees: $ cat employees.txt Peter,3000 Paul,2000 Mary,1000 $ cat employees.py #!/usr/bin/env python3 import csv class Employee: def __init__(self, name, salary): self.name = name self.salary = salary if __name__ == __main__: employees = [] with open(employees.txt) as f: for row in csv.reader(f): employees.append(Employee(row[0], int(row[1]))) for employee in employees: print(employee.name, -- salary:, employee.salary, doubloons) $ python3 employees.py Peter -- salary: 3000 doubloons Paul -- salary: 2000 doubloons Mary -- salary: 1000 doubloons You wouldn't want to reference Paul as employee2 -- what if the order in the text file changed? Instead you can make a dict that maps name to employee... employees_by_name = {} for employee in employees: name = employee.name if name in employees_by_name: raise ValueError(duplicate name {}.format(name)) employees_by_name[name] = employee and use that dict to look up an employee: while True: name = input(enter a name ) if name == : print(That's all folks) break if name not in employees_by_name: print(unknown name) else: print(Salary:, employees_by_name[name].salary, doubloons) $ python3 employees.py Peter -- salary: 3000 doubloons Paul -- salary: 2000 doubloons Mary -- salary: 1000 doubloons enter a name Peter Salary: 3000 doubloons enter a name Mary Salary: 1000 doubloons enter a name paul unknown name enter a name Paul Salary: 2000 doubloons enter a name That's all folks $ -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
Hi- Questions like this appear so often in various places (mailing lists, forums, sites like Stack Overflow) that I think a very blunt/candid answer is appropriate. This is especially true since there's always someone who responds to the question as-is with some monstrosity of exec() and string formatting, instead of addressing the underlying issue of using the right data structure for what you're trying to accomplish. On 11/04/2014 06:29 AM, Fatih Güven wrote: I want to generate a unique variable name for list using python. list1=... list2=... . . . listx=... where x is a number. *Incorrect.* You do not want to do this. You think you do, but that's presumably because you aren't familiar with common data structures that are available in Python. As Peter Otten said, this is *exactly* the right situation to use a list. You mentioned having structured and repetitive data, wanting to read a .txt file and process each line, and wanting to access each employee's data as appropriate. The general structure of this would be employees = [] with open('employee_data.txt') as f: ... for line in f: ... # parse_line here isn't anything standard or built-in, it's ... # what you would write to transform a line of the data ... # file into whatever object you're interested in with ... # 'name', 'salary', etc. attributes ... employee = parse_line(line) ... employees.append(employee) ... employees[0].salary 15 Your line1 is now employees[0] and so on. Now, you can *easily* answer questions like what's the total salary of all employees? total = 0 for employee in employees: ... total += employee.salary ... total 7502000 (or 'sum(employee.salary for employee in employees)' of course.) MMR... -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
4 Kasım 2014 Salı 17:01:17 UTC+2 tarihinde Peter Otten yazdı: Fatih Güven wrote: 4 Kasım 2014 Salı 15:37:59 UTC+2 tarihinde Peter Otten yazdı: Veek M wrote: Fatih Güven wrote: 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?: I want to generate a unique variable name for list using python. list1=... list2=... for x in range(1,10): exec(list%d = [] % x) Why would you do this? I have a structured and repetitive data. I was actually asking Veek M. I want to read a .txt file line by line and classified it to call easily. For example employee1 has a name, a salary, shift, age etc. and employee2 and other 101 employee have all of it. Call employee1.name or employee2.salary and assign it to a new variable, something etc. I can only repeat my previous advice. Instead of creating variables for employee1, employee2, and so on make a list of employees: $ cat employees.txt Peter,3000 Paul,2000 Mary,1000 $ cat employees.py #!/usr/bin/env python3 import csv class Employee: def __init__(self, name, salary): self.name = name self.salary = salary if __name__ == __main__: employees = [] with open(employees.txt) as f: for row in csv.reader(f): employees.append(Employee(row[0], int(row[1]))) for employee in employees: print(employee.name, -- salary:, employee.salary, doubloons) $ python3 employees.py Peter -- salary: 3000 doubloons Paul -- salary: 2000 doubloons Mary -- salary: 1000 doubloons You wouldn't want to reference Paul as employee2 -- what if the order in the text file changed? Instead you can make a dict that maps name to employee... employees_by_name = {} for employee in employees: name = employee.name if name in employees_by_name: raise ValueError(duplicate name {}.format(name)) employees_by_name[name] = employee and use that dict to look up an employee: while True: name = input(enter a name ) if name == : print(That's all folks) break if name not in employees_by_name: print(unknown name) else: print(Salary:, employees_by_name[name].salary, doubloons) $ python3 employees.py Peter -- salary: 3000 doubloons Paul -- salary: 2000 doubloons Mary -- salary: 1000 doubloons enter a name Peter Salary: 3000 doubloons enter a name Mary Salary: 1000 doubloons enter a name paul unknown name enter a name Paul Salary: 2000 doubloons enter a name That's all folks $ Thanks for your concern, I will try this. Actually, the main focus is that are there any other Paul in my team. So i want to create a simple ID for employee to distinguish two Paul. I belive that you have a solution for this. -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
Fatih Güven wrote: 4 Kasım 2014 Salı 17:01:17 UTC+2 tarihinde Peter Otten yazdı: Fatih Güven wrote: 4 Kasım 2014 Salı 15:37:59 UTC+2 tarihinde Peter Otten yazdı: Veek M wrote: Fatih Güven wrote: 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?: I want to generate a unique variable name for list using python. list1=... list2=... for x in range(1,10): exec(list%d = [] % x) Why would you do this? I have a structured and repetitive data. I was actually asking Veek M. I want to read a .txt file line by line and classified it to call easily. For example employee1 has a name, a salary, shift, age etc. and employee2 and other 101 employee have all of it. Call employee1.name or employee2.salary and assign it to a new variable, something etc. I can only repeat my previous advice. Instead of creating variables for employee1, employee2, and so on make a list of employees: $ cat employees.txt Peter,3000 Paul,2000 Mary,1000 $ cat employees.py #!/usr/bin/env python3 import csv class Employee: def __init__(self, name, salary): self.name = name self.salary = salary if __name__ == __main__: employees = [] with open(employees.txt) as f: for row in csv.reader(f): employees.append(Employee(row[0], int(row[1]))) for employee in employees: print(employee.name, -- salary:, employee.salary, doubloons) $ python3 employees.py Peter -- salary: 3000 doubloons Paul -- salary: 2000 doubloons Mary -- salary: 1000 doubloons You wouldn't want to reference Paul as employee2 -- what if the order in the text file changed? Instead you can make a dict that maps name to employee... employees_by_name = {} for employee in employees: name = employee.name if name in employees_by_name: raise ValueError(duplicate name {}.format(name)) employees_by_name[name] = employee and use that dict to look up an employee: while True: name = input(enter a name ) if name == : print(That's all folks) break if name not in employees_by_name: print(unknown name) else: print(Salary:, employees_by_name[name].salary, doubloons) $ python3 employees.py Peter -- salary: 3000 doubloons Paul -- salary: 2000 doubloons Mary -- salary: 1000 doubloons enter a name Peter Salary: 3000 doubloons enter a name Mary Salary: 1000 doubloons enter a name paul unknown name enter a name Paul Salary: 2000 doubloons enter a name That's all folks $ Thanks for your concern, I will try this. Actually, the main focus is that are there any other Paul in my team. So i want to create a simple ID for employee to distinguish two Paul. I belive that you have a solution for this. The easiest is to use the index: #!/usr/bin/env python3 import csv class Employee: def __init__(self, name, salary): self.name = name self.salary = salary if __name__ == __main__: employees = [] with open(employees.txt) as f: for row in csv.reader(f): employees.append(Employee(row[0], int(row[1]))) for index, employee in enumerate(employees, 1): print(#{}, name: {}.format(index, employee.name)) while True: index = input(enter an index (1...{}) .format(len(employees))) if index == : print(That's all folks) break index = int(index) -1 employee = employees[index] print(Name: {0.name}, Salary: {0.salary}.format(employee)) If the ID must stay the same for two runs of the script you have to put it into the text file as another column. However, you are soon reaching territory where a database is more convenient than a text file. -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
On Tue, 04 Nov 2014 05:45:04 -0800, Fatih Güven wrote: 4 Kasım 2014 Salı 15:19:20 UTC+2 tarihinde Veek M yazdı: Fatih Güven wrote: 4 Kas?m 2014 Sal? 13:29:34 UTC+2 tarihinde Fatih Güven yazd?: I want to generate a unique variable name for list using python. list1=... list2=... for x in range(1,10): exec(list%d = [] % x) This is okay but i can't use the method .append for example list1.append(abc) This is one solution using a dictionary of lists to maintain the name association. It may not be the best method. It may not be the best solution for you. It may not be the answer your instructor is looking for, and it contains deliberate syntax errors. lists = {} for fn in filenames infile = open(fn, r) lists[fn] = [] for line in infile lists[fn].append(line) infile.close() If you have to ask how to do this sort of thing, you probably shouldn't be coding employee data processing systems anyway! If this was a coding assignment for a course, you should have had sufficient instruction in the relevant algorithms and language features to be able to figure it out yourself. In either case, what not explain what you tried, what you expected it to do, and what it actually did. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
On Tue, 04 Nov 2014 05:53:04 -0800, Fatih Güven wrote: Call employee1.name or employee2.salary and assign it to a new variable, something etc. 1) Put the file into a database. 2) database calls -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: generating unique variable name via loops
On 2014-11-04 05:53, Fatih Güven wrote: for x in range(1,10): exec(list%d = [] % x) Why would you do this? I have a structured and repetitive data. I want to read a .txt file line by line and classified it to call easily. For example employee1 has a name, a salary, shift, age etc. and employee2 and other 101 employee have all of it. Call employee1.name or employee2.salary and assign it to a new variable, something etc. -- This sounds remarkably like a CSV or tab-delimited file. If so, the way to do it would be import csv with open(data.txt, rb) as f: dr = csv.DictReader(f) for row in dr: do_something(row[Name], row[salary]) If the file format is more complex, it's often useful to create a generator to simplify the logic: class Person: def __init__(self, name=, salary=0, shift=, ): self.name = name self.salary = salary self.shift = shift def various_person_methods(self, ...): pass def people_from_file(f): build Person objects as you iterate over the file for row in file: person = Person( ... ) yield person with open(data.txt, r): for person in people_from_file(f): do_something(person) You can then reuse that generator with multiple files if you need. -tkc -- https://mail.python.org/mailman/listinfo/python-list