我在OSX 10.9上使用python 2.7,并希望05. ?????.mp3用12个符号剪切unicode string(),所以我用mp3file[:12]它来剪切12个符号.但结果我得到了字符串05. ?????.m,只有11个符号.但len(mp3file[:12])返回12.看起来问题是俄罗斯符号?.
这可能有什么问题?
这个的主要问题 - 我通常无法显示字符串{:<12}'.format(mp3file[:12]).
我试图通过一些已经存在的关于我的错误的问题,但他们都没有做到这一点.这是我尝试运行的代码:
from random import *
location1 = randint(0,7)
location2 = location1 + 1
location3 = location2 + 1
guess = None
hits = 0
guesses = 0
isSunk = False
while (isSunk == False):
guess = raw_input("Ready, aim, fire! (enter a number from 0-6): ")
if (guess < 0 | guess > 6):
print "Please enter a valid cell number!"
else:
guesses = guesses + 1;
if (guess == location1 | guess == location2 | guess == location3):
print "HIT!" …Run Code Online (Sandbox Code Playgroud) 我正在研究一些问题并遇到了这个问题.
Python代码
row=[]
col=[]
init=[-1,-1]
Run Code Online (Sandbox Code Playgroud)
现在我把它附加init到row和col.
row.append(init)
row.append(init)
col.append(init)
col.append(init)
Run Code Online (Sandbox Code Playgroud)
因此row = [[-1,-1],[-1,-1]]和col = [[-1,-1],[-1,-1]]
现在,当我改变init[0] = 9我的row并col成为
row = [[9,-1],[9,-1]]和col = [[9,-1],[9,-1]]
作为我项目的一部分,我需要将字符组成对(唯一).我在列表中有超过1000个这样的字符.从这些字符列表中创建唯一对的最快和优化方法是什么.我目前正在使用itertools,我的代码似乎执行得非常糟糕.
我的代码使用itertools:
import itertools
characters = ['A', 'B', 'C', 'D', 'E']
relations = []
for character in range(len(characters) + 1):
for combination in itertools.combinations(characters, character):
if len(combination) == 2:
relations.append(combination)
print relations
Run Code Online (Sandbox Code Playgroud)
预期产出:
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'C'),
('B', 'D'), ('B', 'E'), ('C', 'D'), ('C', 'E'), ('D', 'E')]
Run Code Online (Sandbox Code Playgroud) 我有一个位置名称,然后是每个位置的纬度和经度.我正在尝试确定在Python中用两个值表达单个键的最佳方法.我在想这个:
dict = {
'California': ('36.46715833333333', '-117.85891388888888'),
'California2': ('36.46715833333333', '-117.85891388888888'),
}
Run Code Online (Sandbox Code Playgroud)
然后添加更多:
dict['California'] = ('36.46715833333333', '-117.85891388888888')
Run Code Online (Sandbox Code Playgroud)
但是,我将如何轻松地遍历每个并提取它们?
for location in dict:
for lat, lon in location:
print lat, lon
Run Code Online (Sandbox Code Playgroud)
这给出了一个ValueError,声明它没有多个值可以解压缩.我如何迭代这个并得到每个lat和long?
我想知道哪种是从相同类型的对象列表中获取属性的最佳方法.有没有比for循环更有效的解决方案?我尝试使用getattr,但没有使用列表.
只是为了说清楚.假设我已经定义了一个带有属性的类FooBar
class Foo():
def __init__(self, Bar):
self.bar = Bar
A = Foo(5)
B = Foo(6)
C = [A,B]
Run Code Online (Sandbox Code Playgroud)
我是否可以Bar为整个列表同时获取属性以使最终结果为值为[5,6]的列表?谢谢你的帮助
我有以下等式:
result=[(i,j,k) for i in S for j in S for k in S if sum([i,j,k])==0]
Run Code Online (Sandbox Code Playgroud)
我想在if语句中添加另一个条件,使得我的结果集不包含(0,0,0).我尝试执行以下操作:
result=[(i,j,k) for i in S for j in S for k in S if sum([i,j,k])==0 && (i,j,k)!=(0,0,0)]但是我收到指向的语法错误&&.我测试了我的表达式第一个条件并且它是正确的.
我试图从这个维基百科页面刮掉有生日的人
这是现有的代码:
hdr = {'User-Agent': 'Mozilla/5.0'}
site = "http://en.wikipedia.org/wiki/"+"january"+"_"+"1"
req = urllib2.Request(site,headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
print soup
Run Code Online (Sandbox Code Playgroud)
这一切都很好,我得到整个HTML页面,但我想要特定的数据,我不知道如何使用没有id使用的Beautiful Soup访问它.该<ul>标签没有一个id也不做<li>标记.另外,我不能只询问每个<li>标签,因为页面上还有其他列表.是否有特定方式来调用给定列表?(我不能只为这一页使用修复程序,因为我计划迭代所有日期并让每个页面生日,我不能保证每个页面都与此页面完全相同).
我正在学习python文件处理.我尝试使用此代码一次读取一个字符
f = open('test.dat', 'r')
while (ch=f.read(1)):
print ch
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用
这是错误消息
C:\Python27\python.exe "C:/Users/X/PycharmProjects/Learning Python/01.py"
File "C:/Users/X/PycharmProjects/Learning Python/01.py", line 4
while (ch=f.read(1)):
^
SyntaxError: invalid syntax
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud) 我正在尝试在IDLE中打开一个txt文件,但它给了我一个错误.我无法弄清楚我的文件名中的f会发生什么,或者为什么单个'\'在错误消息中变为double.
>>>f=open('D:\programs\python 2.7.10\programs\foo.txt','r')
Traceback (most recent call last):
File "<pyshell#94>", line 1, in <module>
f=open('D:\programs\python 2.7.10\programs\foo.txt','r')
IOError: [Errno 22] invalid mode ('r') or filename: 'D:\\programs\\python 2.7.10\\programs\x0coo.txt'
Run Code Online (Sandbox Code Playgroud) python ×10
python-2.7 ×6
class ×1
combinations ×1
html-parsing ×1
if-statement ×1
list ×1
string ×1
typeerror ×1
unicode ×1
web-scraping ×1