【问题标题】:Accessing the elements of a list - python访问列表的元素 - python
【发布时间】:2021-03-05 11:47:07
【问题描述】:

我有一个具有以下结构的列表,我想分别获得每个元素的信息,例如位置。

joint_trajectory: 
  header: 
    seq: 0
    stamp: 
      secs: 0
      nsecs:         0
    frame_id: "world"
  joint_names: 
    - shoulder_pan_joint
    - shoulder_lift_joint
    - elbow_joint
    - wrist_1_joint
    - wrist_2_joint
    - wrist_3_joint
  points: 
    - 
      positions: [-0.00025209861478181494, 0.004307795221944133, 0.0006349868678086068, 0.0010435817857663565, -4.234193532681729e-05, 3.768785300195532e-05]
      velocities: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
      accelerations: [-0.6884183719945808, 0.0, 0.0, 0.0, 0.0, 0.0]
      effort: []
      time_from_start: 
        secs: 0
        nsecs:         0
    - 
      positions: [-0.04992968849098622, -0.08433208536812643, -0.13655855187577923, 0.22660211433504318, 0.028808576199230457, 0.15709421547345312]
      velocities: [-0.2239121377558192, -0.39952713492749836, -0.6183733676072944, 1.0166615034673043, 0.1300399389709771, 0.7079019520801277]
      accelerations: [-0.6943738586354055, -1.2389734700835597, -1.9176374521112265, 3.1527686625836706, 0.40326681306781165, 2.1952745167278875]
      effort: []
      time_from_start: 
        secs: 0
        nsecs: 379899770
    - 
      positions: [-0.09960727836719062, -0.172971965958197, -0.27375209061936706, 0.45216064688432, 0.05765949433378773, 0.3141507430939043]
      velocities: [-0.36247695177120814, -0.6467687704198166, -1.0010448544385069, 1.6458078889936585, 0.21051328953911835, 1.1459769189588018]
      accelerations: [-0.652382575660194, -1.1640482911845413, -1.8016710229404767, 2.9621094097625744, 0.37887981944549876, 2.0625183763666244]
      effort: []
      time_from_start: 
        secs: 0
        nsecs: 536582108
    - 
      positions: [-0.149284868243395, -0.2616118465482675, -0.4109456293629549, 0.6777191794335968, 0.086510412468345, 0.47120727071435536]
      velocities: [-0.44476904942193296, -0.7936028202890654, -1.2283091825886876, 2.019450910580548, 0.2583055149340806, 1.4061447560022973]
      accelerations: [-0.6557433669411694, -1.170044961686803, -1.8109524484275814, 2.977368909094765, 0.38083164348439513, 2.0731435739650403]
      effort: []
      time_from_start: 
        secs: 0
        nsecs: 658372363
    - 
      positions: [-0.19896245811959942, -0.35025172713833813, -0.5481391681065427, 0.9032777119828737, 0.11536133060290228, 0.6282637983348066]
      velocities: [-0.515602863886791, -0.9199918192632637, -1.4239294148374249, 2.3410681888216205, 0.2994431906421152, 1.630087039951357]
      accelerations: [-0.7018697640759863, -1.2523484378471594, -1.9383387340382296, 3.1868034343091045, 0.40762015941069035, 2.2189729465994303]
      effort: []
      time_from_start: 
        secs: 0
        nsecs: 761514193
    - 
      positions: [-0.2486400479958038, -0.4388916077284087, -0.6853327068501306, 1.1288362445321505, 0.14421224873745955, 0.7853203259552577]
      velocities: [-0.5753051920982908, -1.0265188721417164, -1.5888080592921592, 2.612143528320158, 0.33411610830911154, 1.8188369447498993]
      accelerations: [-0.5950528657343278, -1.0617547086102592, -1.6433447877210596, 2.701806821118893, 0.34558483126529427, 1.8812695437867313]
      effort: []
      time_from_start: 
        secs: 0
        nsecs: 851909030

我打印列表,但是当我尝试访问每个元素时,错误是该元素未定义 AttributeError: 'list' 对象没有属性 'joint_names'

这是代码:

pred_plans = []
    pred_plans.append(copy.copy(plan1))
    print(type(pred_plans))        

    for p in pred_plans.joint_names: print p

【问题讨论】:

    标签: python list python-2.7


    【解决方案1】:

    这可能是评论,但我的声誉!

    python 中的列表是使用索引访问的,您可以尝试 pred_plans[i],其中 i 的限制可以由 len(pred_plans) 给出。 这可能会有所帮助:

    https://www.geeksforgeeks.org/iterate-over-a-list-in-python/

    python 中的字典是使用键访问的。

    【讨论】:

    • 是的,这是真的。但在这种情况下,一切都在索引 0 中(例如:打印 pred_plans [0]),打印整个列表,我以后如何访问其他部分?当像这样打印时,整个结构都会出现,因此我的问题
    • 您需要对嵌套列表使用嵌套的 for 循环。如果不想遍历整个列表,您也可以使用“break”语句。字典看起来更适合您的情况。
    猜你喜欢
    • 2018-09-12
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    相关资源
    最近更新 更多