在Python中我想创建由集合组成的集合,因此我得到一组集合(嵌套集合).
例:
{{1,2}, {2,3}, {4,5}}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试以下内容时:
s = set()
s.add(set((1,2)))
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
s.add(set((1,2)))
TypeError: unhashable type: 'set'
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我的错误在哪里以及我如何实现目标?
我[[Int]]在Haskell中有一个2D列表,我想检查两件事:
例如:
[[1,2,3], [1,55,9]] 与列具有相同的行数 - 此处为2 - 并且每行具有相同数量的元素,即3.
但
[[1,2], [1,55], [4,7]] 虽然它具有不等数量的行和列,即3r 2c,但每行中的元素数量相同.
又一个例子:
[[1,2], [1,55], [4,7,8]] 既没有与列相同的行数,也没有每行具有相同数量的元素.
实际上第1步包括第2步,我是对吗?
我的尝试:
所以我到目前为止尝试的是:
listIsEqual :: [[Int]] -> Bool
listIsEqual myList = (all (\x -> length x == (length myList)) )
Run Code Online (Sandbox Code Playgroud)
现在我得到以下错误消息:
Couldn't match expected type `Bool' with actual type `[a0] -> Bool'
In the return type of a call of `all'
Probable cause: `all' is applied to too few arguments
In the expression: (all (\ x …Run Code Online (Sandbox Code Playgroud) 我必须制作三个函数来替换扁平字符串和列表.
我不知道,是否有像其他语言一样的替换功能.我搜索了但不幸的是没有成功:-(
所以我的尝试还很薄.
第一功能:
replace :: String -> String -> String -> String
replace findStr replaceStr myText = replace()??
Run Code Online (Sandbox Code Playgroud)
我的第一个功能的方法:
replace :: String -> String -> String -> String
replace [] old new = []
replace str old new = loop str
where
loop [] = []
loop str =
let (prefix, rest) = splitAt n str
in
if old == prefix -- found an occurrence?
then new ++ loop rest -- yes: replace
else head str : loop (tail …Run Code Online (Sandbox Code Playgroud) 我是这个社区的新人.我学习Haskell并且遇到Haskell编码问题.我希望你能帮助我.
我在这里和谷歌搜索,没有任何成功.
我的问题是fowllows:我想编写一个函数,它将列表作为参数,如下所示:
myStringListFilter :: [String] -> [String]
Run Code Online (Sandbox Code Playgroud)
处理以下步骤:
删除第一个字母
myStringListFilter myList = map tail strListe myList
Run Code Online (Sandbox Code Playgroud)过滤列表中以"u"或"U"开头的每个元素.
myStringListFilter myList = filter (´elem´ ['u', 'U']) (map tail strListe myList)
Run Code Online (Sandbox Code Playgroud)第二步不起作用.我收到错误.
如果我需要以下内容,如何实现解决方案:
Input: ["butter", "chees", "aUbergine", "egg", "milk", "bUbble", "curry"]
Output: ["chees", "egg", "milk"]
Run Code Online (Sandbox Code Playgroud) 我有一个列表[String]任务是删除列表中的那些元素,它们有"q"或"p",然后用toUpper大写列表中的所有字母.
我尝试的是如下:
delAndUpper :: [String] -> [String]
delAndUpper myList = filter (\x -> not('p' `elem` x || 'q' `elem` x)) myList
Run Code Online (Sandbox Code Playgroud)
它正确地从列表中删除了不需要的元素,但由于toUpper的类型是Char,因此无法在此列表中应用toUpper.
我用地图试了一下它不起作用.
delAndUpper myList = map toUpper (filter (\x -> not('p' `elem` x || 'q' `elem` x)) myList)
Run Code Online (Sandbox Code Playgroud)
我知道,在这行代码中,toUpper获取一个列表作为值,因此它无法工作,但知道如何进入列表并将应用映射到上层.
请你帮助我好吗.
提前致谢!
问候
我必须制作一个2D列表[[Int]].[Int]在Haskell中的一维列表中.
该函数应采用args"r"Int表示行数和1D列表,应将其切成长度为"r"的行.
如果列表的长度超过r*r,则应删除列表的其余部分.Howerver如果列表的长度小于r*r,那么缺失的元素应该作为0插入列表中.
例1:
输入:r = 2
list = [1,2,3,4,5,6]
输出:[[1,2],[3,4]]
例2:
输入:r = 3
list = [1,2,3,4,5,6]
输出:[[1,2,3],[4,5,6],[0,0,0]]
所以我的方法是关于thre函数如下:
zeroList :: Int -> [Int] -> [Int]
zeroList r myList = (take (r*r-(length myList)) (0 : zeroList r myList))
processList :: Int -> [Int] -> [[Int]]
processList r myList = (if (length myList < r*r)
then (myList:(zeroList r myList))
else if (length (myList > r*r))
then (reverse (drop r (reverse myList)))
else
myList)
make2DList :: Int …Run Code Online (Sandbox Code Playgroud)