Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any magic python way to get the firsts letters of each name and join with '#company.com' to create a email address?
Here is how I did:
name = ['Elon Reeve Musk']
full_name = [word[0].lower() for word in name[0].split(' ')]
firts_letters = "".join(full_name)
username = '%s#company.com' %(firts_letters)
The result is erm#company.com
Here's some "python magic"
name = ['Elon Reeve Musk']
f"{''.join(filter(str.isupper, name[0]))}#company.com".lower()
>>> erm#company.com
Whether this is better than your method is debatable. Most of the time a few lines of easy to read code is better than a one line hack.
My recommendation would be
name = ['Elon Reeve Musk']
initials = ''.join(word[0] for word in name[0].split())
f'{initials.lower()}#company.com'
>>> erm#company.com
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
im trying to have an input in every line of the displayed data.
here are the codes
def attendance():
print ("\n\nList of Students\n")
print ("StudNo StudName Age Course Attendance")
for idx in range(len(studNo)):
print (" {0:15} {1:10s} {2:10s} {3:10s}".format(studNo[idx],
studName[idx], Age[idx], Course[idx]))
and this is the output
List of Students
StudNo StudName Age Course Attendance
a1001 Daryl 21 CS
a1002 Akex 21 CS
a1003 John 24 CS
a1004 Jose 22 CS
im creating a simple attendance monitoring system. so im trying to have an input for each line so i can write A for absent or P for present.
You can use the input function. input("Write your prompt here") will print the prompt and ask the user to write an entry of text. This can get messy with your desired output, so I suggest collecting all attendance information and then printing
students = ['Kathy','Will','Nayeon']
attendanceRecord = []
for student in students:
attendance = input("What is "+student+"\'s attendance? ")
attendanceRecord.append(attendance)
print("\n") # add a space before printing student list
print("Name"+" Attendace")
for i in range(len(students)):
print(students[i] + " "+ attendanceRecord[i])
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i am trying to make the same functionality using lambda to get the plural of a word, this is my function (i want to write it in lambda form).
import re
def plural(noun):
if re.search("[sxz]$",noun):
return re.sub("$","es",noun)
elif re.search("[^aeioudgkprt]h$",noun):
return re.sub("$","es",noun)
elif re.search("[^aeiou]y$",noun):
return re.sub("y$","ies",noun)
else:
return noun+"s"
i want to make the same function job using lambda ,not sure where to start.
(i am using python 2.7)
import inflect
result = inflect.engine().plural('bike')
import re
rules=((lambda word:re.search('[sxz]$',word),lambda word:re.sub('$','es',word)),
(lambda word: re.search('[^aeioudgkprt]h$', word),lambda word: re.sub('$', 'es', word)),
(lambda word: re.search('[^aeiou]y$', word),lambda word: re.sub('y$', 'ies', word)),
(lambda word: re.search('$', word),lambda word: re.sub('$', 's', word)))
def plural(noun):
for findpattern,rule in rules:
if findpattern(noun):
return rule(noun)
print plural('boy')
This your functions as lambda rules, it will search for pattern if it's true,apply rule
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to write in a file which changes the name with the counter.
for j in range(30):
fileout.write("bla bla")
and fileout should be
file001 if j=1
file002 if j=2
...
...
I cannot format it in the right way.
Anyone can help me?
Thank you
You can try to use the .zfill( < number of digits > ) option. It will complet your string with 0.
For example, this code should work :
radical ='file'
numberOfDigits = 3
for j in range( 30 ) :
fileout = open ( radical + '%s' %(str(j).zfill(numberOfDigits)) + '.txt' , 'w' )
fileout.write('bla bla')
fileout.close()
Not sure if below means what you needed ?
Code below will create file000 - file029 with 'bla bla' inside
for i in range(30):
with open("file%03d" % i, 'w') as f:
f.write('bla bla')
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Could someone help me with following function:
def nagios_chart():
alpha = [chr(item).upper() for item in range(ord('b'), ord('l')+1)]
for idx, column in enumerate(alpha):
print worksheet.write_column('column + 1', nagios_data[idx])
if __name__ == '__main__':
nagios_chart()
I need output like this:
worksheet.write_column('B1', nagios_data[0])
worksheet.write_column('C1', nagios_data[1])
worksheet.write_column('D1', nagios_data[2])
worksheet.write_column('E1', nagios_data[3])
worksheet.write_column('F1', nagios_data[4])
worksheet.write_column('G1', nagios_data[5])
worksheet.write_column('H1', nagios_data[6])
worksheet.write_column('I1', nagios_data[7])
worksheet.write_column('J1', nagios_data[8])
worksheet.write_column('K1', nagios_data[9])
worksheet.write_column('L1', nagios_data[10])
To use the column as a variable, remove it from the quotes so it isn't treated as a string literal. Then you can concatenate it with '1'.
def nagios_chart():
alpha = [chr(item).upper() for item in range(ord('b'), ord('l')+1)]
for idx, column in enumerate(alpha):
print worksheet.write_column(column + '1', nagios_data[idx])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
ID = int(data_key)
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID='ID'")
where i have done wrong...
Thanks..
Use a parameter (examples 1, 2):
ID = int(data_key)
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID=:1", ID)
# or
results = db.GqlQuery("SELECT * FROM PatientMeds WHERE patientinfo_ID=:id", id=ID)
You could also use the more "Pythonic" model API:
from path.to.your.models import PatientMeds
results = PatientMeds.all().filter('patientinfo_ID =', int(data_key))