【问题标题】:Unique fields analysis into formatted strings [closed]对格式化字符串的唯一字段分析[关闭]
【发布时间】:2014-02-12 22:28:42
【问题描述】:

这是我的第一篇文章。我正在开始对以 URL 格式格式化的字符串集合进行某种研究。假设我有一个包含如下字符串的文件:

A/B/C/D

对我来说,这个字符串有 4 个组件。文件中的字符串具有不同的长度。我正在寻找一种有效的方法,可能是在 BASH 中,以获取每个字段的唯一字符串数。

非常感谢任何帮助或提示!

谢谢!

麦克风

【问题讨论】:

  • 先决定语言可能是个好主意。
  • 我理解在 Bash、Python 或 Perl 中执行此类操作的动机,但是…… C++?真的吗?
  • 请参阅 stackoverflow.com/questions/13648410/…vontrapp 的答案,了解如何在 bash 中执行此操作。
  • 如果根据您的定义,A/B/C/D 是“字符串”,并且(我猜,这里)ABCD 是“字段”,那么我对“每个字段的唯一字符串数”的概念有点不清楚......你的意思是每个字符串中的字段数吗?或者特定字段值出现的不同字符串的数量?或者是其他东西? “独特”与它有什么关系?唯一字段还是唯一字符串?

标签: python c++ bash


【解决方案1】:

假设字符串总是由 / 分隔,这就是我在 Python 中的做法

start1 = "A/B/C/D"
start2 = "B/D/E/A/B"
start3 = "D/A/A/B/D/C"
start4 = "C"

startList = [start1, start2, start3, start4]
print "startList: ", startList
fields = []

for start in startList:
    for field in start.split('/'):
        fields.append(field)

print "fields: ", fields

countDict = dict.fromkeys(fields)
print "countDict 1: ", countDict

for entry in countDict.keys():
    countDict[entry] = fields.count(entry)

print "countDict 2: ", countDict

这是print 语句的输出:

startList: ['A/B/C/D', 'B/D/E/A/B', 'D/A/A/B/D/C', 'C']
fields: ['A', 'B', 'C', 'D', 'B', 'D', 'E', 'A', 'B', 'D', 'A', 'A', 'B', 'D', 'C', 'C']
countDict 1: {'A': None, 'C': None, 'B': None, 'E': None, 'D': None}
countDict 2: {'A': 4, 'C': 3, 'B': 4, 'E': 1, 'D': 4}

但是,如果起始字符串很大(数百万个条目)并且速度确实很重要,那么 Python 可能不是您的最佳选择。它易于学习,可读性强(也是我最喜欢的语言),但它不如 C 这样的编译语言快。话虽如此,它对于绝大多数应用程序来说已经足够快了

关于这种特殊方法的说明。有很多“更高级”的方法来计算列表中的条目。许多更快,更“pythonic”,但这应该足以满足您的目的。如果您想查看这些方法,只需在站点周围快速搜索即可。如果此方法中的任何内容不清楚,请告诉我,希望对您有所帮助!


如果您想要的是每个字符串中唯一条目的数量,这就是您要查找的内容:

start1 = "A/B/C/D"
start2 = "B/D/E/A/B"
start3 = "D/A/A/B/D/C"
start4 = "C"

startList = [start1, start2, start3, start4]
print "startList: ", startList

countDict = dict.fromkeys(startList)
print "countDict 1: ", countDict

for start in startList:
    countDict[start] = len(set(start.split('/')))

print "countDict 2: ", countDict

这是print 语句的输出:

startList:  ['A/B/C/D', 'B/D/E/A/B', 'D/A/A/B/D/C', 'C']
countDict 1:  {'B/D/E/A/B': None, 'A/B/C/D': None, 'C': None, 'D/A/A/B/D/C': None}
countDict 2:  {'B/D/E/A/B': 4, 'A/B/C/D': 4, 'C': 1, 'D/A/A/B/D/C': 4}

【讨论】:

  • 感谢您的回答。问题是我必须为一堆字符串分析每个字段中唯一字符串的数量(跟踪字段本身),而不是在同一个字符串中。假设:string_1 = A/B/C/D;字符串_2 = A/B/F/G。所以结果是:Field_1 = 1;字段_2 = 1;字段_3 = 2; Field_4 = 2。
  • 哦,明白了,不好意思我误解了你的问题,我会更新一下
  • @user3224616,请看我的编辑
  • 谢谢!现在它更接近我想要的。如果我没记错的话,您的 countDict 1 表示第一个字段中“A”字符的唯一性,第二个字段中“C”字符的唯一性,依此类推。但是,根据您编写的字符串,我的理想结果是: Field_1 = 4 个不同的字符;字段_2 = 3;字段_3 = 3;字段_4 = 3;字段_5 = 2; Field_6 = 1。希望清楚。再次感谢!
  • @user3224616,如果这就是你想要的,那么我的第一个答案更接近,我会再次更新
【解决方案2】:

鉴于“URL”位,假设您的意思是计算唯一组件而不是每个组件中的单词数。那么我可能不会为此使用 bash,出于简单起见,但如果我不得不做类似的事情

  • 检查输入是否包含/

    [[ $input == *"/"* ]]
    
  • 检查输入不包含空白字符

    [[ $input != *[[:space:]]* ]]
    
  • 将内部字段分隔符 (IFS) 设置为 /

    IFS="/" #Note you are doing this in a shell script and not directly in a shell
    
  • 从输入中创建一个数组。

    arr=($input)
    
  • 使数组唯一。请参阅https://stackoverflow.com/a/17758600/3076724 以获得可能最简单的答案

  • 然后打印组件的数量/对每个组件做一些事情

    echo "Number of components in $input = ${#arr[@]}"
    for i in "${arr[@]}"; do
      #Do something with each component "$i"
    done
    

这应该可以帮助您入门,您可以轻松地将它们组合在一起以制作一个有效的 shell 脚本。

【讨论】:

    【解决方案3】:

    如果您关心个别部分:

    for n in 1 2 3 4 5 6 7
    do
        echo "for field # $n, unique values:"
        cut -d / -f $n collection-of-strings | sort | uniq -c
    done
    

    如果您正在查看 URI 类型前缀:

    for n in 1 2 3 4 5 6 7
    do
        echo "for fields # 1…$n, unique prefices:"
        cut -d / -f 1-$n collection-of-strings | sort | uniq -c
    done
    

    假设您的字段不超过 7 个,如果字符串较长,请相应调整 for 循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      相关资源
      最近更新 更多