Tôi muốn làm như sau trong mã của mình:
- In từng từ của một chuỗi trong một dòng mới
- In từng ký tự trong một dòng mới
Tôi đã có thể đạt được phần thứ hai bằng cách sử dụng mã sau:
s2 = "I am testing"
for x in s2:
print x
Tôi đang gặp khó khăn trong việc đạt được phần đầu tiên. Tôi đã viết mã sau đây nhận ra nơi có không gian trong chuỗi.
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
Cũng đã thử các loại bên dưới trong đó có tất cả các không gian của chuỗi:
s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
Nhưng vẫn không đạt được đầu ra mong muốn của tôi, đó là một cái gì đó như:
I
am
testing
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Bàn luận: sky is blue in color
Đưa ra một chuỗi, nhiệm vụ là viết một chương trình Python để in từ cuối cùng trong chuỗi đó.: color
Đầu vào: Sky có màu xanh lam color is last word in the sentence.
Đầu ra: Màu sắc: Learn algorithms at geeksforgeeks
Giải thích: Màu sắc là từ cuối cùng trong câu.: geeksforgeeks
Đầu vào: Tìm hiểu các thuật toán tại Geekforgeeks geeksforgeeks is last word in the sentence.
Đầu ra: GeekSforGeeksUsing For loop + String Concatenation
- Giải thích: Geekforgeek là từ cuối cùng trong câu.
- Cách tiếp cận số 1: Sử dụng cho Loop + String Concatenationnewstring.
- Quét câunewstring using string concatenation.
- Lấy một chuỗi trống, báo chí.
- Traverse Chuỗi theo thứ tự ngược lại và thêm ký tự vào báo chí bằng cách ghép chuỗi.newstring and return it [it is the last word in the sentence].
Dưới đây là việc thực hiện phương pháp trên:
Python3
def
lastWord[string]:
5
lastWord[string]:
7
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
7
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
9 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
0s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
2
5for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
6
7I
am
testing
2lastWord[string]:
7geeksforgeeks22____11
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
022
geeksforgeeks0
lastWord[string]:
7for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
07for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
07 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
6for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
10def
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
12I
am
testing
2geeksforgeeks6
geeksforgeeks7
Minimum average ascii word = beginning Maximum average ascii word = every2
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 Minimum average ascii word = beginning Maximum average ascii word = every4
Minimum average ascii word = beginning Maximum average ascii word = every5
I
am
testing
7Minimum average ascii word = beginning Maximum average ascii word = every7
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
21for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
7geeksforgeeks7
geeksforgeeks8
Output:
geeksforgeeks
25for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
7 Using split[] methods3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
29for i in s2: if not[i.isspace[]]: print i else: print "space"
1for i in s2: if not[i.isspace[]]: print i else: print "space"
4s3 = ''.join[[i for i in s2 if not[i.isspace[]]]] print s3
5s3 = ''.join[[i for i in s2 if not[i.isspace[]]]] print s3
34for i in s2: if not[i.isspace[]]: print i else: print "space"
1for i in s2: if not[i.isspace[]]: print i else: print "space"
36split[].for i in s2: if not[i.isspace[]]: print i else: print "space"
9for i in s2: if not[i.isspace[]]: print i else: print "space"
0s3 = ''.join[[i for i in s2 if not[i.isspace[]]]] print s3
1s3 = ''.join[[i for i in s2 if not[i.isspace[]]]] print s3
2s3 = ''.join[[i for i in s2 if not[i.isspace[]]]] print s3
6for i in s2: if not[i.isspace[]]: print i else: print "space"
44for i in s2: if not[i.isspace[]]: print i else: print "space"
2I am testing
46for i in s2: if not[i.isspace[]]: print i else: print "space"
1for i in s2: if not[i.isspace[]]: print i else: print "space"
48for i in s2: if not[i.isspace[]]: print i else: print "space"
Dưới đây là việc thực hiện phương pháp trên:
Python3
def
lastWord[string]:
5
lastWord[string]:
7
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
7
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
9 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
0s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
2
5for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
6
7I
am
testing
2lastWord[string]:
7geeksforgeeks22____11
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
022
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
21for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
7
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
25for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
7Output:
geeksforgeeks
for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
29for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
4s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
5
34for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
1 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
36O[n]for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
9 for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
0s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
1 s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
2s3 = ''.join[[i for i in s2 if not[i.isspace[]]]]
print s3
5
6for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
44O[n]for i in s2:
if not[i.isspace[]]:
print i
else:
print "space"
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
ĐọcS of length N, representing a sentence, the task is to print the words with the highest and lowest average of ASCII values of characters.
Examples:
Bàn luận S = “every moment is fresh beginning”
Output:
Word with minimum average ASCII values is “beginning”.
Word with maximum average ASCII values is “every”.
Explanation:
The average of ASCII values of characters of the word “every” = [[101+118+101+114+121]/5 =111]
The average of ASCII values of characters of the word “moment” =: [[ 109+111+109+101+110+116]/6 = 109.33333]
The average of ASCII values of characters of the word “is” = [[105+115]/2 =]
The average of ASCII values of characters of the word “fresh” = [[102+114+101+115+104]/5 = 110]
The average of ASCII values of characters of the word “beginning” = [[98+101+103+105+110+110+105+110+103]/9 =105].
Therefore, the word with minimum of average of ASCII values is “beginning” and maximum of average ASCII values is “every”.Đưa ra một chuỗi s có độ dài n, đại diện cho một câu, nhiệm vụ là in các từ có giá trị ASCII trung bình cao nhất và thấp nhất của các ký tự. S = “sky is blue”
Output:
Word with minimum average ASCII values is “blue”.
Word with maximum average ASCII values is “sky”.
Đầu vào: s = Hồi Mỗi khoảnh khắc là đầu ra mới bắt đầu: từ có giá trị ASCII trung bình tối thiểu là bắt đầu. Từ với giá trị ASCII trung bình tối đa = [[101+118+101+114+121]/5 = 111] Trung bình của các giá trị ASCII của các ký tự của từ & nbsp; = 109.33333] Trung bình các giá trị ASCII của các ký tự của từ & nbsp; +115+104]/5 = 110] Trung bình của các giá trị ASCII của các ký tự của từ & nbsp; Do đó, từ có tối thiểu trung bình của các giá trị ASCII là bắt đầu và tối đa các giá trị ASCII trung bình là một số người khác.The idea is to use the split[] function. Follow the steps below to solve the problem:
- Đầu vào: S = Sky Sky là đầu ra màu xanh lam: Word có giá trị ASCII trung bình tối thiểu là & nbsp;list, say lis[].
- Cách tiếp cận: Ý tưởng là sử dụng hàm chia []. Thực hiện theo các bước dưới đây để giải quyết vấn đề:maxi, mini, maxId, and minId,to store the maximum of average of ASCII values, the minimum average ASCII value, the index of the word with the maximum average ASCII value in the list lis, and the index of the word with the minimum average ASCII value in the list lis[] respectively.
- Tách tất cả các từ của chuỗi được phân tách bằng khoảng trắng bằng hàm chia []. Lưu trữ nó trong một danh sách, nói LIS [].averageValue[], to find the average ASCII value of a string.
- Khởi tạo bốn biến nói Maxi, Mini, MaxID và MinID, để lưu trữ tối đa giá trị ASCII trung bình, giá trị ASCII trung bình tối thiểu, chỉ số của từ có giá trị ASCII trung bình tối đa trong danh sách LIS và chỉ số của Từ với giá trị ASCII trung bình tối thiểu trong danh sách LIS [] tương ứng.lis[] and perform the following operations:
- Xác định một hàm, giả sử trung bình [], để tìm giá trị ASCII trung bình của một chuỗi. ithword in the list lis[],and store it in a variable, say curr.
- Traverse danh sách LIS [] và thực hiện các hoạt động sau:curr> maxi, then update maxi as maxi = curr and assign maxId = i.
- Đối với mỗi ithword trong danh sách lis [], và lưu trữ nó trong một biến, giả sử Curr.curr< mini, then update mini as mini = curr and assign minId = i.
- Nếu curr> maxi, thì hãy cập nhật maxi dưới dạng maxi = curr và gán maxID = i.lis[minId] and lis[maxId] with minimum and maximum average of ASCII value of its characters.
Nếu curr