我试图将使用Python 2.7.2正常工作的程序转换为Python 3.1.4.
我正进入(状态
TypeError: Str object not callable for the following code on the line "for line in lines:"
Run Code Online (Sandbox Code Playgroud)
码:
in_file = "INPUT.txt"
out_file = "OUTPUT.txt"
##The following code removes creates frequencies of words
# create list of lower case words, \s+ --> match any whitespace(s)
d1=defaultdict(int)
f1 = open(in_file,'r')
lines = map(str.strip(' '),map(str.lower,f1.readlines()))
f1.close()
for line in lines:
s = re.sub(r'[0-9#$?*><@\(\)&;:,.!-+%=\[\]\-\/\^]', " ", line)
s = s.replace('\t',' ')
word_list = re.split('\s+',s)
unique_word_list = [word for word in word_list] …Run Code Online (Sandbox Code Playgroud) 我有一个元组列表
b = [('676010', '5036043'), ('771968', '4754525'), ('772025', '4754525'), ('772072', '4754527'), ('772205', '4754539'), ('772276', '4754542'), ('772323', '4754541'), ('647206', '5036049')]
Run Code Online (Sandbox Code Playgroud)
我正在使用map()它们将它们转换为类型float.
In [45]: print [map(float,e) for e in b]
[[676010.0, 5036043.0], [771968.0, 4754525.0], [772025.0, 4754525.0], [772072.0, 4754527.0], [772205.0, 4754539.0], [772276.0, 4754542.0], [772323.0, 4754541.0], [647206.0, 5036049.0]]
Run Code Online (Sandbox Code Playgroud)
如何对元素应用数学运算map(),比方说除以100,000?
我知道以不同的方式到达那里,但我如何实现这一目标map()?
In [46]: print [(float(tup[0])/100000,float(tup[1])/100000) for tup in b]
[(6.7601, 50.36043), (7.71968, 47.54525), (7.72025, 47.54525), (7.72072, 47.54527), (7.72205, 47.54539), (7.72276, 47.54542), (7.72323, 47.54541), (6.47206, 50.36049)]
Run Code Online (Sandbox Code Playgroud) 我需要在map函数中使用条件语句
我正在复制a path d中a的每个单值,SVG但我不希望对象M和L数组发生这种情况
以下是数组作为字符串的示例.
M 175 0 L 326.55444566227675 87.50000000000001 L 326.55444566227675 262.5 L 175 350 L 23.445554337723223 262.5 L 23.44555433772325 87.49999999999999 L 175 0
Run Code Online (Sandbox Code Playgroud)
这是我没有条件语句的例子
let neWd = array.map(x => { return x * 2; }).reverse().join(' ')
Run Code Online (Sandbox Code Playgroud)
我怎么能在e6中写下这个?我不希望元素L和M(类似的if x ? 'M' : 'L' return)发生乘法运算
我想将resolve_on应用于列表c1中的每个元素,比如
for(Char c:c1){
resolve_on c c1 c2;}
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能使用map函数呢?
resolvents :: [Char] -> [Char] -> [[Char]]
resolvents c1 c2 = map (//what should I do) c1
resolve_on :: Char -> [Char] -> [Char] -> [Char]
resolve_on c c1 c2
Run Code Online (Sandbox Code Playgroud) 所以我的代码是:
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
myArray: [
{id:1, continent:[
{id:1, name: 'Russia'},
{id:2, name: 'Germany'},
{id:3, name: 'Poland'}
]},
{id:2, continent:[
{id:1, name: 'China'},
{id:2, name: 'India'},
{id:3, name: 'Bangladesh'}
]},
{id:3, continent:[
{id:1, name: 'Brazil'},
{id:2, name: 'Peru'},
{id:3, name: 'Chile'}
]}
],
otherValue: 23
}
}
subBoardList(id){
return this.state.myArray[id].map((continent) => {
return(
<View>
<Text>{continent.name}</Text>
</View>
)
})
}
render() {
return (
{this.subBoardList(2)}
);
}
}
Run Code Online (Sandbox Code Playgroud)
我想显示将 id 传递给subBoardList函数的大洲数组的内容。我怎么做? …
我们希望在 a 上运行一个函数,Result或者如果结果实际上是一个错误,则有一个默认值。就像是:
let is_dir = entry.file_type().is_dir() || false
// ^--- file_type() returns a Result
// ^--- default to false if Result is an IOError
Run Code Online (Sandbox Code Playgroud)
目前,我们正在这样做:
let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
Run Code Online (Sandbox Code Playgroud)
但这似乎非常令人困惑,对map单个项目结果运行 a 。有更好的方法吗?
我有两个问题,使用map和length第一个应该给我回字数,但它只计算列表中的元素.
countWords :: [String]-> Int
countWords xs = length (map (words) xs)
countWords ["asd qwe", "-- Foo", "", "\thello world "] => 4--instead it have six words
Run Code Online (Sandbox Code Playgroud)
第二个是棘手的,因为它应该为整个列表返回一个int.我只计算各个元素的字符,而不是整数.
countChars :: [String]-> [Int] --it should be Int
countChars xs = map (\w -> length (w)) xs
countChars ["asd qwe", "-- Foo", "", "\thello world "] => [8,6,0,13]--it should give back the sum of this list which is 27
Run Code Online (Sandbox Code Playgroud) 我打算编写一个map函数,该函数本质上需要一个变量和一个列表,并返回一个列表。
我尝试使用标准地图,但是从这里我看到的是它在格式“地图功能列表”中时,我试图传递另一个参数,这是另一点。
data Point = {xCoord :: Int,
yCoord :: Int}
movePoint :: Point -> Point -> Point
movePoint (Point x y) (Point xMove yMove)
= Point (x + xMove) (y + yMove)
// Add a "vector" to a list of points
movePoints :: [Point] -> Point -> [Point]
movePoints = error "Not yet"
Run Code Online (Sandbox Code Playgroud)
例如,如果我有一个矢量,例如(2,2),并且我有一个点列表,例如[(-2,1),(0,0),(5,4)等],我想使用映射以将(2,2)添加到列表中的所有点并返回点列表,我不确定该怎么做。我是Haskell的新手,所以任何提示都很棒。
我正在尝试将函数映射到 Map (来自Data.Map)实例以将其转换为新类型的 Map。具体来说,我有两种类型的地图:
type Scope = Map.Map String AExpr
type Row = Map.Map String Value
Run Code Online (Sandbox Code Playgroud)
映射AExpr到Value(给定它的第一个参数 Scope )的函数:
evalAExpr :: Scope -> AExpr -> Value
Run Code Online (Sandbox Code Playgroud)
还有一个 type 的实例Scope,比如说x,我想为它映射函数evalAExpr以获得一个 type 的实例Row。
根据文档,这应该可以简单地使用:
map :: (a -> b) -> Map k a -> Map k b
Run Code Online (Sandbox Code Playgroud)
所以在我的情况下,这将是:
x :: Scope
evalAExpr :: Scope -> AExpr -> Value
y = map (evalAExpr x) x :: Row …Run Code Online (Sandbox Code Playgroud) 这是我在编辑器上编辑并在 shell 上编译的代码。
如果我输入整数19,当我打印出c时,它仍然['1','9']不是[1,9]我想要的。我在交互式解释器上尝试了这个,而不是编译 python 文件,它起作用了。
a = raw_input("Please enter a positive number ")
c = []
c = list(a)
map(int, c)
Run Code Online (Sandbox Code Playgroud) 我有一个数组,对于这个数组的每个元素,我需要fetch一些数据(取决于元素)并将这些数据添加到数组中的元素中。
举个例子,我将fetch用一个 Promise来模拟(在现实生活中,这将是一个网络服务的答案):
let p = new Promise((resolve, reject) => resolve('x'))
let a = ['a', 'b']
a = a.map(x => p.then(y => x + y))
console.log(a)Run Code Online (Sandbox Code Playgroud)
我期望的是第一个元素 ( a)p被调用,并在解析后将结果添加到a(giving ax)。对于b.
最终我期待一个新的数组['ax', 'bx']。
我得到的是一系列 Promises
作为 Promises 的新手(我发现理论上这很棒)我很难理解这里出了什么问题。是否可以在其中组合.map()异步操作?
总的来说,我对 Haskell 和函数式编程还很陌生。这似乎是一个如此简单的问题,但我在语法上苦苦挣扎。我想将一个整数列表作为输入,如果它为空,则返回一个空列表,如果它不为空,则使用 map 函数向每个元素添加五个。
到目前为止,这是我的代码,但它会产生很多错误。有什么建议?
addFive :: [a] -> [a]
addFive [] = []
addFive a = map(+5)
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的列表
['111', '222', '333', 'xxx', '3233']
如果可能的话,我想将所有元素更改为int。在本例中,xxx不是数字。那么我怎样才能忽略'xxx'并将所有其他元素更改为int?
我可以使用 for 循环和一些 if 语句来完成此操作。但如果可能的话,我更喜欢使用map函数。任何其他方便的方式将不胜感激。
map-function ×13
haskell ×5
list ×5
python ×4
javascript ×3
string ×2
arity ×1
arrays ×1
casting ×1
dictionary ×1
division ×1
es6-promise ×1
idioms ×1
if-statement ×1
integer ×1
lambda ×1
promise ×1
python-2.7 ×1
react-native ×1
reactjs ×1
rust ×1
typeerror ×1
types ×1