首页 \ 问答 \ Rails5 - ruby 2.2.3 // ActiveSupport :: TimeZone如何变化?(Rails5 - ruby 2.2.3 // how ActiveSupport::TimeZone changed?)

Rails5 - ruby 2.2.3 // ActiveSupport :: TimeZone如何变化?(Rails5 - ruby 2.2.3 // how ActiveSupport::TimeZone changed?)

在昨天( 这里 )提出一个关于时区的问题,以下内容不适用于使用ruby 2.2.3运行的rails 5:

ActiveSupport::TimeZone.zones_map

在哪里可以阅读这个新版本之间的差异? 有什么办法可以做到这一点?


Asking a question yesterday (here) about time zones, the following is not working with rails 5 running on ruby 2.2.3 :

ActiveSupport::TimeZone.zones_map

Where can one read about the differences of use between with this new version ? What could be the way to achieve the same ?


原文:https://stackoverflow.com/questions/36172335
更新时间:2023-07-15 16:07

最满意答案

您可以使用“ 如何从列表中删除重复项”中set方法,同时保留顺序? ,使用x[1]作为唯一标识符:

def unique_second_element(seq):
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x[1] in seen or seen_add(x[1]))]

请注意,如果您想保留最后一次出现, OrderedDict方法也会显示; 对于第一次发生,您必须将输入反向,然后再次反向输出。

您可以通过支持key功能使其更加通用:

def unique_preserve_order(seq, key=None):
    if key is None:
        key = lambda elem: elem
    seen = set()
    seen_add = seen.add
    augmented = ((key(x), x) for x in seq)
    return [x for k, x in augmented if not (k in seen or seen_add(k))]

然后使用

import operator

unique_preserve_order(yourlist, key=operator.itemgetter(1))

演示:

>>> def unique_preserve_order(seq, key=None):
...     if key is None:
...         key = lambda elem: elem
...     seen = set()
...     seen_add = seen.add
...     augmented = ((key(x), x) for x in seq)
...     return [x for k, x in augmented if not (k in seen or seen_add(k))]
... 
>>> from pprint import pprint
>>> import operator
>>> yourlist = [(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c'), (64, u'a-b-c-ca-d-ab-ea-d-c-c'), (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (61, u'top-coldestcitiesinamerica'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, u's-s-main'), (44, u'vehicle-propulsion'), (43, u'x_file')]
>>> pprint(unique_preserve_order(yourlist, operator.itemgetter(1)))
[(67, u'top-coldestcitiesinamerica'),
 (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'),
 (65, u'a-b-c-ca-d-ab-ea-d-c-c'),
 (63,
  u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'),
 (62, u'ghgemissions'),
 (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'),
 (57, u'culture'),
 (55, u'cas-k-ihaveanidea'),
 (54, u'trendsfor'),
 (53, u'batteryimpedance'),
 (52, u'evs-howey-full'),
 (51, u'bericht'),
 (49, u'classiccarinsurance'),
 (47, u'uploaded_file'),
 (46, u'x_file'),
 (45, u's-s-main'),
 (44, u'vehicle-propulsion')]

You could use the set approach from How do you remove duplicates from a list in whilst preserving order?, using x[1] as the unique identifier:

def unique_second_element(seq):
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x[1] in seen or seen_add(x[1]))]

Note that the OrderedDict approach also shown would also work if you wanted to preserve the last occurrence; for a first occurrence you'd have to reverse the input then reverse again for the output.

You could make this even more generic by supporting a key function:

def unique_preserve_order(seq, key=None):
    if key is None:
        key = lambda elem: elem
    seen = set()
    seen_add = seen.add
    augmented = ((key(x), x) for x in seq)
    return [x for k, x in augmented if not (k in seen or seen_add(k))]

then use

import operator

unique_preserve_order(yourlist, key=operator.itemgetter(1))

Demo:

>>> def unique_preserve_order(seq, key=None):
...     if key is None:
...         key = lambda elem: elem
...     seen = set()
...     seen_add = seen.add
...     augmented = ((key(x), x) for x in seq)
...     return [x for k, x in augmented if not (k in seen or seen_add(k))]
... 
>>> from pprint import pprint
>>> import operator
>>> yourlist = [(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c'), (64, u'a-b-c-ca-d-ab-ea-d-c-c'), (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (61, u'top-coldestcitiesinamerica'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, u's-s-main'), (44, u'vehicle-propulsion'), (43, u'x_file')]
>>> pprint(unique_preserve_order(yourlist, operator.itemgetter(1)))
[(67, u'top-coldestcitiesinamerica'),
 (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'),
 (65, u'a-b-c-ca-d-ab-ea-d-c-c'),
 (63,
  u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'),
 (62, u'ghgemissions'),
 (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'),
 (57, u'culture'),
 (55, u'cas-k-ihaveanidea'),
 (54, u'trendsfor'),
 (53, u'batteryimpedance'),
 (52, u'evs-howey-full'),
 (51, u'bericht'),
 (49, u'classiccarinsurance'),
 (47, u'uploaded_file'),
 (46, u'x_file'),
 (45, u's-s-main'),
 (44, u'vehicle-propulsion')]

相关问答

更多
  • 假设您想要抑制所有元素的“重复”,而不仅仅是第一个元素,您可以使用: lst = [('a','b'), ('c', 'b'), ('a', 'd'), ('e','f'), ('a', 'b')] def merge(x): s = set() for i in x: if not s.intersection(i): yield i s.update(i) 给 >>> list(merge(lst)) [('a', 'b ...
  • 每个列表中的第一个元素唯一标识每个列表。 太棒了,那我们先把它转换成字典: d = {x[0]: x[1:] for x in pairsList} # d: {1: [(11, 12), (13, 14)], 2: [(21, 22), (23, 24)], 3: [(31, 32), (13, 14)], 4: [(43, 44), (21, 22)]} 我们来索引整个数据结构: index = {} for k, vv in d.iteritems(): for v in vv: ...
  • 这可以使用setdefault轻松完成: def foo(some_list): result = {} for k, v in some_list: result.setdefault(k, []).append(v) return result (使用setdefault比使用get(k, []) + [v]更有效get(k, []) + [v]因为我们在附加位置) This is easily done using setdefault: def ...
  • 在hoogle上搜索Eq a => [a] -> [a] ,返回nub函数: nub函数从列表中删除重复的元素。 特别是,它只保留每个元素的第一次出现。 (名称nub的意思是“本质”。) 在文档中,更一般的情况是nubBy 。 也就是说,这是一个O(n^2)算法,可能效率不高。 如果值是Ord类型的实例,则可以使用Data.Set.fromList ,如下所示: import qualified Data.Set as Set nub' :: Ord a => [a] -> [a] nub' = Set. ...
  • 您可以使用itertools.groupby因为您已经对这些值进行了排序。 这里是数据: >>> lot [(0.10507038451969995, 'Deadly stampede in Shanghai - Emergency personnel help victims.'), (0.07858638182141627, 'Deadly stampede in Shanghai - Police and medical staff help injured people after the sta ...
  • 您可以使用itertools.groupby轻松完成此操作 lst = [ [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smiles'), ('Lord', 'Other'), ('Lady', 'Another'), ('Lady', 'Diana', 'Spencer'), ('Lord', 'Dave', 'Castle')], [('Lord', 'Justice', 'Smith'), ('Lady', 'Justice', 'Smile ...
  • 使用集合库。 在下面的代码val_1中,val_2分别给出了元组的第一个元素和第二个元素的重复项。 import collections val_1=collections.Counter([x for (x,y) in a]) val_2=collections.Counter([y for (x,y) in a]) >>> print val_1 <<< Counter({1: 3, 2: 1, 6: 1}) 这是每个元组的第一个元素的出现次数 >>> print val_2 <<< Counter ...
  • 您可以使用“ 如何从列表中删除重复项”中的set方法,同时保留顺序? ,使用x[1]作为唯一标识符: def unique_second_element(seq): seen = set() seen_add = seen.add return [x for x in seq if not (x[1] in seen or seen_add(x[1]))] 请注意,如果您想保留最后一次出现, OrderedDict方法也会显示; 对于第一次发生,您必须将输入反向,然后再次反向输出。 ...
  • 我想我已经使用图表达到了优化的工作解决方案。 基本上,我创建了一个图形,每个节点都包含其用户信息和索引。 然后,使用dfs遍历图并找到重复项。 I think I've reached to an optimized working solution using a graph. Basically, I've created a graph with each node contains its user information and its index. Then, use dfs to traver ...
  • 如果您的列表相当小,则使用嵌套循环是可以的,但对于较大的列表很快就会变得效率低下。 例如,如果len(list1)== 10和len(list2)== 20,则内部循环内的代码将执行200次。 这是一种通过字典构建所需元组列表的算法。 字典将元组数据存储在列表中,因为它更有效:它可以附加到列表,而元组是不可变的,所以每次使用i = i + (j[0],)将项添加到元组的末尾时你实际上正在创建一个新的元组对象(以及临时(j[0],)元组),并丢弃与之绑定的旧元组对象。 list1 = [('abc', 1 ) ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)