微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

每次重复函数时从列表中添加新数字Python 3

如何解决每次重复函数时从列表中添加新数字Python 3

我有这个清单:

list_vin = ['D03960','D03987','D04014']

这个使用列表的函数

def lol():
    pyautogui.click(699,60)
    pyautogui.hotkey('command','f')
    pyautogui.typewrite(list_vin[2])
    pyautogui.press('enter')
    time.sleep(0.5)
    pyautogui.hotkey('command','p')
    pyautogui.press('enter')
lol()

还有这个函数重复列表:

def refresh():
    schedule.every(int(1)).seconds.do(lol)
    while 1:
        schedule.run_pending()
        time.sleep(1)
refresh()

我怎样才能使每次函数重复时使用列表中的一个新项目?例如。第一次函数运行它使用这个 D03960,第二次函数运行它使用这个 D03987 在函数的这一行 pyautogui.typewrite(list_vin[2])

我是初学者,如果我的问题表述有误,请不要生气,希望您能理解。顺便说一句,您可以完全更改代码

提前致谢。

解决方法

使用.pop()

pop() 方法返回存在于给定索引处的项目。此项目也从列表中删除。

lol()

因此每次运行 list_vin 时,您的可迭代 <?xml version="1.0" encoding="UTF-8"?> <Shell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:MasterDetailTest.Views" Title="MasterDetailTest" x:Class="MasterDetailTest.AppShell"> <TabBar> <ShellContent Title="About" Icon="icon_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" /> <ShellContent Title="Browse" Icon="icon_feed.png" ContentTemplate="{DataTemplate local:ItemsPage}" /> <ShellContent Title="TestMasterDetailPage" Route="TestMasterDetailPage" ContentTemplate="{DataTemplate local:TestMasterDetailPage}" /> </TabBar> </Shell> 都会变得“更短”

,

使用计数器

@chickenshifu 的回应很棒,但请记住,如果您使用 pop,list_vin 最终将为空。请务必在您的代码中考虑到这一点。

如果您不想从列表中删除项目,您可以执行以下操作:

public class SecondActivity extends AppCompatActivity {

public DBHelper helper;
public ArrayList<String> theList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    String input =((EditText)findViewById(R.id.editText)).getText().toString();
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    helper = new DBHelper(SecondActivity.this);
    SQLiteDatabase database = helper.getReadableDatabase();
    theList = new ArrayList<>();

    Cursor cursor = database.query(DBHelper.TABLE_NAME,new String[]{DBHelper.FIELD_4},DBHelper.FIELD_1+"=?",new String[]{input},null,null);
    if (cursor.moveToFirst())
    {
        do
        {
            theList.add(cursor.getString(4));
        } while (cursor.moveToNext());
    }

    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(SecondActivity.this,android.R.layout.simple_spinner_item,theList);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerAdapter);
}

我的方法跟踪您正在使用的 list_vin 项目并通过索引抓取该项目。 'iteration' 将在 'lol' 函数结束时增加 1。

iteration = 0 def lol(index): pyautogui.click(699,60) pyautogui.hotkey('command','f') pyautogui.typewrite(list_vin[index]) pyautogui.press('enter') time.sleep(0.5) pyautogui.hotkey('command','p') pyautogui.press('enter') iteration += 1 lol(iteration) 等于 iteration 时,您知道您将到达列表的末尾。

如果您有任何问题或者我需要澄清任何事情,请告诉我!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。