如何在记事本中分发我在python中创建的列表的输出 [英] How can I distribute the output of a list I made in python in my notepad

查看:125
本文介绍了如何在记事本中分发我在python中创建的列表的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决一个问题,原因是我的IDE无法保留一系列数字,导致range函数正常工作.

然后我做了一个解决方案

您需要了解字符串在Python中的工作方式.

字符串是保存在封闭桶中的常量(文字).在官方文档中,您会发现字符串是Unicode代码点".

但是程序员需要以可编程的方式更改或操作文本.您需要的是:

"[[x1] [空格] [逗号] [x2] [逗号] ... [xn] [空格] [逗号]" 其中"xn"是一个数字,并且","是恒定的.

为了实现这一点,程序员可以使用可编程的方式使用掩码".告诉软件他们想在哪里放置更改.可以使用字符串格式运算符:

"%d , %f" %(my_first_integer, my_float)
[0][1][2][3][4][\0]
# Hey Python, return a string, using the above template, 
# but place useful stuff where you find magic keywords.

意思是:

  • 创建6个位置序列;
  • 在[0]中,将类型为int的my_integer转换为chr;
  • 在[1]中,复制" " ;;
  • 在[2]中,复制,".
  • 在[3]中,复制"" ;;
  • 在[4]中,将类型为float的my_float转换为chr;
  • 在[5]中,将"\ 0&"放置在这样字符串就结束了. (自动放置在Python中)

还有其他方法可以执行此操作,即字符串对象有一个方便的方法称为format来处理此构造:

my_integer = 2
my_string = "{0}*pi = {1}".format(my_integer, my_integer*3.14)
print(my_string)

# 2*pi = 6.28

程序员将使用一个或另一个方法来获得相同的最终结果.

在Python和其他语言中,可以使用特定的方法和/或运算符组合字符串,连接,获取子字符串等.

为了保持可读性,您可能(我想)想将每个值放在一行中.在字符串中,可以将\n之类的特殊字符用于Previous question about it so this is a follow-up to the question. Here's my list comment on the previous question.

I actually made some adjustments by adding a line; 'My_list = list(range(100)) before applying your code so it actually worked. But it combines the answers without commas, for example 10 does this '0123456789' instead of '0,1,2,3,4,5,.....8,9'. any suggestions?

I decided to post this question not to allow the other question go out of context (as i was advised to).

Any suggestions?

解决方案

You need to understand how strings works in Python.

Strings are constants (literals) kept in a closed bucket. In official docs you can find that "Strings are immutable sequences of Unicode code points".

But programmers need to change or manipulate text in a programmable way. In your case you want:

"[x1][space][comma][x2][comma]...[xn][space][comma]" where "xn" is a number, and " ," is constant.

In order to achieve this, in a programmable way, programmers can use "masks" to tell the software where they want to place their changes. One can use string format operators:

"%d , %f" %(my_first_integer, my_float)
[0][1][2][3][4][\0]
# Hey Python, return a string, using the above template, 
# but place useful stuff where you find magic keywords.

Which means:

  • Create a 6 positions sequence;
  • In [0], place my_integer of type int converted into chr;
  • In [1], copy " ";
  • In [2], copy ",".
  • In [3], copy " ";
  • In [4], place my_float of type float converted into chr;
  • In [5], place "\0" so the string is over. (Automatically placed in Python)

There are other ways to do this, i.e., the string object has a handy method called formatto handle this construction:

my_integer = 2
my_string = "{0}*pi = {1}".format(my_integer, my_integer*3.14)
print(my_string)

# 2*pi = 6.28

The programmer will achieve the same final result using one or another startegy.

In Python, as well as in other languages, one can combine strings, concatenate, get sub-strings and so on, using specific methods and/or operators.

In order to keep readability you maybe (I guess) want to place each value in a line. In strings you can use special characters like \n for new lines.

my_list = list(range(100))

# ... useful code here and there ...

with open("output.txt", "w") as o:
    o.write("My list:\n")
    o.write("\tSize: {0}\n\n".format(len(my_list)))
    o.write("\t----start----\n")
    for i in range(len(my_list)):
        o.write("%d\n" % my_list[i])
    o.write("\n\t----end----\n")

# That writes:
# My list:
#     Size: 100
#
#     ----start----
# 0
# 1
# 2
# 3
...
# 99
#
#    ----end----

Remember, this is not a comprehensive guide, but a layman one. I'm skipping a lot of boring words and technical details that you'll better find in Python books and courses.

这篇关于如何在记事本中分发我在python中创建的列表的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆