如何计算多维数组字符串的出现?

时间:2018-03-07 06:14:18

标签: python list multidimensional-array

[['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 

3 个答案:

答案 0 :(得分: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))

答案 1 :(得分:0)

您可以使用列表的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)

答案 2 :(得分:0)

您可以在python标准模块collections中使用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, ... }

bash -x ${shell_file}