简体   繁体   English

从Python中另一个函数的列表中提取值

[英]Extract value from a list in another function in Python

I am programming a robot and I want to use an Xbox Controller using pygame. 我正在编程一个机器人,我想使用pygame使用Xbox控制器。 So far this is what I got (original code credits to Daniel J. Gonzalez): 到目前为止,这是我得到的(Daniel J. Gonzalez的原始代码):

"""
Gamepad Module
Daniel J. Gonzalez
dgonz@mit.edu

Based off code from: http://robots.dacloughb.com/project-1/logitech-game-pad/
"""

import pygame


"""
Returns a vector of the following form:
[LThumbstickX, LThumbstickY, Unknown Coupled Axis???, 
RThumbstickX, RThumbstickY, 
Button 1/X, Button 2/A, Button 3/B, Button 4/Y, 
Left Bumper, Right Bumper, Left Trigger, Right Triller,
Select, Start, Left Thumb Press, Right Thumb Press]

Note:
No D-Pad.
Triggers are switches, not variable. 
Your controller may be different
"""

def get():

    out = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    it = 0 #iterator
    pygame.event.pump()

    #Read input from the two joysticks       
    for i in range(0, j.get_numaxes()):
        out[it] = j.get_axis(i)
        it+=1
    #Read input from buttons
    for i in range(0, j.get_numbuttons()):
        out[it] = j.get_button(i)
        it+=1
    first = out[1]
    second = out[2]
    third = out[3]
    fourth = out[4]

    return first, second, third, fourth

def test():
    while True:
        first, second, third, fourth = get()

pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
print 'Initialized Joystick : %s' % j.get_name()
test()

Do you see the list called "out"? 你看到名单“out”了吗? Each element in it is a button on the Xbox Controller. 其中的每个元素都是Xbox控制器上的一个按钮。 I want to extract those elements and put them on variables, one variable to each element/button so I can control my robot. 我想提取这些元素并将它们放在变量上,每个元素/按钮都有一个变量,这样我就可以控制我的机器人。

How could I do it? 我怎么能这样做? I've tried to use global variables but then everything turned down to a mess. 我试图使用全局变量,但后来一切都变得一团糟。 Please note that I am a beginner in Python. 请注意我是Python的初学者。

If you want to have out in your program then just return it from your function get : 如果你想有out在你的程序然后就从你的函数返回它get

def get():
  # rest of the code ...
  return out

Also change your function test: 还要更改功能测试:

def test():
    while True:
        out = get()
        LThumbstickX = out[0]
        LThumbstickY = out[1]
        # and so on

Then run your program as before. 然后像以前一样运行你的程序。 What the function test does is constantly ( while True ) read the keypad. 功能test作用是不断( while True )读取键盘。 You could for example do: 你可以这样做:

def test():
    while True:
        out = get()
        LThumbstickX = out[0]
        if LThumbstickX != 0:
            print 'Left button has been pressed'
            # and so on

You can just return the list and use python's unpacking feature: 您只需返回列表并使用python的解包功能:

def get():
    out = [1,2,3,4]
    return out

first, second, third, fourth = get()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Python 的嵌套列表中提取 JSON 值 - Extract JSON Value From Nested List in Python 从 python 中的 dict 列表中提取 dict 值 - Extract dict value from list of dict in python 从列表中提取一个值并将其添加到另一个列表的相应行中? - Extract a value from a list and add it in the corresponding row of another list? 在python中使用条件的另一个列表的帮助从列表中提取值 - extract values from list with the help of another list with condition in python 根据属性从另一个Python列表中提取对象列表 - Extract list of objects from another Python list based on attribute 如何从列表中提取值以添加到python中的另一个列表 - How to extract values from list to add to another list in python 多处理的返回值并将其提取为 python 中另一个 function 的输入 - return value of multiprocessing and extract it as an input of another function in python 如何根据另一个键的值从嵌套(不总是存在的)字典列表中提取给定键的值 [Python] - How to extract the value of a given key based on the value of another key, from a list of nested (not-always-existing) dictionaries [Python] Python:通过从另一个数组比较从数组中提取索引值 - Python: Extract the index value from the array by comparing from another array 如何根据索引值从另一个列表中提取子列表 - How to extract a sublist from another list based on value with indices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM