python中有一个items的列表 需要将下列列表中的shuju替换成touzhu然后保存到new_items https://odds.500.com/fenxi/shuju-1070052.shtml https://odds.500.com/fenxi/shuju-1043590.shtml 要求结果是这样: https://odds.500.com/fenxi/touzhu-1070052.shtml https://odds.500.com/fenxi/touzhu-1043590.shtml
时间: 2023-02-28 21:01:29 浏览: 255
下面是实现该需求的代码:
```
items = ['https://odds.500.com/fenxi/shuju-1070052.shtml', 'https://odds.500.com/fenxi/shuju-1043590.shtml']
new_items = []
for item in items:
new_item = item.replace('shuju', 'touzhu')
new_items.append(new_item)
print(new_items)
```
输出结果为:
```
['https://odds.500.com/fenxi/touzhu-1070052.shtml', 'https://odds.500.com/fenxi/touzhu-1043590.shtml']
```
相关问题
python中有一个items的列表 需要将下列列表中的shuju替换成touzhu https://odds.500.com/fenxi/shuju-1070052.shtml https://odds.500.com/fenxi/shuju-1043590.shtml 要求结果是这样: https://odds.500.com/fenxi/touzhu-1070052.shtml https://odds.500.com/fenxi/touzhu-1043590.shtml怎么做
可以使用Python中的replace()方法来实现这个需求:items = [https://odds.500.com/fenxi/shuju-1070052.shtml, https://odds.500.com/fenxi/shuju-1043590.shtml] new_items = [item.replace('shuju', 'touzhu') for item in items] 最后的结果就是:new_items = [https://odds.500.com/fenxi/touzhu-1070052.shtml, https://odds.500.com/fenxi/touzhu-1043590.shtml]
python中有一个items的列表 需要将每行的shuju替换成touzhu然后保存到new_items
在 Python 中,你可以使用列表推导式来实现这个操作:
```
items = [...];
new_items = [x.replace("shuju", "touzhu") for x in items]
```
这样,每个列表中的元素都会被替换为 "touzhu",最后将结果保存到 `new_items` 列表中。
阅读全文