另一位Python新手试图移植到C / C ++问题。 [英] Another Python rookie trying to port to C/C++ question.

查看:87
本文介绍了另一位Python新手试图移植到C / C ++问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



一两个星期前,我在这里问到有关将Python移植到C的问题。得到了一些好的

答案(注1)但现在我又得到了另一个题。实际上,更多的请求澄清一个主题,Python教程和

文档给我的理解留下了一丝阴影。


词典/词典类型......


当我认为

" dict"时,我是否理解/正确理解?变量可以看作(有效)两个并行数组吗?

像这样的C构造:


struct DictStruct

{

char * KeyArray [< somenumber>];

ValType ValArray [< somenumber>];

}


其中ValType可能是一个联盟,允许

正确地分配/检索实际值取决于他们的个人类型是什么,以及< somenumber>就是这样:一些

或多或少的任意选择值,提供足够的空间来处理所有预期数量的键/值对?


同样,类似于:


struct DictStruct

{

char Key [25];

ValType值;

DictStruct * PrevDictEntry;

DictStruct * NextDictEntry;

}

(同样,ValType可能是一个联盟,涵盖了字典中可能需要的每种类型的值

- 但在这种情况下,Key直接

包含Key的名称,然后构建一个双链接结构列表

DictStructs最终表现得至少像Python一样dict?


我希望其中任何一个都是Python

处理dicts的合理传真,当然还有相应的开销(可能很少,我可能很少,我我在想......但我还没有编写那部分代码,

所以我可能会发现这将是一场噩梦)我们希望价值

与关键一致,所以我们有走到KeyArray []直到我们找到一个

匹配,然后从另一个数组中提取值,或者为双向链表列出类似的

过程。


或者我在离开场地的方式我是RTFM吗?


(注1)

除了相当多的完全不受欢迎的Python传福音之外。 -

与C / C ++或任何其他语言相比,我不在乎Python有多好,坏或无关紧要的部分是什么?b $ b。我希望这个Python代码是用C / C ++编写的b $ b,讨论结束。对于那些自己接受传教的人来说是不够清楚的(包括那个因为b $ b决定吐出相对令人印象深刻的白痴(就像这样的事情一样)一串侮辱我是多么愚蠢,我的屁股到底有多远,以及如何可怜我的家人一直回到亚当必须是为了制作这样的一个

的回归,因为我没有兴趣采用完美的

语言......)来看看我的Python优点和$ / b $ b in-mailbox?


-

Don Bruder - da **** @ sonic.net < ---首选电子邮件 - SpamAssassinated。

仇恨垃圾邮件?见< http://www.spamassassin.org>对于一些非常好的信息。

我会选择一条清晰的道路:我会选择自由意志! - N. Peart

飞行陷阱信息页面:< http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>


A week or two ago, I asked here about porting Python to C. Got some good
answers (Note 1) but now I''ve got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.

Dictionaries/"dict" types...

Am I understanding/interpreting correctly when I go with the idea that a
"dict" variable can be looked at as (effectively) two parallel arrays?
Something like this C construct:

struct DictStruct
{
char *KeyArray[<somenumber>];
ValType ValArray[<somenumber>];
}

Where "ValType" would be likely be a union, to allow
assigning/retrieving the actual values correctly depending on what their
individiual types were, and "<somenumber>" is just that: Some
more-or-less arbitrarily selected value that provides enough space to
handle all expected numbers of key/value pairs?

Similarly, would something like:

struct DictStruct
{
char Key[25];
ValType Value;
DictStruct *PrevDictEntry;
DictStruct *NextDictEntry;
}

(again, ValType would probably be a union to cover each type of value
that might be wanted in the dictionary - but in this case, Key directly
contains the Key''s name) then building a double-linked list of struct
DictStructs end up behaving at least reasonably like a Python "dict"?

I expect that either will be a reasonable facsimile of how Python
handles dicts, with, of course, the corresponding overhead (which may be
surprisingly little, I''m thinking... but I have yet to code that part,
so I may find out it''s going to be a nightmare) of "We want the value
that goes with key, so we''ve got to walk the KeyArray[] until we find a
match, then extract the value from the other array", or a similar
procedure for the doubly-linked list.

Or am I way out in left field with the way I''m RTFM?

(Note 1)
Along with quite a bit of entirely unwelcome "Python evangelism" --
Which part of "I don''t care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn''t clear enough for those of
you who took it upon yourselves to preach (including the idiot who
decided to spew a relatively impressive (as such things go) string of
insults about how stupid I am, how far up my ass my head is, and how
pathetic my family all the way back to Adam must be for producing such a
throwback because I''ve got no interest in adopting "the perfect
language...") at me on the merits of Python, both on group and
in-mailbox?

--
Don Bruder - da****@sonic.net <--- Preferred Email - SpamAssassinated.
Hate SPAM? See <http://www.spamassassin.org> for some seriously great info.
I will choose a path that''s clear: I will choose Free Will! - N. Peart
Fly trap info pages: <http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>

推荐答案

Don Bruder< da **** @ sonic.net>写道:


[用C / C ++重写Python程序]
Don Bruder <da****@sonic.net> writes:

[rewriting a Python program in C/C++]
像这样的C构造:

struct DictStruct
{
char * KeyArray [< somenumber>];
ValType ValArray [< somenumber>];
}

[...]同样,会是这样的:

struct DictStruct
{char Key [25];
ValType值;
DictStruct * PrevDictEntry;
DictStruct * NextDictEntry;
}

[...]最终表现得至少像Pythondict一样合理?
Something like this C construct:

struct DictStruct
{
char *KeyArray[<somenumber>];
ValType ValArray[<somenumber>];
}
[...] Similarly, would something like:

struct DictStruct
{
char Key[25];
ValType Value;
DictStruct *PrevDictEntry;
DictStruct *NextDictEntry;
}

[...] end up behaving at least reasonably like a Python "dict"?




是的。


除了它不太灵活,*多*慢,和

可能更多的马车。


Thomas



Yes.

Except that it would be less flexible, *much* slower, and
probably much more buggy.

Thomas


Don Bruder写道:
Don Bruder wrote:
一两个星期前,我在这里问过将Python移植到C.得到了一些很好的答案(注1)但现在我又得到了另一个问题ñ。实际上,更多的要求澄清一个主题,Python教程和
文档给我的理解留下了一丝阴影。

字典/字典类型...


你似乎对C感到宾至如归,你可以看看

Objects / dictobject.c和Include / dictobject。 h源代码分发。

与C / C ++或任何其他语言相比,我不关心Python有多好,坏或无关紧要。我希望这个Python代码用C / C ++编写,讨论结束。对于
A week or two ago, I asked here about porting Python to C. Got some good
answers (Note 1) but now I''ve got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.

Dictionaries/"dict" types...
As you seem to feel at home with C, you could have a look into
Objects/dictobject.c and Include/dictobject.h of the source distribution.
Which part of "I don''t care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn''t clear enough for those of

的人来说还不够清楚


聪明的人使用高效的工具并在现有的库上构建。如果你对主题行的C ++部分非常认真,那么标准库又名

STL有一个类似Python的字典的地图模板,可能会增强两者br />
开发和执行速度。


关闭主题(或不关闭):

一个人可以开始讨论但不限制它,我认为这是新闻组的特色而不是烦恼。

彼得



Smart people use efficient tools and build on existing libraries. If you are
serious about the C++ part of your subject line, the Standard library aka
STL has a map template that resembles Python''s dict and might enhance both
development and execution speed.

Off topic (or not):
A single person can start a discussion but not limit it, I think that''s a
feature of newsgroups rather than an annoyance.
Peter


Don Bruder写道:
Don Bruder wrote:
一两个星期前,我在这里问到有关将Python移植到C的问题。得到了一些好的答案(注1)但现在我已经有另一个问题。实际上,更多的要求澄清一个主题,Python教程和
文档给我的理解留下了一丝阴影。

字典/字典类型...


你似乎对C感到宾至如归,你可以看看

Objects / dictobject.c和Include / dictobject。 h源代码分发。

与C / C ++或任何其他语言相比,我不关心Python有多好,坏或无关紧要。我希望这个Python代码用C / C ++编写,讨论结束。对于
A week or two ago, I asked here about porting Python to C. Got some good
answers (Note 1) but now I''ve got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.

Dictionaries/"dict" types...
As you seem to feel at home with C, you could have a look into
Objects/dictobject.c and Include/dictobject.h of the source distribution.
Which part of "I don''t care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn''t clear enough for those of

的人来说还不够清楚


聪明的人使用高效的工具并在现有的库上构建。

标准库(又名STL)有< map>模板可以提高开发和执行速度。


关闭主题(或不关闭):

一个人可以开始一个讨论,但不是结束它,我认为这是新闻组的特色而不是烦恼。

彼得



Smart people use efficient tools and build on existing libraries. The
Standard library aka STL has a <map> template that might enhance both
development and execution speed.

Off topic (or not):
A single person can start a discussion but not end it, I think that''s a
feature of newsgroups rather than an annoyance.
Peter


这篇关于另一位Python新手试图移植到C / C ++问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆