编辑列表中每个第N项的值

时间:2016-03-15 12:32:52

标签: python python-3.x list

对列表中的每个第n个值执行算术运算的最pythonic方法是什么?例如,如果我从list1开始:

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

我想在每个第二项添加1,这将给出:

list2 = [1, 3, 3, 5, 5, 7, 7, 9, 9, 11]

我试过了:

list1[::2]+1

还有:

for x in list1:
    x=2        
    list2 = list1[::x] + 1

7 个答案:

答案 0 :(得分:16)

您可以将#include <windows.h> #include <string> #include <cassert> #include <iostream> /* ... */ STARTUPINFO startUp; PROCESS_INFORMATION procInfo; /* Start program in paused state */ PROCESS_INFORMATION procInfo; if (!CreateProcess(NULL, CmdParams, NULL, NULL, TRUE, CREATE_SUSPENDED | NORMAL_PRIORITY_CLASS, NULL, NULL, &startUp, &procInfo)) { DWORD err = GetLastError(); // TODO format error message std::cerr << "Unable to start the process: " << err << std::endl; return 1; } HANDLE hProc = procInfo.hProcess; /* Create job object and attach the process to it */ HANDLE hJob = CreateJobObject(NULL, NULL); // XXX no security attributes passed assert(hJob != NULL); int ret = AssignProcessToJobObject(hJob, hProc); assert(ret); /* Now run the process and allow it to spawn children */ ResumeThread(procInfo.hThread); /* Block until the process terminates */ if (WaitForSingleObject(hProc, INFINITE) != WAIT_OBJECT_0) { DWORD err = GetLastError(); // TODO format error message std::cerr << "Failed waiting for process termination: " << err << std::endl; return 1; } DWORD exitcode = 0; ret = GetExitCodeProcess(hProc, &exitcode); assert(ret); /* Calculate wallclock time in nanoseconds. Ignore user and kernel times (third and fourth return parameters) */ FILETIME createTime, exitTime, unusedTime; ret = GetProcessTimes(hProc, &createTime, &exitTime, &unusedTime, &unusedTime); assert(ret); LONGLONG createTimeNs = (LONGLONG)createTime.dwHighDateTime << 32 | createTime.dwLowDateTime; LONGLONG exitTimeNs = (LONGLONG)exitTime.dwHighDateTime << 32 | exitTime.dwLowDateTime; LONGLONG wallclockTimeNs = exitTimeNs - createTimeNs; /* Get total user and kernel times for all processes of the job object */ JOBOBJECT_BASIC_ACCOUNTING_INFORMATION jobInfo; ret = QueryInformationJobObject(hJob, JobObjectBasicAccountingInformation, &jobInfo, sizeof(jobInfo), NULL); assert(ret); if (jobInfo.ActiveProcesses != 0) { std::cerr << "Warning: there are still " << jobInfo.ActiveProcesses << " alive children processes" << std::endl; /* We may kill survived processes, if desired */ TerminateJobObject(hJob, 127); } /* Get kernel and user times in nanoseconds */ LONGLONG kernelTimeNs = jobInfo.TotalKernelTime.QuadPart; LONGLONG userTimeNs = jobInfo.TotalUserTime.QuadPart; /* Clean up a bit */ CloseHandle(hProc); CloseHandle(hJob); 与列表推导结合使用,如下所示:

slicing

答案 1 :(得分:8)

numpy也允许您对切片使用+=操作:

In [15]: import numpy as np

In [16]: l = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

In [17]: l[1::2] += 1

In [18]: l
Out[18]: array([ 1,  3,  3,  5,  5,  7,  7,  9,  9, 11])

答案 2 :(得分:4)

使用enumeratelist comprehension

>>> list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> [v+1 if i%2!=0 else v for i,v in enumerate(list1)]
[1, 3, 3, 5, 5, 7, 7, 9, 9, 11]

答案 3 :(得分:3)

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(1, len(list1), 2):
    list1[i] +=1
print(list1)

使用i%2似乎效率不高

答案 4 :(得分:1)

试试这个:

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in range(1,len(list1),2):
    list1[i] += 1

答案 5 :(得分:1)

您可以创建表示增量的迭代器(itertools.cycle([0, 1]),然后将其元素添加到现有列表中。

>>> list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> [a + b for a,b in zip(list1, itertools.cycle([0,1]))]
[1, 3, 3, 5, 5, 7, 7, 9, 9, 11]
>>>

答案 6 :(得分:1)

/usr/bin