{{1}}
问题:对于My_List中的每个单词,如果单词的结尾以“ e”结尾,则在单词的末尾添加“ d”。否则,请添加“ ed”以使其超过时态。将这些过去时词保存到一个名为Past_Tense的列表中。
答案 0 :(得分:1)
因此,这里有一些入门方法:
for word in My_List:
# do something with your word
word[-1]
为您提供word
的最后一个字符。
您可以像这样将字符串连接在一起:"{} something else here".format(word)
将它们按逻辑顺序排列在一起,您可以自己解决问题!哪个更有趣!
答案 1 :(得分:1)
这是一种实现方法:
My_List = ["adopt", "bake", "beam"]
res = [word + 'd' if word[-1] == 'e' else word + 'ed' for word in My_List]
# ['adopted', 'baked', 'beamed']
答案 2 :(得分:1)
您可以使用列表推导和f-strings
将d
添加到以e
结尾的字符串中,可以使用endswith
str
进行检查方法:
[f'{i}d' if i.endswith('e') else f'{i}ed' for i in My_List]
# ['adopted', 'baked', 'beamed']
对于python版本3.6 <
,请使用:
['{}d'.format(i) if i.endswith('e') else '{}ed'.format(i) for i in My_List]
答案 3 :(得分:0)
尝试一下:
Past_Tense = [k+'d' if k.endswith('e') else k+'ed' for k in My_List]
输出:
['adopted', 'baked', 'beamed']
答案 4 :(得分:0)
Past_Tense = [ w + "e"*(w[-1]!="e") + "d" for w in My_List ]
答案 5 :(得分:0)
// NavigationBar.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';
import { Navbar, NavDropdown } from 'react-bootstrap';
import { logOutUser } from '../../actions/UserActions';
import NavBarItems from '../../components/NavbarItems/NavbarItems';
class NavBar extends Component {
state = {
isLogOut: false
}
componentDidUpdate(prevProps) {
if (prevProps.user.logout !== this.props.user.logout) {
this.setState({isLogOut: true});
}
}
handleSingnOutOnclick = () => {
this.props.dispatch(logOutUser());
}
render() {
let loggedUser = localStorage.getItem('userName');
return(
<div>
{
this.state.isLogOut ?
<Redirect to="/login" /> :
<div className="main-navbar">
<Navbar className="navbar-height">
<Navbar.Collapse id="basic-navbar-nav">
<NavBarItems />
</Navbar.Collapse>
<NavDropdown
title={loggedUser}
id="basic-nav-dropdown"
className="nav-anchor nav-dropdown"
>
<NavDropdown.Item>Profile</NavDropdown.Item>
<NavDropdown.Item onClick={() => this.handleSingnOutOnclick()}>
Log Out
</NavDropdown.Item>
</NavDropdown>
</Navbar>
<div className="navbar-bottom-line"></div>
</div>
}
</div>
);
}
}
const mapStateToProps = (state) => {
return {
user: state.user
}
}
export default connect(mapStateToProps)(NavBar);
// NavbarItems.js
import React, { Component } from 'react';
import { Nav } from 'react-bootstrap';
import config from '../../config/config';
import MainNavItem from '../NavItem/MainNavItem';
class NavBarItems extends Component {
getNavItems = () => {
const navItems = config.navItems;
return navItems.map((item, i) => {
return (
<MainNavItem
key={i}
currentIndex={i}
item={item}
/>
);
});
}
render() {
return (
<Nav className="mr-auto">
{this.getNavItems()}
</Nav>
);
}
}
export default NavBarItems;
// MainNavItem.js
import React from 'react';
import { NavLink } from 'react-router-dom';
const MainNavItem = (props) => {
const {
item,
} = props;
return (
<NavLink
exact
to={item.link}
activeClassName="active-item"
className="nav-anchor"
>
<li key={item.link} >
<span>{item.text}</span>
</li>
</NavLink>
);
}
export default MainNavItem;
答案 6 :(得分:0)
为喜欢的人打高尔夫球。不过不要在生产中这样做。您将永远永恒。我不承担任何责任:)
>>> l
['adopt', 'bake', 'beam']
>>> x = [x+['ed', 'd'][x.endswith('e')] for x in l]
>>> x
['adopted', 'baked', 'beamed']
答案 7 :(得分:0)
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense=[]
for word in words:
if word[-1]=='e':
past_tense=[word+'d']
print(past_tense)
else:
past_tense=[word+'ed']
print(past_tense)
答案 8 :(得分:0)
我想这就是像我这样的初学者如何做的事情:
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
length = len(words)
past_tense = []
strLen = 0
for i in range(0, length):
str = words[i]
strLen = len (str)
if str[-1] == 'e':
words[i] = words[i] + 'd'
else:
words[i] = words[i] + 'ed'
past_tense = past_tense + [words[i]]
print(past_tense)
答案 9 :(得分:0)
问题:对于单词中的每个单词,如果单词以“ e”结尾以使其超过时态,则在单词的末尾添加“ d”。否则,请添加“ ed”以使其超过时态。将这些过去时词保存到一个名为past_tense的列表中。
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for word in words:
if word[-1] == "e":
past_tense.append(word + "d")
else:
past_tense.append(word + "ed")
print(past_tense)
答案 10 :(得分:0)
另一种完成收集的方法。通过map()
函数:
My_List = ["adopt", "bake", "beam"]
def ed(word): return word+"d" if word[-1]=="e" else word+"ed"
Past_Tense = list(map(ed, My_List)) # ['adopted', 'baked', 'beamed']
答案 11 :(得分:-1)
这是回答这个问题的正确方法...
wrds = ["end", 'work', "play", "start", "walk", "look", "open", "rain", "learn", "clean"]
past_wrds = []
for i in wrds:
past_wrds.append(i+'ed')