import os
import sys
pid = os.fork()
print ("second test")
if pid == 0:
    print ("this is the child")
    print ("I'm going to exec another program now")
    os.execl("python", "test.py", * sys.argv)
else:
    print ("the child is pid %d" % pid)
    os.wait()
Run Code Online (Sandbox Code Playgroud)
我到处查看示例,但对于我的生活,我根本无法理解。我认为这会起作用,但我收到此错误:
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    os.execl("python", "test.py", * sys.argv)
  File "/usr/local/lib/python3.8/os.py", line 534, in execl
    execv(file, args)
FileNotFoundError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud) class RW {
    int a;
public:
    int read() const {
        return this->a;
    }
    void write(int _a) {
        this->a = _a;
    }
};
#define PHYSICAL_ADDRESS (0x60000000)
#define SIZEOF_PHY_ADDR  (sizeof(RW))
// assume physical memory area is already assigned for the sizeof(RW)
void main()
{
    int val;
    void *phy_ptr = PHYSICAL_ADDRESS;
    RW *rw_ptr = (RW *)phy_ptr;
    rw_ptr->write(1);
    val = rw_ptr->read();
}
Run Code Online (Sandbox Code Playgroud)
请假设上面的代码是伪代码。我有一个可读/写的共享物理内存区域。我想将该区域的指针强制转换为读写。我可以这样做吗?可以接受吗?
我已经检查过它工作正常,但我不确定它是否是正确的 cpp 方式。
我感谢任何回应和讨论!提前致谢!
假设我们有一个列表a=[1,2,3],我需要将 的元素复制a到新列表b。
我们可以做的a=b,但两者a并b指向同一个列表。因此,改变它们中的任何一个都会改变两个列表。
>>> a=b
>>> a
[1, 2, 3]
>>> b
[1, 2, 3]
>>> b.append(4)
>>> a,b
([1, 2, 3, 4], [1, 2, 3, 4])
>>> a.append(5)
>>> a,b
([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])
>>> a is b
True
>>> id(a),id(b)
(2287659980360, 2287659980360)
Run Code Online (Sandbox Code Playgroud)
为了避免这种情况,我们可以这样做b=a[:]。a[:]创建一个具有相同值的不同列表a。现在,即使我 mutate a,b也不会受到影响,反之亦然。b并且b[:]是两个不同的列表。
>>> b=a[:] …Run Code Online (Sandbox Code Playgroud) 我的排序有问题。想要对列表进行排序
['asd_1qwer', 'asd_14qwer', 'asd_26qwer', 'asd_5qwer']
Run Code Online (Sandbox Code Playgroud)
我发现我需要在 1 和 5 上加零。
['asd_01qwer', 'asd_05qwer', 'asd_14qwer', 'asd_26qwer']
Run Code Online (Sandbox Code Playgroud)
不知道如何将其添加到正确的位置,因为asd不是静态的。
list = ['asd_14qwer','asd_5qwer','asd_26qwer','asd_1qwer']
list.sort()
for i in list:
    tempo = i.split('_')[-1].split('qwer')[0]
    if len(tempo) == 1:
        i[:4] + '0' + i[4:]
Run Code Online (Sandbox Code Playgroud)
编辑需要将 0 添加到 1-9 并qwer在所有标签上列出常量。
鉴于以下列表:
lst = [[3,5],[3,10],[3,15],[3,102],[5,21],[5,23],[5,50]]
Run Code Online (Sandbox Code Playgroud)
我想获得以下
 [[3,5,10,15,102], [5,21,23,50]]
请注意,列表是根据第一个元素的值按升序排序的。
这样做的最有效方法是什么?这就是我的想法:
第 1 步:创建一个具有唯一第一个元素的列表。(即本例中的 3 和 5)
first_elements = [] #initialize empty list to which we will append all first elements
for i in range(len(lst)):
    first_elements.append(lst[i][0])
first_elements = list(set(first_elements)) #Filter out the unique first elements
Run Code Online (Sandbox Code Playgroud)
    first_elements = [3,5]
Run Code Online (Sandbox Code Playgroud)
第 2 步:根据第一个元素过滤 lst。将这些附加到新列表中。
new_merged_list = [] # create new list to append to
for i in range(len(first_elements)): 
    first_element_to_filter_by = first_elements[i]
    filtered_2d_list           = [i for i in lst if i[0] == first_element_to_filter_by]
    new_merged_list.append([first_element_to_filter_by])
    for …Run Code Online (Sandbox Code Playgroud) 我计时set()和list()构造函数。set()明显慢于list(). 我使用不存在重复项的值对它们进行了基准测试。我知道 set use hashtables 是它变慢的原因吗?
在撰写本文时(3 月8日),我正在使用 Python 3.7.5 [MSC v.1916 64 位 (AMD64)]、Windows 10。
#No significant changed observed.
timeit set(range(10))
517 ns ± 4.91 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
timeit list(range(10))
404 ns ± 4.71 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)Run Code Online (Sandbox Code Playgroud)
当大小增加set()变得非常慢时list()
# When …Run Code Online (Sandbox Code Playgroud) 我想附加到字符串列表中字典中的各个键
myDictionary = {'johny': [], 'Eli': [], 'Johny': [], 'Jane': [], 'john': [], 'Ally': []}
votes = ['johny', 'Eli', 'Eli', 'Jane', 'Ally', 'Johny', 'john', 'Eli']
outPut={'johny': ['johny'], 'Eli': ['Eli','Eli'], 'Johny': ['Johny'], 'Jane': ['Jane'], 'john': ['john'], 'Ally': ['Ally']}
Run Code Online (Sandbox Code Playgroud)
我试图这样做,但在每个键中附加整个列表
votes_dictionary={}
votes_dictionary=votes_dictionary.fromkeys(votes,[])
for i in votes:
    print(i.lower())
    votes_dictionary[i].append(i)
print(votes_dictionary)
Run Code Online (Sandbox Code Playgroud) 字,字,字......对不起这个标题。
假设我想将字符串中的每个“是”实例替换为“否”。我可以只使用 string.replace()。但随之而来的就是这个问题:
string = "yes eyes yesterday yes"
new_str = string.replace("yes", "no")
# new_str -> "no eno noterday no"
Run Code Online (Sandbox Code Playgroud)
我怎样才能保留“眼睛”和“昨天”,将“是”改为“否”。
挑战是在 python 中找到第一个非连续数字。我已经成功地使用一个名为 more_itertools 的库来做到这一点,但挑战要求不涉及任何库。我该如何解决?这是我的代码:
from more_itertools import consecutive_groups
def first_non_consecutive(l):
    g = []
    for grp in consecutive_groups(l):
        g.append(list(grp))
    if len(g) > 1:
        return g[1][0]
Run Code Online (Sandbox Code Playgroud) 我有一个类似于以下的数据集:
    dt = {'A': [0,0,0,1], 
          'B': [0, 2,0,3],
          'C': [0,0,0,4],
          'D': [0,5,0,6]}
    dt = pd.DataFrame(dt)
Run Code Online (Sandbox Code Playgroud)
我的目标是在列 ['A', 'B','C', 'D'] 对于该行都为零时过滤所有行。在实际数据集中,而不是 4 列,我有超过 20 列。所以下面的解决方案是不可行的:
    dt = dt[(dt['A'] == 0) & (dt['B'] == 0) & (dt['C'] == 0) & (dt['D'] == 0)]
Run Code Online (Sandbox Code Playgroud)
所以我想出了以下解决方案:
    dt['new'] = np.nan
    lst = [0,1,2,3]
    for i in range(len(dt)):
        dt.iloc[i, 4] = all(dt.iloc[i, lst] == 0) 
Run Code Online (Sandbox Code Playgroud)
最后我可以根据“新”列进行过滤。
我正在寻找更有效的解决方案,最好是没有循环的解决方案,任何帮助将不胜感激。
python ×9
python-3.x ×7
list ×5
c++ ×1
dataframe ×1
dictionary ×1
exec ×1
filter ×1
merge ×1
optimization ×1
pandas ×1
performance ×1
replace ×1
set ×1
slice ×1
string ×1