鱼C论坛

 找回密码
 立即注册
查看: 790|回复: 2

[已解决]求大神帮忙python:第一列相同,则对第二列分类统计比例

[复制链接]
发表于 2021-12-25 22:00:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
示例数据:
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment315
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664


想要得到的结果:
block10_scaffold_1        total:21        alignment2        12        0.5714        alignment665        8        0.3810        alignment315        1        0.0476       
block12_scaffold_1        total:26        alignment2        14  0.5385        alignment664        12        0.4615

如果第一列相同,则统计第一列的总数,然后统计第二列有几种类型,分别输出在第一列值的后面【统计每个类型个数及占第一列的比例。每行不同类型按占比的大小排列(从大到小)】

感恩
最佳答案
2021-12-26 10:48:20
本帖最后由 傻眼貓咪 于 2021-12-26 11:00 编辑

data = """block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment315
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664"""

arr = dict()

for each in data.split("\n"):
    a, b = each.split("...")
    if a in arr:
        arr[a][0] += 1
        if(b in arr[a][1]):
            arr[a][1][b] += 1
        else:
            arr[a][1][b] = 1
    else:
        arr[a] = [1, dict()]
        arr[a][1][b] = 1

for each in arr.items():
    key, res = each
    a, b = res
    print(f"{key} total: {a}", end = "\t")
    ans = []
    for elem in b.items():
        k, v = elem
        ans.append((k, v, v/a))
    ans.sort(key = lambda x: x[2], reverse = True)
    [print(f"{k} {v} {q: .4f}", end = "\t") for k, v, q in ans]
    print()
block10_scaffold_1 total: 21    alignment2 12  0.5714   alignment665 8  0.3810  alignment315 1  0.0476
block12_scaffold_1 total: 26    alignment2 14  0.5385   alignment664 12  0.4615
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-26 10:48:20 | 显示全部楼层    本楼为最佳答案   
本帖最后由 傻眼貓咪 于 2021-12-26 11:00 编辑

data = """block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment2
block10_scaffold_1...alignment315
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block10_scaffold_1...alignment665
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment2
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664
block12_scaffold_1...alignment664"""

arr = dict()

for each in data.split("\n"):
    a, b = each.split("...")
    if a in arr:
        arr[a][0] += 1
        if(b in arr[a][1]):
            arr[a][1][b] += 1
        else:
            arr[a][1][b] = 1
    else:
        arr[a] = [1, dict()]
        arr[a][1][b] = 1

for each in arr.items():
    key, res = each
    a, b = res
    print(f"{key} total: {a}", end = "\t")
    ans = []
    for elem in b.items():
        k, v = elem
        ans.append((k, v, v/a))
    ans.sort(key = lambda x: x[2], reverse = True)
    [print(f"{k} {v} {q: .4f}", end = "\t") for k, v, q in ans]
    print()
block10_scaffold_1 total: 21    alignment2 12  0.5714   alignment665 8  0.3810  alignment315 1  0.0476
block12_scaffold_1 total: 26    alignment2 14  0.5385   alignment664 12  0.4615
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-26 17:00:52 | 显示全部楼层
感谢帮助,感动.....
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-12 13:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表