当前位置:网站首页><No. 8> 1816. Truncate sentences (simple)
<No. 8> 1816. Truncate sentences (simple)
2022-07-07 12:00:00 【Smoked Luoting purple Pavilion】
Catalog
Title Description :
The sentence It's a list of words , Words in the list are separated by a single space , And there are no leading or trailing spaces . Each word is made up of English letters in upper and lower case only ( No punctuation ).
for example ,"Hello World"
、"HELLO"
and "hello world hello world"
It's all sentences .
Here is a sentence for you s And an integer k , Would you please s truncation , send truncation The following sentence contains only front k Word . return truncation s After the sentence .
Python Realization :
class Solution(object):
def truncateSentence(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
list_num = []
list_num = s.split(" ") # Split the original string by spaces
list_final = []
for i in range (k):
list_final.append(list_num[i])
return " ".join(list_final) # take list convert to str
In one line :
class Solution(object):
def truncateSentence(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
return " ".join(s.split(" ")[:k])
边栏推荐
- MySQL安装常见报错处理大全
- 《论文阅读》Neural Approaches to Conversational AI(1)
- 5V串口接3.3V单片机串口怎么搞?
- [Yugong series] go teaching course 005 variables in July 2022
- R语言使用quantile函数计算评分值的分位数(20%、40%、60%、80%)、使用逻辑操作符将对应的分位区间(quantile)编码为分类值生成新的字段、strsplit函数将学生的名和姓拆分
- Test the foundation of development, and teach you to prepare for a fully functional web platform environment
- Internet Protocol
- The road to success in R & D efficiency of 1000 person Internet companies
- STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
- Talk about SOC startup (VII) uboot startup process III
猜你喜欢
Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
[filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]
Camera calibration (2): summary of monocular camera calibration
清华姚班程序员,网上征婚被骂?
There are so many factors that imprison you
Flet tutorial 17 basic introduction to card components (tutorial includes source code)
相机标定(2): 单目相机标定总结
Flet教程之 15 GridView 基础入门(教程含源码)
随机推荐
.NET MAUI 性能提升
SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
[filter tracking] strapdown inertial navigation pure inertial navigation solution matlab implementation
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
Flet教程之 15 GridView 基础入门(教程含源码)
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
Swiftui tutorial how to realize automatic scrolling function in 2 seconds
How much do you know about excel formula?
Electron adding SQLite database
Suggestions on one-stop development of testing life
Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?
[system design] index monitoring and alarm system
EasyUI learn to organize notes
Common locking table processing methods in Oracle
【神经网络】卷积神经网络CNN【含Matlab源码 1932期】
Camera calibration (2): summary of monocular camera calibration
[neural network] convolutional neural network CNN [including Matlab source code 1932]
Neural approvals to conversational AI (1)