- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
我有一个集合,每个集合的元素都有值,我想按值将元素尽可能均匀地分布在 N 个片段上。
我试过用scala代码完成如下结果,一直没找到好方法,很容易死循环
像这样:
Map("table1"->100,"table2"->500,"table3"->20000,"table4"->10,"table5"->1000,"table6"->200,"table7"->10000)
对于4件,我希望得到尽可能平均的结果。
Map("table7"->10000)
Map("table3"->20000)
Map("table1"->100,"table2"->500,"table4"->10,"table4"->10,"table5"->1000,"table6"->200)
Map()
或者,很完美
Map("table7"->10000)
Map("table3"->20000)
Map("table1"->100,"table2"->500,"table4"->10,"table4"->10,"table6"->200)
Map("table5"->1000)
我用了@Krzysztof Atłasik的代码,得到的结果是重复的。10 个桶。公式数据为:
val testMap=Map("test_7d_all_qr "->1,"test_daily_advertise_position "->1,"test_7d_province "->94,"test_city_statistics "->34916,"test_30days_page_view "->11,"test_30days_ad_anomaly_auth_analyze "->26,"tt_user_grouping "->1,"oauth_refresh_tokens "->42,"tt_daily_share "->4476,"test_ad_anomaly_ip_analyze "->148,"test_7d_ad_anomaly_time_analyze "->15,"share_tool_template "->1,"test_30days_visit_duration "->70,"tt_qr_group_01 "->1,"tt_seven_user_tmp "->35890,"test_qr_code_statistics_temporary "->1,"tt_cms_scene "->1,"test_30days_terminal_analysis "->1406,"test_7d_terminal_analysis "->765,"test_7d_single_scene "->51,"test_30days_single_scene "->77,"test_monitor_daily_new_user "->1,"tt_ad_authorize "->1,"tt_sdk_version_appcount "->1,"test_ad_anomaly_auth_analyze "->428,"tt_attribute_relationship "->1,"test_7d_page_view "->9,"tt_ad_user_info "->1,"phone_model "->1,"share_tool_var "->1,"share_tool_type_template "->1,"tt_stay_logs_link "->8,"test_advertise_home_hour "->4,"test_monitor_hourly "->1,"test_7d_phonebrand "->71,"tt_cms_system "->1,"test_7d_city_statistics "->297,"test_link_cheat_protect "->1,"test_daily_advertise_link "->1,"test_30days_trend_analysis "->17,"tt_cms_log "->1,"test_hourly_advertise_position "->6,"push_join_info "->1,"test_monitor_daily_app "->5,"test_7d_event "->491,"cms_boss_words "->1,"test_daily_error "->13903,"test_user_activity "->569,"tt_event "->127,"test_link_summary_hour "->290,"test_7d_single_qr_group "->1,"tt_sdk_log "->1,"tt_code "->42,"test_page_view "->11689,"tt_7d_device_statistics "->709,"test_7d_ad_anomaly_ip_analyze "->15,"cms_wx_data "->1,"tt_sdk_version_apps "->311,"test_qr_code_statistics "->138,"test_daily_link "->709,"test_30days_event "->974,"test_7d_error "->1,"test_30days_city_statistics "->527,"tt_link_trace "->8,"test_7d_single_position "->1,"test_30days_single_link "->5,"test_daily_event "->33480,"tt_share_page "->53,"tt_30days_hierarchy_share "->1,"tt_stay_logs "->17149,"test_dailyshare_gender "->282,"test_hourly_advertise_media "->6,"test_monitor_daily_link "->4,"tt_device_statistics "->66629,"oauth_authorization_codes "->73,"test_ad_anomaly_all_analyze "->5488,"test_30days_userShare_top10 "->943,"test_daily_funnel "->782,"oauth_scopes "->1,"top100070922 "->0,"test_7d_userShare_top10 "->639,"tt_hierarchy_share "->148,"oauth_clients "->1,"user_phone "->0,"tt_sms_queue "->2,"tt_cms_article "->1,"test_monitor_daily "->1,"cms_source_type "->1,"test_30days_entrance_page "->11,"test_30days_ad_anomaly_all_analyze "->636,"oauth_jwt "->1,"tt_cms_user "->1,"tt_ci_sessions "->38,"test_7d_link_monitor "->1,"test_daily_link_monitor "->2717,"test_hourly_qr_group "->239,"tt_7d_hierarchy_share "->1,"test_30days_event_paras "->1,"tt_event_link_funnel "->2,"test_blackIP_setting "->1,"test_visit_duration "->2476,"tt_qr_group "->19,"test_hourly_scene "->41057,"test_30days_ad_anomaly_time_analyze "->26,"test_hourly_scene_group "->24908,"tt_smartLink_dict_name "->1,"test_scene_statistics "->7868,"test_30days_single_entrance_page "->59,"test_online_status "->1,"convert_path "->1,"user_group_funnel_daily "->7,"test_daily_media "->238,"test_grey_ak "->1,"cms_market "->1,"test_dailyshare_top10 "->10329,"test_hourly_qr "->334,"test_monitor_daily_trend "->8,"test_dailyshare_source "->224,"test_ad_anomaly_time_analyze "->158,"test_30days_scene_group "->34,"test_hourly_share_summary "->2895,"test_30days_single_page_view "->107,"service_user_view "->1,"tt_funnel "->1,"test_30days_single_qr_group "->1,"test_monitor_hourly_link "->12,"test_30days_error "->1,"test_daily_scene_group "->3702,"test_smartLink_day_analysis "->5770,"tt_prevent_cheat "->1,"test_hourly_advertise_link "->8,"tt_wechat_user_bind "->440049,"user_apps "->110,"test_dailyshare_page "->1394,"test_link_summary "->57,"test_smartLink_summary_analysis "->352,"share_tool_code "->1,"test_7d_trend_analysis "->15,"test_7d_event_paras "->1,"tt_sdk_history "->1,"tt_mini_code_authorize "->1,"test_visit_depth "->1852,"test_daily_entrance_page "->1254,"tt_private_construct_info "->1,"test_monitor_hourly_trend "->14,"tt_user_etl "->12914,"test_7d_scene_group "->26,"test_30days_link_summary "->1,"test_7d_visit_depth "->43,"tt_task "->1,"tt_code_share "->29,"tt_authorization "->1,"test_daily_advertise_media "->1,"share_tool_user_template "->1,"tt_30days_device_statistics "->1301,"tt_code_tool "->21,"test_30days_single_media "->2,"tt_scene_link "->1,"tt_code_sem "->2,"tt_stay_logs_media "->4,"tt_mini_radio "->1,"tt_sdk_notice "->1,"test_entrance_page "->5209,"test_terminal_analysis "->91350,"test_7d_single_entrance_page "->39,"test_7d_visit_duration "->57,"cms_analy_bind "->1,"test_30days_link_monitor "->1,"test_hourly_trend_analysis_debug "->1,"test_advertise_home "->1,"tt_stay_logs_position "->4,"test_30days_single_position "->1,"tt_smartLink_dict "->114,"test_ad_anomaly_all_analyze_hour "->1,"test_monitor_hourly_qr "->1,"test_smartLink_hour_analysis "->39287,"test_hourly_position "->843,"tt_cms_scene_type "->1,"test_dailyshare_city "->2915,"test_operation_log "->1,"oauth_access_tokens "->42,"share_tool_type "->1,"test_hourly_media "->1432,"tt_mini_routine_push "->6,"test_daily_position "->92,"test_app_summary "->1,"test_trend_analysis_debug "->1,"test_daily_phonebrand "->6565,"test_daily_share_summary "->371,"test_third_setup "->1,"user_app_relations "->20,"test_7d_single_link "->2,"tt_field_mapping "->1,"test_event_paras "->684271,"test_7d_entrance_page "->9,"test_dailyshare_user "->548969,"test_region_statistics "->11658,"test_7d_ad_anomaly_all_analyze "->396,"test_30days_visit_depth "->53,"tt_authorize "->6,"test_30days_province "->148,"tt_ad_activity_hourly "->714,"test_7d_link_summary "->1,"oauth_users "->1,"test_monitor_daily_qr "->1,"test_30days_phonebrand "->117,"test_7d_single_media "->1,"tt_db_split "->1,"test_30days_ad_anomaly_ip_analyze "->25,"service_user "->1,"test_monitor_hourly_share "->34,"test_hourly_trend_analysis "->16825,"test_30days_all_qr "->1,"test_trend_analysis "->2196,"aladdin_user "->4,"test_30days_single_qr "->3,"test_daily_page_view "->1295,"tt_stay_logs_bak "->250,"test_hourly_link "->3199,"tt_media "->3,"test_7d_ad_anomaly_auth_analyze "->15,"test_7d_single_qr "->1,"test_daily_qr_group "->37,"test_7d_single_page_view "->70,"user_feedback "->1,"test_user_activity_details "->2486,"test_monitor_daily_share "->1)
我得到了这个结果
Map(test_event_paras -> 684271)
Map(test_dailyshare_user -> 548969)
Map(tt_wechat_user_bind -> 440049)
Map(test_terminal_analysis -> 91350)
Map(test_daily_qr_group -> 37, tt_stay_logs_bak -> 250, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_monitor_hourly_trend -> 14, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_30days_trend_analysis -> 17, test_7d_terminal_analysis -> 765)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_advertise_home_hour -> 4, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765, tt_media -> 3)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765, tt_event_link_funnel -> 2)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765, tt_sdk_history -> 1)
Map(aladdin_user -> 4, test_30days_ad_anomaly_ip_analyze -> 25, tt_stay_logs_bak -> 250, test_30days_scene_group -> 34, test_hourly_link -> 3199, test_7d_single_page_view -> 70, tt_device_statistics -> 66629, user_phone -> 0, test_smartLink_summary_analysis -> 352, test_7d_single_scene -> 51, test_user_activity -> 569, tt_hierarchy_share -> 148, test_daily_page_view -> 1295, top100070922 -> 0, tt_smartLink_dict -> 114, test_trend_analysis -> 2196, test_7d_terminal_analysis -> 765)
我按照Krzysztof Atłasik的方式重写了一个方法, 结果正确。
def distributeMy(elements: Map[String, Long], bucketCount: Long) = {
implicit val ordering: Ordering[(Long, Map[String, Long])] = Ordering.by(-_._1)
val map = elements.toList.sortBy(-_._2)
var bucket =new mutable.TreeSet[(Long,Map[String,Long])]
map.foreach{
case (k:String,v:Long) =>{
if(bucket.size < bucketCount){
bucket.add((v,Map(k->v)))
}
else{
val element = bucket.last
bucket.remove(bucket.last)
bucket.add((element._1 + v ,element._2 ++ Map(k->v) ))
}
}
}
bucket.toSet
}
在开发中的网页上,我在 IE 上遇到此错误 element = $(element); 此代码位于prototype.js 预期对象 如何消除此错误。 更新: 现场也使用了 jQuery。 最佳答
我有两个大小相同的嵌套数组: Array1 =[[1, 2], [], [2, 3]] Array2= [[1, 4], [8, 11], [3, 6]] 我需要将它们合并到一个数组中,如下所示: A
我有一些 jQuery 代码,当单击具有特定 ID 的项目时运行。当 ID 是 的一部分时,它就可以工作。元素,但当它位于 中时则不然元素。为什么会这样呢?我想使用 an,因为如果用户关闭了 Ja
Flex-box 规范 3声明 flex 元素不是 block 容器: A flex item establishes a new formatting context for its content
我遇到了一个意想不到的问题。 HTML JS $(function() { var $divs = $('.myDiv'); // create new div not in
我使用 Bootstrap 和 Ember.js 得到了一个无序列表。每个列表项都是一个显示新帖子的链接,每当您单击该链接时,Ember 都会添加类 active默认情况下。我正在使用 Bootstr
我正在尝试让一个函数正常工作,但运气不佳,所以我想向 Stackoverflow 智囊团提出一个新手问题! 基本上,我有一个表单,并且循环遍历所有元素以查看是否存在自定义数据属性。如果存在,则保持该元
我想映射一个可选数组,删除那些 nil 值,并使用另一个函数映射非 nil 值。 我知道我可以通过使用 compactMap 然后使用常规 map 来实现这一点,但我只想遍历数组一次。 我为此实现了一
我如何定位 li 元素,除非它们出现在 之后元素?换句话说,我想针对步骤而不是注释。 我尝试向 OL 添加一个我想从选择中排除的类,但我想出的代码不起作用。 (顺便说一句,重构 html 不是一种选
Warning 1 The element 'system.webServer' has invalid child element 'rewrite'. List of possible eleme
我正在尝试编写一个脚本,该脚本将遍历 HTML 源并创建 DOM 的 JSON 文件,然后使用 d3.js 在 TreeView 中显示该文件。我遇到的问题是不仅希望显示元素(TITLE、P、LI 等
我有以下 HTML 表单:- Option 1 Option 2
我试图在选定的 HTML 元素之后选择下一个具有类名 slider-value 的 span 元素。我尝试了多种解决方案,但没有一个有效。 我可以通过 id 选择它,但我不希望那样做使代码冗余。 $(
如果电子邮件地址无效,我想在屏幕上显示一条消息“请输入有效的电子邮件地址”。 body 元素的innerHTML 语句工作正常,但我用于p 元素的innerHTML 语句不起作用。 有一次,当我测试它
以下 jQuery 代码调用 ul 元素,查找元素内的前 三个 li 列表项,并隐藏剩余的 li 项目。然后,它附加一个 li 元素,其中显示“显示更多...”,并且在单击时显示之前隐藏的列表项。 (
我问了a question早些时候关于将编辑/删除链接与 h1 元素内联的最佳方法。我能够通过给出的答案实现这一点,但我现在有额外的要求,我需要在 h1 下方显示一个段落并编辑/删除链接。 到目前为止
我使用 MVC 4 和 knockout.js 库版本 2.1.0 显示从服务器检索到的大量文件的表中的以下摘录。 0)"> 正在正确检索数据,
我创建了一个脚本,该脚本在鼠标悬停在父容器上时激活,并且应该将其子元素移离鼠标。我目前已经让它工作了,但是代码的某些部分似乎与 REACT 代码应该是什么样子相矛盾。特别是两个部分。 我在渲染函数中使
我是 JS 新手,正在尝试理解项目 https://github.com/tastejs/todomvc 的代码 请参阅屏幕截图,我尝试对 button X 以及其父元素 div 设置断点,但在这两种
例如,假设有一个带有奇特颜色的标记: Something written here 使用 Visual Studio 2017 和 MVC 5 元素,有没有办法检查和定位当前应用了哪些样式,以及负责它
我是一名优秀的程序员,十分优秀!