我只与Prolog合作了几天.我理解一些事情,但这真让我感到困惑.
我想要编写一个带有列表并将其展平的函数.
?- flatten([a,[b,c],[[d],[],[e]]],Xs).
Xs = [a,b,c,d,e]. % expected result
Run Code Online (Sandbox Code Playgroud)
该函数取出列表的内部结构.
这是我到目前为止:
flatten2([],[]).
flatten2([Atom|ListTail],[Atom|RetList]) :-
atom(Atom), flatten2(ListTail,RetList).
flatten2([List|ListTail],RetList) :-
flatten2(List,RetList).
Run Code Online (Sandbox Code Playgroud)
现在,这在我打电话时起作用:
?- flatten2([a,[b,c],[[d],[],[e]]], R).
R = [a,b,c,d,e]. % works as expected!
Run Code Online (Sandbox Code Playgroud)
但是当我打电话来查看我输入的列表是否已经展平时,将返回false
而不是true
:
?- flatten2([a,[b,c],[[d],[],[e]]], [a,b,c,d,e]).
false. % BAD result!
Run Code Online (Sandbox Code Playgroud)
为什么一方面工作,另一方面又不工作?我觉得我错过了很简单的事情.
我有一个包含几个数组的数组,每个数组包含几个对象,类似于此.
[[object1, object2],[object1],[object1,object2,object3]]
Run Code Online (Sandbox Code Playgroud)
这是记录到控制台的对象的屏幕截图.
什么是最好的方法来展平它,所以它只是一个对象数组?
我试过这个没有运气:
console.log(searchData);
var m = [].concat.apply([],searchData);
console.log(m);
Run Code Online (Sandbox Code Playgroud)
searchData记录上面的截图,但是m登出[]
这是searchData的实际内容:
[[{"_id":"55064111d06b96d974937a6f","title":"Generic Title","shortname":"generic-title","contents":"<p>The Healing Center offers practical, social, and spiritual support to individuals and families. Services include, but are not limited to: food and clothing, job skills training and job search assistance, auto repair (Saturdays only), mentoring, financial counseling, tutoring, prayer, life skills training, and helpful information about local community services.</p><p>Stay in touch with us:</p>","__v":0},{"_id":"5508e1405c621d4aad2d2969","title":"test english","shortname":"test-page","contents":"<h2>English Test</h2>","__v":0}],[{"_id":"550b336f33a326aaee84f883","shortname":"ok-url","title":"now english","contents":"<p>okokko</p>","category":"Transportation","__v":0}]]
Run Code Online (Sandbox Code Playgroud) 我一直在将域对象展平为DTO,如下例所示:
public class Root
{
public string AParentProperty { get; set; }
public Nested TheNestedClass { get; set; }
}
public class Nested
{
public string ANestedProperty { get; set; }
}
public class Flattened
{
public string AParentProperty { get; set; }
public string ANestedProperty { get; set; }
}
// I put the equivalent of the following in a profile, configured at application start
// as suggested by others:
Mapper.CreateMap<Root, Flattened>()
.ForMember
(
dest => dest.ANestedProperty
, opt …
Run Code Online (Sandbox Code Playgroud) 我目前正在iphone上开发一个简单的Photoshop应用程序.当我想展平我的图层时,标签处于良好的位置,但字体大小不好.这是我的代码扁平化:
UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument));
for (UILabel *label in arrayLabel) {
[label drawTextInRect:label.frame];
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
我有一个df
从数据库加载数据的数据框.大多数列都是json字符串,而有些列甚至是jsons列表.例如:
id name columnA columnB
1 John {"dist": "600", "time": "0:12.10"} [{"pos": "1st", "value": "500"},{"pos": "2nd", "value": "300"},{"pos": "3rd", "value": "200"}, {"pos": "total", "value": "1000"}]
2 Mike {"dist": "600"} [{"pos": "1st", "value": "500"},{"pos": "2nd", "value": "300"},{"pos": "total", "value": "800"}]
...
Run Code Online (Sandbox Code Playgroud)
如您所见,并非所有行在列的json字符串中具有相同数量的元素.
我需要做的是保持正常的列像它一样,id
并name
像这样平整json列:
id name columnA.dist columnA.time columnB.pos.1st columnB.pos.2nd columnB.pos.3rd columnB.pos.total
1 John 600 0:12.10 500 300 200 1000
2 Mark 600 NaN 500 300 Nan 800
Run Code Online (Sandbox Code Playgroud)
我试过这样使用json_normalize
:
from pandas.io.json import json_normalize
json_normalize(df) …
Run Code Online (Sandbox Code Playgroud) 感谢nHibernate,我使用的一些数据结构是列表中列表中的列表.例如,我有一个名为"category"的数据对象,它有一个.Children属性,可以解析为一个类别列表......每个类别都可以有子节点......依此类推.
我需要找到一种方法,从这个结构中的顶级类别开始,并获得一个列表或数组或类似于整个结构中所有孩子的东西 - 所有孩子的所有孩子等等,扁平化为一个名单.
我确信它可以通过递归来完成,但我发现递归代码很难实现,而且我确信在.Net 4中必须有一个更简单的方法使用Linq或者某些 - 任何建议?
我有一个嵌套的元组结构(String,(String,Double))
,我想将其转换为(String,String,Double)
.我有各种嵌套元组,我不想手动转换每个元组.有没有方便的方法呢?
selectMany.ToArray()
方法是什么?它是内置的方法C#
吗?
我需要将二维数组转换为一维数组.
请记住,JSON结构之前是未知的,即它完全是任意的,我们只知道它是JSON格式.
例如,
以下JSON
{
"Port":
{
"@alias": "defaultHttp",
"Enabled": "true",
"Number": "10092",
"Protocol": "http",
"KeepAliveTimeout": "20000",
"ThreadPool":
{
"@enabled": "false",
"Max": "150",
"ThreadPriority": "5"
},
"ExtendedProperties":
{
"Property":
[
{
"@name": "connectionTimeout",
"$": "20000"
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
应该被反序列化为具有键的类似Map的结构(为了简洁,不包括以上所有内容):
port[0].alias
port[0].enabled
port[0].extendedProperties.connectionTimeout
port[0].threadPool.max
Run Code Online (Sandbox Code Playgroud)
我目前正在调查杰克逊,所以我们有:
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {};
Map<String, String> o = objectMapper.readValue(jsonString, typeRef);
Run Code Online (Sandbox Code Playgroud)
但是,生成的Map实例基本上是嵌套地图的Map:
{Port={@alias=diagnostics, Enabled=false, Type=DIAGNOSTIC, Number=10033, Protocol=JDWP, ExtendedProperties={Property={@name=suspend, $=n}}}}
Run Code Online (Sandbox Code Playgroud)
虽然我需要使用"点符号"使用展平键的平面地图,如上所述.
我宁愿不自己实现这个,虽然此刻我没有看到任何其他方式......
我想知道如何完全展平列表和包含它们的东西.除此之外,我想出了这个解决方案,它可以滑动具有多个元素的东西并将它们放回去,或者在滑动之后用一个元素取出它.
这与我如何"压扁"perl 6中的列表列表略有不同?,这是完全平坦的,因为任务是重组.
但是,也许有更好的方法.
my @a = 'a', ('b', 'c' );
my @b = ('d',), 'e', 'f', @a;
my @c = 'x', $( 'y', 'z' ), 'w';
my @ab = @a, @b, @c;
say "ab: ", @ab;
my @f = @ab;
@f = gather {
while @f {
@f[0].elems == 1 ??
take @f.shift.Slip
!!
@f.unshift( @f.shift.Slip )
}
}
say "f: ", @f;
Run Code Online (Sandbox Code Playgroud)
这给出了:
ab: [[a (b c)] [(d) e f [a (b c)]] [x …
Run Code Online (Sandbox Code Playgroud)