如何将函数应用于变量输入列表?例如,filter函数返回true值,但不返回函数的实际输出.
from string import upper
mylis=['this is test', 'another test']
filter(upper, mylis)
['this is test', 'another test']
Run Code Online (Sandbox Code Playgroud)
预期的产出是:
['THIS IS TEST', 'ANOTHER TEST']
Run Code Online (Sandbox Code Playgroud)
我知道upper是内置的.这只是一个例子.
对不起,如果问题有点令人困惑.这与此问题类似
我认为上述问题接近我想要的,但在Clojure中.
还有一个问题
我需要这样的东西,但在那个问题中不是'[br]',而是有一个需要搜索和删除的字符串列表.
希望我清楚自己.
我认为这是因为python中的字符串是不可变的.
我有一个需要从字符串列表中删除的干扰词列表.
如果我使用列表理解,我最终会一次又一次地搜索相同的字符串.所以,只有"of"被删除而不是"the".所以我修改后的列表看起来像这样
places = ['New York', 'the New York City', 'at Moscow' and many more]
noise_words_list = ['of', 'the', 'in', 'for', 'at']
for place in places:
stuff = [place.replace(w, "").strip() for w in noise_words_list if place.startswith(w)]
Run Code Online (Sandbox Code Playgroud)
我想知道我在做什么错.
嗨我的问题与很久以前某个用户的帖子有关:
对于我的具体情况.我有一个这样的列表:
appl = ['1', 'a', 'a', '2', 'a']
Run Code Online (Sandbox Code Playgroud)
我想只用一个空的空格(它保存"a"的位置)来替换"a"的"单个未知"实例.我不确定如何只为一个角色做这件事.有人可以帮忙吗?提前致谢.
编辑:我应该提到我需要使用"索引"函数来首先找到"a",因为它是一个变化的变量.然后我需要代替字符的代码.
EDIT2:很多人都假设索引会是2,但我想指出的是该字符的索引是未知的.此外,"a"将在列表中出现约20次(将保留固定数字),但它们的位置将会发生变化.我想根据用户输入替换"a".这是我正在使用的实际列表:
track [2] = ["|","@","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"]
Run Code Online (Sandbox Code Playgroud)
@是一个charachter,| 是一个边界,""是一个空的空间.用户输入输入以选择向右移动的数量,并显示一个新图像,显示@的新位置,并将@的旧位置替换为空格.列表的长度保持不变.这是我的问题的背景.
Stack Overflow 上有很多关于这个一般主题的问答,但它们要么质量很差(通常是初学者的调试问题暗示的),要么以其他方式错过了目标(通常是不够通用)。至少有两种极其常见的方法会使幼稚的代码出错,初学者从关于循环的规范中获益更多,而不是从将问题作为拼写错误或关于打印所需内容的规范中获益。所以这是我尝试将所有相关信息放在同一个地方。
假设我有一些简单的代码,可以对一个值进行计算x并将其分配给y:
y = x + 1
# Or it could be in a function:
def calc_y(an_x):
return an_x + 1
Run Code Online (Sandbox Code Playgroud)
现在我想重复计算 的许多可能值x。我知道for如果我已经有要使用的值列表(或其他序列),我可以使用循环:
xs = [1, 3, 5]
for x in xs:
y = x + 1
Run Code Online (Sandbox Code Playgroud)
while或者,如果有其他逻辑来计算值序列,我可以使用循环x:
def next_collatz(value):
if value % 2 == 0:
return value // 2
else:
return 3 * value + 1
def collatz_from_19():
x = 19
while x != 1:
x …Run Code Online (Sandbox Code Playgroud) 我有一个列表,其中包含从在线网站上抓取的数据。名单是这样的
list1 = ['\nJob Description\n\nDESCRIPTION: Interacts with users and technical team members to analyze requirements and develop
technical design specifications. Troubleshoot complex issues and make recommendations to improve efficiency and accurac
y. Interpret complex data, analyze results using statistical techniques and provide ongoing reports. Identify, analyze,
and interpret trends or patterns in complex data sets. Filter and "clean data, review reports, and performance indicator
s to locate and correct code problems. Work closely with management to prioritize business and information …Run Code Online (Sandbox Code Playgroud) python ×5
list ×2
function ×1
iteration ×1
python-3.x ×1
regex ×1
replace ×1
stop-words ×1
web-scraping ×1