我有这个字符串列表和一些前缀.我想删除列表中以任何这些前缀开头的所有字符串.我试过了:
prefixes = ('hello', 'bye')
list = ['hi', 'helloyou', 'holla', 'byeyou', 'hellooooo']
for word in list:
list.remove(word.startswith(prexixes)
Run Code Online (Sandbox Code Playgroud)
所以我希望我的新列表是:
list = ['hi', 'holla']
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
ValueError: list.remove(x): x not in list
Run Code Online (Sandbox Code Playgroud)
出了什么问题?
我试图通过使用带有 SELECT MAX(id) 的查询来查找 MySQL 数据库中的最新条目。我已经获得了最新的 id,所以我知道查询有效,但现在我想在 while 循环中使用它,所以我每次迭代都会获得最新的条目。
这是我到目前为止:
import pymysql
con = pymysql.connect(host='.....', user='.....',
password='.....', database='.....')
cur = con.cursor()
while True:
query = "SELECT MAX(id) FROM reports"
cur.execute(query)
data = cur.fetchall()
last = (data[0][0])
print(last)
Run Code Online (Sandbox Code Playgroud)
问题是更新数据库后我一直得到相同的结果。例如,现在我有 45 个条目,所以我的脚本在 while 循环中打印 '45'。但是在我向表中添加另一行后,它会继续打印“45”而不是我期望的“46”。当我停止脚本并再次运行它时,它会打印“46”并在我添加另一行后继续打印。
我大约两周前才开始使用 MySQL,所以我对它没有那么多了解。我觉得我在这里错过了一些非常小的东西。我该怎么办?任何帮助将不胜感激。
我正在尝试向我的数据库添加一些数据,但是我收到错误Catchable致命错误:类PDOStatement的对象无法在第114行的/var/www/mandje.php中转换为字符串.这是代码我正在使用:
foreach($_SESSION["cart"] as $id => $value){
$query = $db->query('SELECT * FROM Producten WHERE ProductID ="'.$id.'" ');
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$price = $row['Prijs'];
$ProductID = $row['ProductID'];
}
$sql="INSERT INTO Bestellingsdetail( Bestelnummer, ProductID, Aantal, Prijs)
VALUES ($max,$ProductID,$value,$price)"; //<---- line 114
$count = $db->execute($sql);
Run Code Online (Sandbox Code Playgroud)
我真的不知道这里出了什么问题.任何帮助将非常感激 :)
我正在尝试编写一个函数来检查字符串中的字符是否使用递归进行排序.这就是我想出的:
def is_sorted(x,i):
if i >= len(x):
return True
elif x[i] <= x[i-1]:
return False
else:
is_sorted(x,i+1)
Run Code Online (Sandbox Code Playgroud)
我用这些来测试我的功能:
x = "abcadef"
y = "aabcdef"
z = "abcdef"
print is_sorted(x, 1)
print is_sorted(y, 1)
print is_sorted(z, 1)
Run Code Online (Sandbox Code Playgroud)
我希望得到False,False,True,但我得到None,False,None.为什么?:(
我想知道Twitter中的推文中出现一个单词的频率.我使用Twitter API从twitter下载了500条推文,并制作了一个字典,其中包含单词频率作为键,以及与该频率对应的所有单词列表作为值.
我一直以为字典总是无序的,所以我想以某种方式订购我的字典.但是当我看着它时,它已经被键从低到高排序.这怎么可能?
这是我使用的代码:
def countWords(cleanDict):
reverseDict = {}
FreqDict = {}
count = 1
for tweet_id in cleanDict:
tweet = cleanDict[tweet_id]
wordList = tweet.split()
for word in wordList: # Creates a dictionary with words as keys and
# frequencies as values
if word in reverseDict:
reverseDict[word] += 1
else:
reverseDict[word] = 1
for word in reverseDict: # Creates a dictionary with frequencies as keys and
# lists of words as values
if reverseDict[word] in FreqDict:
temp = FreqDict[freqDict[word]]
temp.append(word) …Run Code Online (Sandbox Code Playgroud) 我正在尝试从词典列表中获取一些数据。数据来自csv文件,因此都是字符串。文件中的键都有双qoutes,但是由于它们都是字符串,因此我想删除它们,以便它们在字典中看起来像这样:
{'key':value}
Run Code Online (Sandbox Code Playgroud)
代替这个
{'"key"':value}
Run Code Online (Sandbox Code Playgroud)
我尝试只使用string = string [1:-1],但这是行不通的...
这是我的代码:
csvDelimiter = ","
tsvDelimiter = "\t"
dataOutput = []
dataFile = open("browser-ww-monthly-201305-201405.csv","r")
for line in dataFile:
line = line[:-1] # Removes \n after every line
data = line.split(csvDelimiter)
for i in data:
if type(i) == str: # Doesn't work, I also tried if isinstance(i, str)
# but that didn't work either.
print i
i = i[1:-1]
print i
dataOutput.append({data[0] : data[1]})
dataFile.close()
print "Data output:\n"
print dataOutput
Run Code Online (Sandbox Code Playgroud)
我从印刷品获得的所有印刷品都不错,没有双引号,但是当我将数据附加到dataOutput时,引号又回来了!
知道如何使它们永远消失吗?
这是我的代码:
for (var i=1;1<=10;i++) {
console.log(i);
}
var count = 10;
while (count>0) {
console.log(count);
count--;
}
do {
console.log("1 loop");
} while (count === 1000);
Run Code Online (Sandbox Code Playgroud)
我唯一能想到的是一个无限循环,但对我来说似乎没有.
python ×5
dictionary ×2
list ×2
string ×2
crash ×1
cursor ×1
javascript ×1
loops ×1
mysql ×1
pdo ×1
php ×1
select ×1
startswith ×1
while-loop ×1