如何在Python中删除本地文件夹的内容?
目前的项目适用于Windows,但我也希望看到*nix.
filter,map并且reduce在Python 2中完美地工作.这是一个例子:
>>> def f(x):
return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]
>>> def cube(x):
return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> def add(x,y):
return x+y
>>> reduce(add, range(1, 11))
55
Run Code Online (Sandbox Code Playgroud)
但是在Python 3中,我收到以下输出:
>>> filter(f, range(2, 25))
<filter object at 0x0000000002C14908>
>>> map(cube, range(1, 11))
<map object at 0x0000000002C82B70> …Run Code Online (Sandbox Code Playgroud) 我有一个脚本,它读取一个文本文件,将十进制数字作为字符串拉出来并将它们放入一个列表中.
所以我有这个清单:
['0.49','0.54','0.54','0.54','0.54','0.54','0.55','0.54','0.54','0.54','0.55','0.55',' 0.55','0.54','0.55','0.55','0.54','0.55','0.55','0.54']
如何将列表中的每个值从字符串转换为浮点数?
我试过了:
for item in list:
float(item)
Run Code Online (Sandbox Code Playgroud)
但这似乎对我不起作用.
我必须在表格中列出大量的单词:
['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
Run Code Online (Sandbox Code Playgroud)
然后使用条带功能,将其转换为:
['this', 'is', 'a', 'list', 'of', 'words']
Run Code Online (Sandbox Code Playgroud)
我以为我写的东西会起作用,但我一直都会收到错误说:
"'list'对象没有属性'strip'"
这是我试过的代码:
strip_list = []
for lengths in range(1,20):
strip_list.append(0) #longest word in the text file is 20 characters long
for a in lines:
strip_list.append(lines[a].strip())
Run Code Online (Sandbox Code Playgroud) 我有兴趣了解Python 3.x的新语言设计.
我喜欢在Python 2.7中使用的功能map:
Python 2.7.12
In[2]: map(lambda x: x+1, [1,2,3])
Out[2]: [2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
但是,在Python 3.x中,事情发生了变化:
Python 3.5.1
In[2]: map(lambda x: x+1, [1,2,3])
Out[2]: <map at 0x4218390>
Run Code Online (Sandbox Code Playgroud)
我理解如何,但我找不到为什么的参考.为什么语言设计师会做出这样的选择,在我看来,这会引入很多痛苦.这是为了让开发人员坚持列表理解吗?
IMO,list自然可以被认为是Functors ; 而且我一直被认为以这种方式思考:
fmap :: (a -> b) -> f a -> f b
Run Code Online (Sandbox Code Playgroud) 我有一个包含版本字符串的列表,例如:
versions_list = ["1.1.2", "1.0.0", "1.3.3", "1.0.12", "1.0.2"]
Run Code Online (Sandbox Code Playgroud)
我想对它进行排序,结果将是这样的:
versions_list = ["1.0.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]
Run Code Online (Sandbox Code Playgroud)
数字的优先顺序显然应该是从左到右,它应该是降序.所以1.2.3来之前2.2.3和2.2.2之前2.2.3.
我如何在Python中执行此操作?
为什么以下脚本会出错:
payIntList[i] = payIntList[i] + 1000
TypeError: 'map' object is not subscriptable
payList = []
numElements = 0
while True:
payValue = raw_input("Enter the pay amount: ")
numElements = numElements + 1
payList.append(payValue)
choice = raw_input("Do you wish to continue(y/n)?")
if choice == 'n' or choice == 'N':
break
payIntList = map(int,payList)
for i in range(numElements):
payIntList[i] = payIntList[i] + 1000
print payIntList[i]
Run Code Online (Sandbox Code Playgroud) 如何将函数应用于变量输入列表?例如,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是内置的.这只是一个例子.
在Python 3中运行此代码后:
import pdb
def foo():
nums = [1, 2, 3]
a = 5
pdb.set_trace()
foo()
Run Code Online (Sandbox Code Playgroud)
以下表达式有效:
(Pdb) print(nums)
[1, 2, 3]
(Pdb) print(a)
5
(Pdb) [x for x in nums]
[1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
但是以下表达式失败:
(Pdb) [x*a for x in nums]
*** NameError: global name 'a' is not defined
Run Code Online (Sandbox Code Playgroud)
以上在Python 2.7中运行良好.
这是一个错误还是我遗失了什么?
更新:查看新接受的答案.这确实是一个错误(或有问题的设计),现在通过在pdb中引入新的命令和模式来解决.
我有一些数字列表:
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
[3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
我如何添加这些列表的元素,假设我使用的所有列表都是相同的长度?
以下是我希望从上述列表中获得的输出类型.
[6, 9, 12, 15, 18]
Run Code Online (Sandbox Code Playgroud)
我知道我需要一种循环 - 但我怎么能优雅地做到这一点?