我正在尝试使用以下代码选择随机国家/地区的随机城市:
COUNTRIES= ["NL", "NO"]
NL = [
"Assen", "Almere", "Leeuwarden", "Nijmegen", "Groningen",
"Maastricht", "Eindhoven", "Amsterdam", "Enschede", "Utrecht",
"Middelburg", "Rotterdam"
]
NO = [
"Fredrikstad", "Baerum", "Oslo", "Ringsaker", "Gjovik",
"Drammen", "Sandefjord", "Skien", "Arendal", "Kristiansand",
"Stavanger", "Bergen", "Forde", "Alesund", "Trondheim",
"Stjordal", "Bodo", "Tromso", "Alta"
]
CHOSEN_CITY = random.choice(random.choice(COUNTRIES))
print CHOSEN_CITY
Run Code Online (Sandbox Code Playgroud)
但是,这只能给我一个'L','O'或者N.我在这做错了什么?
您将如何在使用所有字符的同时将普通字符串拆分为尽可能多的相同部分。例如
a = "abab"
Run Code Online (Sandbox Code Playgroud)
会返回"ab",而与
b= "ababc"
Run Code Online (Sandbox Code Playgroud)
它会返回"ababc",因为它不能使用所有字母分成相同的部分。
我想将变量中的大写字符串字符更改为小写,并用“_”替换空格。我知道我可以对所有实例使用“if”语句,但这会花费太长时间。它是将用户的输入保存到文件名即
user_selection = 'Barracuda Limited' # what I have
save_name == 'barracuda_limited' # what I want
Run Code Online (Sandbox Code Playgroud)
注意:我已经通读了有关如何发帖的页面,并且正在尽我所能,但我才刚刚开始学习编码,并且在尝试构建我的问题时遇到了麻烦。
在解决了一段时间以来我一直被束缚的编程挑战之后,我总是在想,"它有效,那就足够了".
在我看来,我认为这不是真正正确的思维模式,我认为我应该始终尝试以最佳性能进行编码.
无论如何,有了这个说,我刚试了一个ProjectEuler问题.具体问题#2.
我怎么能改进这个解决方案.我觉得它真的很冗长.就像我在递归中传递前一个数字一样.
<?php
/* Each new term in the Fibonacci sequence is generated by adding the previous two
terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not exceed
four million.
*/
function fibonacci ( $number, $previous = 1 ) {
global $answer;
$fibonacci = $number + $previous; …Run Code Online (Sandbox Code Playgroud) 我刚刚回到Project Euler并且丢失了我的帐户和解决方案,所以我回到了问题7.但是,我的代码不起作用.对我来说这似乎相当原始,有人可以帮助我调试我的(短)脚本吗?
应该找到10001 Prime.
#!/usr/bin/env python
#encoding: utf-8
"""
P7.py
Created by Andrew Levenson on 2010-06-29.
Copyright (c) 2010 __ME__. All rights reserved.
"""
import sys
import os
from math import sqrt
def isPrime(num):
flag = True
for x in range(2,int(sqrt(num))):
if( num % x == 0 ):
flag = False
if flag == True:
return True
else:
return False
def main():
i, n = 1, 3
p = False
end = 6
while end - i >= 0:
p …Run Code Online (Sandbox Code Playgroud) 嘿,我试图从列表中删除一个项目(不使用set):
list1 = []
for i in range(2,101):
for j in range(2,101):
list1.append(i ** j)
list1.sort()
for k in range(1,len(list1) - 1):
if (list1[k] == list1[k - 1]):
list1.remove(list1[k])
print "length = " + str(len(list1))
Run Code Online (Sandbox Code Playgroud)
该set函数工作正常,但我想应用此方法.除了我得到:
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
在声明中:
if (list1[k] == list1[k - 1]):
Run Code Online (Sandbox Code Playgroud)
编辑添加 (感谢Ned Batchelder)工作代码是:
list1 = []
for i in range(2,101):
for j in range(2,101):
list1.append(i ** j)
list1.sort()
k = 0
while k < len(list1) - 1: …Run Code Online (Sandbox Code Playgroud) 我试图解决问题41,但我不知道为什么我的代码停在987654319:
def IsPandigital(No):
a = sorted(str(No))
for i in range(1,10):
if a[i - 1] != str(i):
return False
return True
def IsPrime(No):
i = 2
while i < No:
if No % i == 0:
return False
i += 1
return True
i = 987654321
while i > 0:
print i
raw_input()
if IsPrime(i) == True and IsPandigital(i) == True:
print i
break
i -= 1
print i
print "EOP"
raw_input()
Run Code Online (Sandbox Code Playgroud)
PS:我知道我应该从799999999开始,因为:
GergS:每9位数和8位数的pandigital数可被3整除.
我知道如何将两个矩阵相乘(在一个类下)。下面我展示我的代码。然而我似乎无法弄清楚如何在Python中将矩阵和整数相乘。
更新:矩阵示例。
L=[[1,2],[3,4],[5,6]]
3*L
# [[1,6],[9,12],[15,18]]
def __mul__(self,other):
'''this will multiply two predefined matrices where the number of
columns in the first is equal to the number of rows in the second.'''
L=self.L
L2=other.L
result=[]
if len(L[0])==len(L2):
for i in range(len(L)):
row=[]
for j in range(len(L2[0])):
var=0
for k in range(len(L2)):
var=var+L[i][k]*L2[k][j]
row=row+[var]
result = result+[row]
return matrix(result)
else:
raise ValueError('You may not only multiply m*n * n*q matrices.')
Run Code Online (Sandbox Code Playgroud) Python 2.6的x86 id()函数可以返回的最高数字是多少?
我认为上限必须是任何32位应用程序可以看到的内存量必须是:2 32,即4294967296?
(这样就可以了,因为我必须在C#类型中使用这个值,UInt32否则符合CLS Int64就足够了,这就是我担心的原因,尽管与问题无关.)
但是,如果我在Windows x64上运行超过2GB的内存,说32GB的内存 - id()即使Python解释器本身是x86,Python的功能是否可能返回高于2 32的值?
我猜它归结为Python解释器的机器视图 - 我想WoW64将64位内存地址转换为32位地址 - 但我只是猜测 - 我需要确定!
您好我是Python新手,目前已复制以下脚本:
# filename is printer.py
import sys
for arg in sys.argv:
print arg # the zeroth element is the module name
raw_input("press any key") # a trick to keep the python console open
Run Code Online (Sandbox Code Playgroud)
我试图获取模块名称后跟的参数.但运行此代码会出现以下错误:
U:\printer.py
File "U:\printer.py", line 6
print arg # zeroth element is the module name
^
SyntaxError: Invalid syntax
Run Code Online (Sandbox Code Playgroud)
有谁知道这里有什么不对吗?我使用的是Python 3.2
python ×9
list ×2
python-3.x ×2
arguments ×1
command-line ×1
fibonacci ×1
lowercase ×1
php ×1
primes ×1
python-2.6 ×1
python-2.7 ×1
random ×1
reference ×1
replace ×1
string ×1
text ×1
unique ×1
windows ×1
wow64 ×1