Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何更改列表的视图?_Python_List - Fatal编程技术网

Python 如何更改列表的视图?

Python 如何更改列表的视图?,python,list,Python,List,晚上好!我有下面的代码,当您编写 命令提示符下的python new.py-s13-p5 命令提示符打印的是: [[1[0]、[0]]、[1[0]、[0]]、[1[0]、[0]]、[1[0]]、[1[0]]、[1[0]]] 但我想: [[1,0,0],[1,0,0],[1,0,0],[1,0],[1,0],[1,0]] 我该怎么做 -s12是字符串的长度,-p7是1s 谢谢大家! 我的代码示例: import argparse p = argparse.ArgumentParser() p.

晚上好!我有下面的代码,当您编写 命令提示符下的python new.py-s13-p5

命令提示符打印的是:
[[1[0]、[0]]、[1[0]、[0]]、[1[0]、[0]]、[1[0]]、[1[0]]、[1[0]]]

但我想:
[[1,0,0],[1,0,0],[1,0,0],[1,0],[1,0],[1,0]]

我该怎么做

-s12是字符串的长度,-p7是1s

谢谢大家!

我的代码示例:

import argparse


p = argparse.ArgumentParser()
p.add_argument("-pulses", help = "number of pulses", type = int)
p.add_argument("-slots", help = "length of the rythm", type = int)
args = p.parse_args()
slots = args.slots
pulses = args.pulses


pauses = slots - pulses

mod = slots % pulses

rhythm = []

if mod != 0:
    i = 0
    j = 0
    temp = []

    while i < pulses:
        rhythm.append([1])
        i = i + 1

    while j < pauses:
        rhythm.append([0])
        j = j + 1

    m = slots
    n = pauses

    while (rhythm[-1]==[0]):

        if (n!=0):
            step = m%n
            hlp = n
            m = n
            n = step

            i = 0

            while (i<step):
                rhythm[i].append(rhythm[-1])
                rhythm.remove(rhythm[-1])
                i = i + 1

print (rhythm)
import argparse
p=argparse.ArgumentParser()
p、 添加参数(“-pulses”,help=“脉冲数”,type=int)
p、 添加参数(“-slots”,help=“rythm的长度”,type=int)
args=p.parse_args()
插槽=args.slots
脉冲=args.pulses
暂停=插槽-脉冲
mod=插槽%脉冲
节奏=[]
如果mod!=0:
i=0
j=0
温度=[]
而我:
节奏。追加([1])
i=i+1
当j<暂停时:
节奏。追加([0])
j=j+1
m=插槽
n=暂停
而(节奏[-1]=[0]):
如果(n!=0):
步长=m%n
hlp=n
m=n
n=步进
i=0

而(i注意:这只是对评论的复制和粘贴

退房


我还没有完全分析您的代码,但我相信您的问题在于
append()
。尝试将其替换为
extend()

Woozy Coder肯定正确地回答了这个问题。 更一般地说,关于列表的附加和扩展:

  • dest_list.append(追加的_项)将一项追加到列表中。如果追加的项是列表,则此列表将按原样追加,并成为目标列表末尾的附加项

  • extend(list\u extension)用另一个列表扩展一个列表,另一个列表的每一项都单独附加到目标列表的末尾


    • 问题出在这条线路上

      rhythm[i].append(rhythm[-1])
      
      节奏[-1]
      返回一个列表(
      [0]
      [1]
      )。因此必须使用
      扩展
      而不是
      附加

      rhythm[i].extend(rhythm[-1])
      

      您的代码似乎过于复杂。我认为下面的代码完成了您的任务:

      def rhythm_list(slots, pulses):
          q, r = divmod(slots, pulses)
          full = [1] + [0] * q
          part = full[:-1]
          return [full] * r + [part] * (pulses - r)
      
      # Test
      print(rhythm_list(13, 5))
      print(rhythm_list(12, 7))
      print(rhythm_list(12, 4))
      
      输出

      [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0], [1, 0]]
      [[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1], [1]]
      [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]
      
      请注意,输出列表包含对
      完整
      部分
      列表的重复引用,因此如果您这样做:

      m = rhythm_list(12, 7)
      m[0][0]=2
      
      然后
      m
      变成:

      [[2, 0], [2, 0], [2, 0], [2, 0], [2, 0], [1], [1]]
      
      如果不希望出现这种行为,则将
      节奏列表的最后一行更改为

      return [full[:] for _ in range(r)] + [part[:] for _ in range(pulses - r)]
      

      查看。我还没有完全分析您的代码,但我相信您的问题存在于
      append()
      。尝试将其替换为
      extend()
      。非常好,这完全有效!非常感谢!很高兴听到@Veligkekas G。如果下面的答案之一解决了您的问题,您应该接受它(单击相应答案旁边的复选标记)。这样做有两件事。它让所有人都知道您的问题已得到满意的解决,并让帮助您的人对您的帮助感到满意。请参阅以获取完整的解释。我不知道,我只是这样做了:)谢谢@pm2ring这真的很有帮助!谢谢