我正在使用python,我有一个集合列表,构造如下:
list = [set([])]*n
Run Code Online (Sandbox Code Playgroud)
...其中n是列表中我想要的集合数.我想为列表中的特定集添加值.说,第二集.我试过了
list[1].add(value)
Run Code Online (Sandbox Code Playgroud)
但是,这会将值添加到列表中的每个集合.这种行为对我来说非常不直观.通过进一步的测试,我认为我发现了问题:列表显然包含10个相同集合的实例,或者指向同一集合的10个指针,或者其他东西.通过反复调用构造列表
list.append(set([]))
Run Code Online (Sandbox Code Playgroud)
允许我使用上面的语法将元素添加到单个集合.所以我的问题是:我的第一个列表构建技术到底发生了什么?很明显,我不太了解语法.另外,是否有更好的方法来初始化n元素列表?我一直在使用这种语法,这是我的第一个问题.
请参阅以下代码:
def good():
foo[0] = 9 # why this foo isn't local variable who hides the global one
def bad():
foo = [9, 2, 3] # foo is local, who hides the global one
for func in [good, bad]:
foo = [1,2,3]
print('Before "{}": {}'.format(func.__name__, foo))
func()
print('After "{}": {}'.format(func.__name__, foo))
Run Code Online (Sandbox Code Playgroud)
结果如下:
# python3 foo.py
Before "good": [1, 2, 3]
After "good": [9, 2, 3]
Before "bad" : [1, 2, 3]
After "bad" : [1, 2, 3]
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
#This is a game to guess a random number.
import random
guessTaken = 0
print("Hello! What's your name kid")
myName = input()
number = random.randint(1,20)
print("Well, " + myName + ", I'm thinking of a number between 1 and 20.")
while guessTaken < 6:
print("Take a guess.")
guess = input()
guess = int(guess)
guessTaken = guessTaken + 1
if guess < number:
print("You guessed a little bit too low.")
if guess > number:
print("You guessed a little too high.") …Run Code Online (Sandbox Code Playgroud) 我有关于阅读和打印utf8文本文件的python问题.
我有一个没有BOM的utf8编码的test.txt.该文件中包含两个字符:
??
Run Code Online (Sandbox Code Playgroud)
第一个字符"大"是中文,第二个"声"是日文.现在,当我使用Ulipad(一个python编辑器)运行以下代码来读取txt文件,并打印这两个字符.
import codecs
infile = "C:\\test.txt"
f = codecs.open(infile, "r", "utf-8")
s = f.read()
print(s)
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误,
"UnicodeEncodeError: 'cp950' codec can't encode character '\u58f0' in position 1:
illegal multibyte sequence"
Run Code Online (Sandbox Code Playgroud)
我发现它是由第二个字符"声"引起的.
但是当我使用相同的代码在python默认的GUI IDLE中进行测试时,它可以打印两个没有错误的字符.那么,我该如何解决这个问题呢.
我的运行环境是python 3.1,windows xp繁体中文.
j=0
x=[]
for j in range(9):
x=x+ [j]
Run Code Online (Sandbox Code Playgroud)
这将输出
[1,2,3,4,5,6,7,8,9]
Run Code Online (Sandbox Code Playgroud)
我想要它
['1','2','3'...
Run Code Online (Sandbox Code Playgroud)
我怎么才能得到它?
如果有人幻想我真的很快,我会非常感激:
static function make_url_safe($z){
$z = strtolower($z);
$z = preg_replace('/[^a-zA-Z0-9\s] /i', '', $z);
$z = str_ireplace(' ', '-', $z);
return $z;
}
Run Code Online (Sandbox Code Playgroud)
应该将js函数用于将此函数转换为javascript?
我正在尝试设置除了当前请求的操作视图脚本之外还要执行的视图脚本.我希望从控制器操作本身执行此操作,以便从布局$ this-> layout() - > content helper中获得此新视图脚本输出.
我找到了setView()方法,但不知道如何从控制器中使用它.
非常感谢.
有人能解释为什么Python会做以下事情吗?
>>> class Foo(object):
... bar = []
...
>>> a = Foo()
>>> b = Foo()
>>> a.bar.append(1)
>>> b.bar
[1]
>>> a.bar = 1
>>> a.bar
1
>>> b.bar
[1]
>>> a.bar = []
>>> a.bar
[]
>>> b.bar
[1]
>>> del a.bar
>>> a.bar
[1]
Run Code Online (Sandbox Code Playgroud)
这让人很困惑!
我目前遇到了一个问题:我想处理非常有效地将字符串添加到其他字符串,所以我查找了许多方法和技术,并且我想出了"最快"的方法.但我完全不明白它是如何工作的:
def method6():
return ''.join([`num` for num in xrange(loop_count)])
Run Code Online (Sandbox Code Playgroud)
特别让([`num` for num in xrange(loop_count)])我感到困惑.
我想要一串说下面的话:
Guiness Harp "Holy Moses"
Run Code Online (Sandbox Code Playgroud)
这样在C#或VB中得到一个匹配集:
Guiness
Harp
Holy Moses
Run Code Online (Sandbox Code Playgroud)
基本上,除非空格周围有引号,否则它会拆分空格,然后引号之间的单词被视为单个短语.
谢谢,凯文
python ×7
c# ×1
controller ×1
javascript ×1
mutable ×1
php ×1
python-3.x ×1
regex ×1
scope ×1
string ×1
unicode ×1
vb.net ×1
view ×1