鱼C论坛

 找回密码
 立即注册
查看: 2300|回复: 7

[已解决]python中for in命令与print命令组合打印结果排列方式问题

[复制链接]
发表于 2018-3-13 09:31:09 | 显示全部楼层 |阅读模式

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

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

x
代码:
str1 = '''拷贝过来的字符串'''
list1 = []

for each in str1:
    if each not in list1:
        if each == '\n':
            print('\\n', str1.count(each))
        else:
            print(each, str1.count(each))
        list1.append(each)


结果:
拷 1
贝 1
过 1
来 1
的 1
字 1
符 1
串 1
为什么不是:
拷 1 贝 1 过 1 来 1 的 1 字 1 符 1 串 1
最佳答案
2018-3-13 09:39:00
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
   
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

  1. ##  help(print)可以看到print方法的多个value直接的分隔符默认为空格(sep = ' '),结尾默认为换行(end='\n')。
  2. ##  想得到你需要的效果只要将end = ' '(空格)就可以了。
  3. print('\\n', str1.count(each), end = ' ')
  4. print(each, str1.count(each), end = ' ')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-3-13 09:39:00 | 显示全部楼层    本楼为最佳答案   
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
   
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

  1. ##  help(print)可以看到print方法的多个value直接的分隔符默认为空格(sep = ' '),结尾默认为换行(end='\n')。
  2. ##  想得到你需要的效果只要将end = ' '(空格)就可以了。
  3. print('\\n', str1.count(each), end = ' ')
  4. print(each, str1.count(each), end = ' ')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 09:40:43 | 显示全部楼层
print 每次打印后自动换行啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 09:44:36 | 显示全部楼层
print()函数默认换行,要想不换行,需要修改里面的参数。
  1. print(xxxx, end='')
复制代码
以空字符串结尾就不会换行了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 09:51:44 | 显示全部楼层
else:
            print(each, str1.count(each))
这里改成
else:
            print(each, str1.count(each),end=' ')
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-13 10:39:35 | 显示全部楼层

谢谢你,明白了,还交给我一个解决问题的方法
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-13 10:53:14 | 显示全部楼层
print函数默认参数为(end=‘\n’),所以每次循环都会换行,因此需要为end赋一个参数,即(end=' ')
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-14 14:51:19 | 显示全部楼层
小疑 发表于 2018-3-13 10:39
谢谢你,明白了,还交给我一个解决问题的方法

一起加油吧。多多交流!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-8 14:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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