我有一个列表的以下元素,列表是100个元素长.
[(50, (2.7387451803816479e-13, 219))]
Run Code Online (Sandbox Code Playgroud)
如何将每个元素转换为这样?
[(50, 2.7387451803816479e-13, 219)]
Run Code Online (Sandbox Code Playgroud) 我有一张地图:
Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3"))
Run Code Online (Sandbox Code Playgroud)
我想删除所有None元素并展平地图.最简单的方法是什么?我才发现这种方式:
Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3")).filter(_._2.nonEmpty).map(item => (item._1 -> item._2.getOrElse(Nil)))
Run Code Online (Sandbox Code Playgroud)
结果是:
Map(key1 -> value1, key3 -> value3)
Run Code Online (Sandbox Code Playgroud)
你知道更好的方法吗?
我很惊讶,之前没有人问过这个问题.
查看Json对象文档和快速谷歌搜索并没有产生令人满意的结果.
它的优点是什么?这个怎么运作?
编辑:为了清楚起见,请看一下这个展平/不展平的例子.
谢谢.
我想转此:
let x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Run Code Online (Sandbox Code Playgroud)
进入这个:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
非常优雅.
当然,最直接的方式是
var y = [Int]()
x.forEach { y.appendContentsOf($0) }
Run Code Online (Sandbox Code Playgroud)
但这使得生成的数组变得可变,这是不必要的.我不喜欢这样.
我试过用reduce:
let y = x.reduce([Int]()) { (array, ints) -> [Int] in
array.appendContentsOf(ints)
return array
}
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨这array是不可变的,所以我不能调用变异方法appendContentsOf.
因此,我添加了一些东西:
let y = x.reduce([Int]()) { (array, ints) -> [Int] in
var newArr = array
newArr.appendContentsOf(ints)
return newArr
}
Run Code Online (Sandbox Code Playgroud)
这简直太糟糕了.我有一种本能,这不是很好的.
如何比上述方法更加平滑地展平阵列?单线会很好.
介绍
R代码是使用Sparklyr包编写的,用于创建数据库模式.[给出了可重现的代码和数据库]
现有结果
root
|-- contributors : string
|-- created_at : string
|-- entities (struct)
| |-- hashtags (array) : [string]
| |-- media (array)
| | |-- additional_media_info (struct)
| | | |-- description : string
| | | |-- embeddable : boolean
| | | |-- monetizable : bollean
| | |-- diplay_url : string
| | |-- id : long
| | |-- id_str : string
| |-- urls (array)
|-- extended_entities (struct)
|-- retweeted_status (struct) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 json 文件加载到 Pandas 数据框。我发现有一些嵌套的json。下面是示例 json:
{'events': [{'id': 142896214,
'playerId': 37831,
'teamId': 3157,
'matchId': 2214569,
'matchPeriod': '1H',
'eventSec': 0.8935539999999946,
'eventId': 8,
'eventName': 'Pass',
'subEventId': 85,
'subEventName': 'Simple pass',
'positions': [{'x': 51, 'y': 49}, {'x': 40, 'y': 53}],
'tags': [{'id': 1801, 'tag': {'label': 'accurate'}}]}
Run Code Online (Sandbox Code Playgroud)
我使用以下代码将 json 加载到数据帧中:
with open('EVENTS.json') as f:
jsonstr = json.load(f)
df = pd.io.json.json_normalize(jsonstr['events'])
Run Code Online (Sandbox Code Playgroud)
下面是 df.head() 的输出
但是我发现了两个嵌套的列,例如位置和标签。
我尝试使用以下代码将其展平:
Position_data = json_normalize(data =jsonstr['events'], record_path='positions', meta = ['x','y','x','y'] )
Run Code Online (Sandbox Code Playgroud)
它向我显示了如下错误:
KeyError: "Try running with errors='ignore' as key 'x' is …Run Code Online (Sandbox Code Playgroud) 我想采用嵌套哈希和数组的哈希,并将其展平为具有唯一值的单个哈希.我一直试图从不同的角度来解决这个问题,但后来我让它变得比它需要的更复杂,让自己迷失在正在发生的事情中.
示例源哈希:
{
"Name" => "Kim Kones",
"License Number" => "54321",
"Details" => {
"Name" => "Kones, Kim",
"Licenses" => [
{
"License Type" => "PT",
"License Number" => "54321"
},
{
"License Type" => "Temp",
"License Number" => "T123"
},
{
"License Type" => "AP",
"License Number" => "A666",
"Expiration Date" => "12/31/2020"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
示例所需哈希:
{
"Name" => "Kim Kones",
"License Number" => "54321",
"Details_Name" => "Kones, Kim",
"Details_Licenses_1_License Type" => "PT",
"Details_Licenses_1_License Number" => "54321", …Run Code Online (Sandbox Code Playgroud) LATERAL FLATTEN(...)Snowflake 中和 的使用有什么区别TABLE(FLATTEN(...))?FLATTEN我检查了,LATERAL和的文档,TABLE无法明确以下查询之间的功能差异。
select
id as account_id,
account_regions.value::string as region
from
salesforce.accounts,
lateral flatten(split(salesforce.accounts.regions, ', ')) account_regions
Run Code Online (Sandbox Code Playgroud)
select
id as account_id,
account_regions.value::string as region
from
salesforce.accounts,
table(flatten(split(salesforce.accounts.regions, ', '))) account_regions
Run Code Online (Sandbox Code Playgroud) 我的情景:
现在我需要将这些文件组合并展平.pdftk在php中轻松完成工作:
exec("pdftk template.pdf fill_form fieldData.xfdf output flatFile.pdf flatten");
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不支持完全的utf-8.例如:西里尔字母和希腊字母被扰乱.我使用了Arial,带有unicode字符集.
编辑1:由于这个问题已经超过9个月没有解决,我决定为此开始赏金.如果有选择,以赞助某项功能或在PDFTK一个bug修正,我会很高兴捐赠.
编辑2:我不再在这个项目上工作,所以我无法验证新的答案.如果有人有类似的问题,我很高兴,如果他们能回应我的意见.
flatten ×10
json ×2
list ×2
python ×2
apache-spark ×1
arrays ×1
dictionary ×1
hash ×1
javascript ×1
lateral-join ×1
nested ×1
pandas ×1
pdf ×1
pdftk ×1
perl ×1
r ×1
ruby ×1
scala ×1
snowflake-cloud-data-platform ×1
sparklyr ×1
swift ×1
tuples ×1
utf-8 ×1
xfdf ×1