我做了很多细读,但对于我想要理解的概念,我没有明确的答案.
在Python中,如果我列出一个列表,例如:
L1=['muffins', 'brownies','cookies']
Run Code Online (Sandbox Code Playgroud)
然后尝试使用代码替换第一个指向列表中对象的指针,即"松饼":
L1[0:1] = 'cake'
Run Code Online (Sandbox Code Playgroud)
我会得到一个列表L1:
['c', 'a', 'k', 'e', 'brownies', 'cookies']
Run Code Online (Sandbox Code Playgroud)
然而,如果我采用相同的列表并执行操作(现在使用字符串蛋糕中的4个元素):
L1[0:4] = ['cake'] # presumably, it's now passing the string cake within a list? (it passed into the modified list shown above)
Run Code Online (Sandbox Code Playgroud)
我得到了我最初想要的输出:
['cake', 'brownies', 'cookies']
Run Code Online (Sandbox Code Playgroud)
任何人都能解释为什么会这样吗?我假设当我最初拿蛋糕而不是在"列表"中时,它会将字符串分解为单个字符,以存储为对这些字符的引用,而不是对字符串的单个引用...
但我不完全确定.
这是我的第二篇文章,如果我做错了什么,我道歉 - 我会尽量简明扼要.
我做了一些搜索,大多数转义必须处理嵌入的JSON字符串 - 我的问题实际上是打开文件本身.
目前,我正在努力使我的代码尽可能通用,所以我正在使用:
file = open(os.path.expanduser(r'~/Desktop/Austin/Tweets/10_7_2012_12/09-Tweets.txt'), 'r')
Run Code Online (Sandbox Code Playgroud)
问题是,当解释器看到这个代码时,它会在文件名中看到"/",我认为它正试图沿着另一个目录运行.我通过删除文件名中的"/"并输入以下内容来确认:
file = open(os.path.expanduser(r'~/Desktop/Austin/Tweets/10_7_2012_1209-Tweets.txt'), 'r')
Run Code Online (Sandbox Code Playgroud)
它装得很好.
对所有这些文件执行此操作的问题在于,我有几百个包含数千条推文的文件,这有点不切实际.
所以我的问题是:有没有办法在文件名中加载带正斜杠的文件?
我看到很多方法用搜索按钮加载文件,但没有包括如何处理名称中的正斜杠...我试过:
file = open(os.path.expanduser('~/Desktop/Austin/Tweets/10_7_2012_12\/09-Tweets.txt'), 'r')
Run Code Online (Sandbox Code Playgroud)
和
file = open(os.path.expanduser('~/Desktop/Austin/Tweets/10_7_2012_12//09-Tweets.txt'), 'r')
Run Code Online (Sandbox Code Playgroud)
一切都无济于事.
关于Python如何处理正斜杠的解释将受到欢迎,如果有人关心教一个天真的本科生.
我在Leopard上使用Mac OSX.我正在运行一个与Twitter Streaming API通信的网络爬虫; 名称中的斜杠是使用"/"保存它们以指定日期的结果.
解决方案:您可以在Mac OSX上的文件名中使用正斜杠.从文件系统的角度来看,/实际上是一个冒号,它被转换为/在Finder中.
Kindall的解释如下:碳层是必要的,它使用标准的Mac文件名分隔符,冒号.从历史上看,Mac文件名中的斜线可以追溯到1984年.Mac用户也希望在GUI中看到冒号而不是斜线作为路径名分隔符(或者至少在2001年,当这种行为被建立时).
我目前正在使用HTML5框架Phaser创建一个多人游戏.
这是一个僵尸在地图上产生的游戏,玩家必须射杀它们才能杀死它们.僵尸瞄准最接近他们的玩家.
目前,我的设计策略存在问题.由于运动跟踪,我不确定Phaser是否可以使用这种类型的游戏.
目前,客户端正在处理所有玩家移动,因此每当玩家移动时,它会将其广播到服务器,然后服务器将其发送给所有其他客户端.
但是,我想让僵尸和子弹完全由服务器控制.然后,服务器使用每个僵尸的速度及其当前位置更新每个客户端.我的理由是,任何非玩家输入的内容都应由服务器计算.这将防止诸如两个客户说不同时间僵尸死亡然后试图彼此通信,同时在不同位置有子弹,或者在客户之间的不同时间产生僵尸的问题.
这是一个僵尸类的例子:
function Zombie(game, data){
this.game = game;
this.id = data.id;
Phaser.Sprite.call(this, this.game, data.x, data.y, 'zombie');
this.anchor.setTo(0.5,0.5);
this.animations.add('right', [0,1,2,3], 7, true);
this.animations.add('left', [4,5,6,7], 7, true);
this.game.physics.arcade.enable(this);
this.body.collideWorldBounds = true;
this.health = data.health;
this.maxHealth = data.maxHealth;
this.speed = data.speed;
this.target = this.game.player;
this.waiting = 100;
this.name = "zombie";
this.healthBary = 20;
this.healthBar = this.game.add.sprite(this.x, this.y + this.healthBary, 'player_health');
this.healthBar.anchor.setTo(0.5, 0.5);
CollisionManager.addObjectToGroup(this, 'baddies');
this.game.add.existing(this);
}
Zombie.prototype = Object.create( Phaser.Sprite.prototype );
Zombie.prototype.constructor = Zombie;
Zombie.prototype.update = function(){
this.updateHealthBar();
this.moveTowards(this.target); …
Run Code Online (Sandbox Code Playgroud) 我试图x -> (e^x - 1) / x
在以下七个值处评估函数:
1, .5, .1, .01, .001, .00001, .0000001
Run Code Online (Sandbox Code Playgroud)
我的代码是:
x = [1,.5, .1, .01, .001, .00001, .0000001];
y = (exp(x)-1)/x
Run Code Online (Sandbox Code Playgroud)
问题是,这只会为y产生一个值,即:
y =
1.629441654061645
Run Code Online (Sandbox Code Playgroud)
我注意到,如果我删除/ x,它会给出七个与x的每个值相对应的值(尽管是错误的值).
为什么会这样呢?
我对Haskell很新,我现在正在使用LearnYouAHaskell.我试图将一个由空格分隔的字符串,并将其分解为一个较小的字符串列表.我目前的计划:
main = do
putStrLn "Insert a string to convert: "
-- Input string
line <- getLine
words line;
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,它告诉我我有一个IO错误.根据我的理解,getLine是一个动作,因此,由于这是不纯的,我必须将它绑定到"行".Line是getLine的精确表示,它是一个IO String.
但是,不应该成为一个字符串?当我尝试在线使用单词时,它告诉我"无法匹配预期类型"IO a0"与实际类型[String]
好像line不是字符串.此外,我可以在程序中使用:t行,当我查看它是否是正确类型的实际?
我为新手问题道歉,但我有点卡住了.
编辑:
我在GHCI做了类似的事情,它告诉我我的类型实际上是一个普通的字符串..我不明白.
Prelude> line <- getLine
"Hello fellows"
Prelude> :t line
line :: String
Prelude> words line
["Hello","fellows"]
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?
我目前正在尝试将几个文本文件加载到MongoDB中(它们是JSON格式).
我尝试使用操作系统,但我似乎遇到了麻烦.我目前的方法是:
>>> import pymongo
>>> import os
>>> import json
>>> from pymongo import Connection
>>> connection = Connection()
>>> db = connection.Austin
>>> collection = db.tweets
>>> collection = db.tweet_collection
>>> db.tweet_collection
Collection(Database(Connection('localhost', 27017), u'Austin'), u'tweet_collection')
>>> collection
Collection(Database(Connection('localhost', 27017), u'Austin'), u'tweet_collection')
>>> tweets = db.tweets
>>> tweet = open(os.path.expanduser('~/Tweets/10_7_2012_12:09-Tweets.txt'),'r')
>>> for line in tweet:
... d = json.loads(line)
... tweets.insert(d)
...
Run Code Online (Sandbox Code Playgroud)
用于插入单个推文.我希望能够打开多个文件并运行相同的代码片段,即将JSON转换为python词典的for循环,并自动将其插入到集合中.
有没有人有一个如何做到这一点的完整例子,并附有解释?
当我们讨论这个主题时,我试图使用对数据库理解不够的MongoDB(愚蠢和愚蠢,我知道),但MongoDB可以同时支持多个数据库实例,并存储集合,这些是集合的文件,你可以插入单个文件,对吗?
(另外,请忽略集合推文和tweet_collection之间的不一致..我只是在试验以获得更好的理解)