找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2806|回复: 4

[求助] python字符分割问题,求帮助下吧

5

主题

7

帖子

7

积分

贫民

积分
7
nowemf 发表于 2018-11-21 15:40:15 | 显示全部楼层 |阅读模式
from time import sleep
from pypinyin import pinyin,lazy_pinyin

str="我喜欢你"
result=[]
for s in str:
    result.append(s)
# print(result)

# for i in result:
# a=result[i]
# print(a)
a=""
zm=[]
for i in range(0, len(result)):  
  print(result[i])  
  a=lazy_pinyin(result[i])
  for q in a:
    zm.append(q)
    print(zm)
    zm=[]

输出的 是

["wo"]

["xi"]

["huan"]

["ni"]
怎么才能弄成这样 求大神帮忙

["w“”o"]

["x“”i"]

["h“”u“”a“”n"]

["n“”i"]


回复

使用道具 举报

0

主题

27

帖子

27

积分

贫民

积分
27
艾幻翔 发表于 2018-11-21 16:11:05 | 显示全部楼层
本帖最后由 艾幻翔 于 2018-11-21 16:16 编辑

上面的效率不高哇,没必要在for里面调用 pinyin 函数
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Created by lightwave on 2017/9/6
  4. # Copyright (c) 2017 lightwave. All rights reserved.

  5. from pypinyin import pinyin

  6. words = "我喜欢你"

  7. pinyin_list = zip(words, pinyin(words))
  8. for word, pinyin in pinyin_list:
  9.     print(word, list(*pinyin))
复制代码


输出结果:
我 ['w', 'ǒ']
喜 ['x', 'ǐ']
欢 ['h', 'u', 'ā', 'n']
你 ['n', 'ǐ']
回复 支持 1 反对 0

使用道具 举报

0

主题

27

帖子

27

积分

贫民

积分
27
艾幻翔 发表于 2018-11-21 16:02:38 | 显示全部楼层
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Created by lightwave on 2017/9/6
  4. # Copyright (c) 2017 lightwave. All rights reserved.

  5. from pypinyin import pinyin

  6. words = "我喜欢你"

  7. for word in words:
  8.     pinyin_list = pinyin(word)
  9.     print(word, [w for w in pinyin_list[0][0]])
复制代码
回复 支持 反对

使用道具 举报

5

主题

7

帖子

7

积分

贫民

积分
7
nowemf  楼主| 发表于 2018-11-21 17:34:45 | 显示全部楼层

真心谢谢你的帮助
回复 支持 反对

使用道具 举报

5

主题

7

帖子

7

积分

贫民

积分
7
nowemf  楼主| 发表于 2018-11-21 20:41:13 | 显示全部楼层

真心感谢 你的帮助已经解决问题了
回复 支持 反对

使用道具 举报

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

本版积分规则

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