相关疑难解决方法(0)

加入正则表达式搜索的输出

  • 我有一个在文件中查找数字的正则表达式.
  • 我把结果放在一个列表中

问题是它为每个找到的单个数字在新行上打印每个结果.它也忽略了我创建的列表.

我想要做的是将所有数字放在一个列表中.我使用了join()但它不起作用.

代码:

def readfile():
    regex = re.compile('\d+')
for num in regex.findall(open('/path/to/file').read()):
    lst = [num]
    jn = ''.join(lst)    
    print(jn)
Run Code Online (Sandbox Code Playgroud)

输出:

122
34
764
Run Code Online (Sandbox Code Playgroud)

python python-3.x

5
推荐指数
1
解决办法
67
查看次数

当我输入'\ t'字符时,输出结果为换行符

我是Python的新手,今天我正在基于列表在Python3.3上编写一个简单的测试程序.所以,我注意到当我输入制表空间字符时\t,输出被闪现,这样我就已经投入了新行字符!下面给出了一个样本:

 def printlist(list_name,tabs=0):
         for items in list_name:
           if isinstance(items,list):
             printlist(items,tabs)
           else:
             print(items)
             for num in range(tabs):
                 print('\t') #tab-stops provide

    list3 = [
             'list no. 3',
             ['tree','stems','root'],
             ['human','hand','leg'],
             ['robot','microprocessor','motor']
            ]

   printlist(list3,1)
Run Code Online (Sandbox Code Playgroud)

输出是:

    >>> ================================ RESTART ================================
>>> 
list no. 3


tree


stems


root


human


hand


leg


robot


microprocessor


motor


>>>
Run Code Online (Sandbox Code Playgroud)

但我想要的输出格式是:

    list no. 3
    tree
    stems
    root
    human
    hand
    leg
    robot
    microprocessor
    motor
Run Code Online (Sandbox Code Playgroud)

[ 我希望标签不是新行 ]

那怎么可能呢?

python tabs newline python-3.x

0
推荐指数
1
解决办法
186
查看次数

如何在Python中的同一行上打印

Python 可以在同一行打印文本吗?

例如,而不是有

1
2
3
Run Code Online (Sandbox Code Playgroud)

我会

1 2 3
Run Code Online (Sandbox Code Playgroud)

提前致谢!

python

0
推荐指数
1
解决办法
2万
查看次数

如何在一行中打印列表元素?

我需要打印排序的整数列表,但它应该在一行中,没有列表方括号,最后没有任何'\n'...

import random
n = int(input(""))
l=[]
for i in range(n):
    x = int(input())
    l.append(x)
not_sorted = True
while not_sorted:    
    x = random.randint(0,n-1)
    y = random.randint(0,n-1)
    while x==y:
        y = random.randint(0,n-1)
    if x>y:
        if l[x]<l[y]:
            (l[x],l[y])=(l[y],l[x])
    if x<y:
        if l[x]>l[y]:
            (l[x],l[y])=(l[y],l[x])
    for i in range(0,n-1):
        if l[i]>l[i+1]:
            break
    else:
       not_sorted = False
for i in range(n):
    print(l[i])
Run Code Online (Sandbox Code Playgroud)

输出应该像这样::: 1 2 3 4 5而不是这样:::: [1,2,3,4,5]

python

-2
推荐指数
1
解决办法
2289
查看次数

仅在一行上打印

我有一个程序只能将数据打印到一行上。

我该怎么做才能做到这一点。

for i in range(10):
    print(i)
Run Code Online (Sandbox Code Playgroud)

是否可以将所有这些打印在一行上,以便打印 0、擦除该行、打印 2、擦除等......?

python printing

-5
推荐指数
1
解决办法
604
查看次数

标签 统计

python ×5

python-3.x ×2

newline ×1

printing ×1

tabs ×1