【问题标题】:how to count the occurance of multidimentional arrays of string?如何计算字符串多维数组的出现次数?
【发布时间】:2018-03-07 06:14:18
【问题描述】:
[['Canal St & Taylor St', 'Canal St & Monroe St (*)'], ['Larrabee St & Menomonee St', 'Sheffield Ave & Kingsbury St'], ['Orleans St & Chestnut St (NEXT Apts)', 'Ashland Ave & Blackhawk St'], ['Franklin St & Monroe St', 'Clinton St & Tilden St'], ['Broadway & Barry Ave', 'Sedgwick St & North Ave'], ['State St & Kinzie St', 'Wells St & Polk St'], ['Wabash Ave & Wacker Pl', 'Clinton St & Tilden St'], ['Daley Center Plaza', 'Canal St & Monroe St (*)'], ['Daley Center Plaza', 'Canal St & Monroe St (*)'], ['Dayton St & North Ave', 'Ogden Ave & Chicago Ave'], ['Wilton Ave & Diversey Pkwy', 'Halsted St & Wrightwood Ave'], ['Canal St & Madison St', 'LaSalle St & Illinois St'], ['Theater on the Lake', 'Lakeview Ave & Fullerton Pkwy'], ['Halsted St & Maxwell St', 'Halsted St & 18th St'], ['Ravenswood Ave & Lawrence Ave', 'Clarendon Ave & Gordon Ter']]

这是我的清单。我如何计算['Canal St & Taylor St', 'Canal St & Monroe St (*)']的出现次数

输出应该是

  ['Canal St & Taylor St', 'Canal St & Monroe St (*)']  : 1
  ['Larrabee St & Menomonee St', 'Sheffield Ave & Kingsbury St'] : 2
etc 

【问题讨论】:

  • 请更清楚地描述您的问题。

标签: python list multidimensional-array


【解决方案1】:

你需要做这样的事情:-

super = [['Canal St & Taylor St', 'Canal St & Monroe St (*)'], ['Larrabee St & Menomonee St', 'Sheffield Ave & Kingsbury St'], ['Orleans St & Chestnut St (NEXT Apts)', 'Ashland Ave & Blackhawk St'], ['Franklin St & Monroe St', 'Clinton St & Tilden St'], ['Broadway & Barry Ave', 'Sedgwick St & North Ave'], ['State St & Kinzie St', 'Wells St & Polk St'], ['Wabash Ave & Wacker Pl', 'Clinton St & Tilden St'], ['Daley Center Plaza', 'Canal St & Monroe St (*)'], ['Daley Center Plaza', 'Canal St & Monroe St (*)'], ['Dayton St & North Ave', 'Ogden Ave & Chicago Ave'], ['Wilton Ave & Diversey Pkwy', 'Halsted St & Wrightwood Ave'], ['Canal St & Madison St', 'LaSalle St & Illinois St'], ['Theater on the Lake', 'Lakeview Ave & Fullerton Pkwy'], ['Halsted St & Maxwell St', 'Halsted St & 18th St'], ['Ravenswood Ave & Lawrence Ave', 'Clarendon Ave & Gordon Ter']]
search = ['Canal St & Taylor St', 'Canal St & Monroe St (*)']
print(super.count(search))

【讨论】:

    【解决方案2】:

    你可以在python标准模块collections中使用Counter。但是,它不允许将密钥作为list,因此您必须将其转换为tuple

    from collections import Counter
    
    a_list = [['Canal St & Taylor St', 'Canal St & Monroe St (*)'], ['Larrabee St & Menomonee St', 'Sheffield Ave & Kingsbury St'], ['Orleans St & Chestnut St (NEXT Apts)', 'Ashland Ave & Blackhawk St'], ['Franklin St & Monroe St', 'Clinton St & Tilden St'], ['Broadway & Barry Ave', 'Sedgwick St & North Ave'], ['State St & Kinzie St', 'Wells St & Polk St'], ['Wabash Ave & Wacker Pl', 'Clinton St & Tilden St'], ['Daley Center Plaza', 'Canal St & Monroe St (*)'], ['Daley Center Plaza', 'Canal St & Monroe St (*)'], ['Dayton St & North Ave', 'Ogden Ave & Chicago Ave'], ['Wilton Ave & Diversey Pkwy', 'Halsted St & Wrightwood Ave'], ['Canal St & Madison St', 'LaSalle St & Illinois St'], ['Theater on the Lake', 'Lakeview Ave & Fullerton Pkwy'], ['Halsted St & Maxwell St', 'Halsted St & 18th St'], ['Ravenswood Ave & Lawrence Ave', 'Clarendon Ave & Gordon Ter']]
    
    counter = Counter([tuple(item) for item in a_list])
    dict(counter)
    
    # {('Larrabee St & Menomonee St', 'Sheffield Ave & Kingsbury St'): 1,('Broadway & Barry Ave', 'Sedgwick St & North Ave'): 1, ... }
    

    【讨论】:

      【解决方案3】:

      你可以使用count列表功能

      l = [['Canal St & Taylor St', 'Canal St & Monroe St (*)'], ['Larrabee St & Menomonee St', 'Sheffield Ave & Kingsbury St'], ['Orleans St & Chestnut St (NEXT Apts)', 'Ashland Ave & Blackhawk St'], ['Franklin St & Monroe St', 'Clinton St & Tilden St'], ['Broadway & Barry Ave', 'Sedgwick St & North Ave'], ['State St & Kinzie St', 'Wells St & Polk St'], ['Wabash Ave & Wacker Pl', 'Clinton St & Tilden St'], ['Daley Center Plaza', 'Canal St & Monroe St (*)'], ['Daley Center Plaza', 'Canal St & Monroe St (*)'], ['Dayton St & North Ave', 'Ogden Ave & Chicago Ave'], ['Wilton Ave & Diversey Pkwy', 'Halsted St & Wrightwood Ave'], ['Canal St & Madison St', 'LaSalle St & Illinois St'], ['Theater on the Lake', 'Lakeview Ave & Fullerton Pkwy'], ['Halsted St & Maxwell St', 'Halsted St & 18th St'], ['Ravenswood Ave & Lawrence Ave', 'Clarendon Ave & Gordon Ter']]
      for i in l:
          c = l.count(i)
          print(i, '--->' ,c)
      

      【讨论】:

      • 无需添加if i in l,因为您正在迭代列表,这意味着应该在列表中
      猜你喜欢
      • 2015-06-30
      • 2020-05-06
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 2010-09-21
      • 1970-01-01
      相关资源
      最近更新 更多