在相当长的一段时间里,我一直试图想出一种方法来遍历列表并删除我当前的项目.我似乎无法像我希望的那样工作.它只循环一次,但我希望它循环2次.当我删除删除行 - 它循环2次.
a = [0, 1]
for i in a:
z = a
print z.remove(i)
Run Code Online (Sandbox Code Playgroud)
输出:
[1]
Run Code Online (Sandbox Code Playgroud)
我期待的输出:
[1]
[0]
Run Code Online (Sandbox Code Playgroud) 我正在寻找重构下面的Python代码的最佳方法.我认为在2或3行代码中有一种Pythonic方法可以做到这一点,但无法弄明白.我搜索过Stackoverflow但找不到类似的问题和解决方案.非常感谢!
list1 = [(Python, 5), (Ruby, 10), (Java, 15), (C++, 20)]
list2 = [(Python, 1), (Ruby, 2), (Java, 3), (PHP, 4), (Javascript, 5)]
# I want to make an unsorted list3 like this
# list3 = [(Python, 6), (Ruby, 12), (Java, 18), (PHP, 4), (Javasript, 5), (C++, 20)]
common_keys = list(set(dict(list1).keys()) & set(dict(list2).keys()))
if common_keys:
common_lst = [(x, (dict(list1)[x] + dict(list2)[x])) for x in common_keys]
rest_list1 = [(x, dict(list1)[x]) for x in dict(list1).keys() if x not in common_keys]
rest_list2 = …Run Code Online (Sandbox Code Playgroud) 尝试在文本文件的内容上使用通配符在python中进行搜索/替换:
如果文本文件的内容如下所示:
“ all_bcar_v0038.ma”; “ all_bcar_v0002.ma”; “ all_bcar_v0011.ma”; “ all_bcar_v0011.ma”;
希望将所有版本号替换为v1000以获得此信息:
“ all_bcar_v1000.ma”; “ all_bcar_v1000.ma”; “ all_bcar_v1000.ma”; “ all_bcar_v1000.ma”;
并把文件写出来。
我在下面尝试过,但是发生的是该脚本仅捕获第一个版本号,而其他版本未受影响:
def replaceAll(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
rigs = ['all_bcar']
rigs_latest = ['all_bcar_v1000']
old_pattern = []
old_compiled = []
old = []
old_version = []
for rig in range(len(rigs)):
old_pattern.append("/" + rigs[rig] + "_(.*).ma")
fin = open(txt_file, "r")
old_compiled.append(re.compile(old_pattern[rig]))
old.append(old_compiled[rig].search(fin.read()))
old_version.append(old[rig].group(1).strip())
old_rig = (rigs[rig] + "_" + old_version[rig])
replaceAll(txt_file,old_rig,rigs_latest[rig])
fin.close()
Run Code Online (Sandbox Code Playgroud)
不确定如何保持搜索循环以查找其他版本并避免已被替换的版本,以跳过任何等于“ …
我不知道如何做到这一点:我有一个list的list这样定义S:
list=[[day,type,expense],[...]];
Run Code Online (Sandbox Code Playgroud)
日和费用是int,类型是string
我需要在白天找到最大费用.一个例子:
list=[[1,'food',15],[4,'rent', 50],[1,'other',60],[8,'bills',40]]
Run Code Online (Sandbox Code Playgroud)
我需要总结当天的元素并找到费用最高的那一天.
结果应该是:
day:1, total expenses:75
我有一个Django应用程序,允许用户创建变量并命名它们
class Product(models.Model):
name = models.CharField(max_length=40, unique=True)
int1_name = models.CharField(max_length=60, blank=True, null=True)
int1_default = models.IntegerField(blank=True, null=True)
int2_name = models.CharField(max_length=60, blank=True, null=True)
int2_default = models.IntegerField(blank=True, null=True)
float1_name = models.CharField(max_length=60, blank=True, null=True)
float1_default = models.FloatField(blank=True, null=True)
float2_name = models.CharField(max_length=60, blank=True, null=True)
float2_default = models.FloatField(blank=True, null=True)
string1_name = models.CharField(max_length=60, blank=True, null=True)
string1_default = models.CharField(max_length=60, blank=True, null=True)
string2_name = models.CharField(max_length=60, blank=True, null=True)
string2_default = models.CharField(max_length=60, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
然后存储它们
class ItemData(models.Model):
created = models.DateTimeField(default=datetime.now)
item = models.ForeignKey(Item, editable=False)
int1_val = models.IntegerField(blank=True, null=True)
int2_val = models.IntegerField(blank=True, …Run Code Online (Sandbox Code Playgroud) 我有一个很大的清单
[[1,.., ..],[2,...,...],[5,...,...],[1,...,...]]
Run Code Online (Sandbox Code Playgroud)
我需要删除所有具有相同第一个值的元素。(只保留一次)
怎么做最有效率?
我正在尝试使用iexact我的Django应用程序.
我在我的数据库类的物品test,TEST,tEsT,和TesT.
我试图找出test我的数据库是否有任何形式.好像我需要使用iexact但是我尝试使用它我收到了一个错误.
这是我的代码片段.
def item_search(x):
item = x.column_in_database
if test__iexact = 'test' in item:
return 1; #this is just pseduocode for stackoverflow
elif
return 0; #this is just pseduocode for stackoverflow
Run Code Online (Sandbox Code Playgroud)
我尝试过各种各样的方法,但我仍然无法让它发挥作用.
我正在尝试分析一系列密码的频率。我的脚本正在使用其他输入媒体,但是我当前的数据集中似乎有一些错误的字符。如何绕过“坏”数据?
import re
import collections
words = re.findall('\w+', open('rockyou.txt').read().lower())
a=collections.Counter(words).most_common(50)
for word in a:
print(word)
Run Code Online (Sandbox Code Playgroud)
然后我得到错误:
Traceback (most recent call last):
File "shakecount.py", line 3, in <module>
words = re.findall('\w+', open('rockyou.txt').read().lower().ASCII)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf1 in position 5079963: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
例如D,如果sum_p不为零.
D = 1/sum_p if sum_p \
else 0
Run Code Online (Sandbox Code Playgroud)
如果我想返回值怎么办?我应该这样做:
return 1/sum_p if sum_p \
else 0
Run Code Online (Sandbox Code Playgroud)
或者只是写
if...
else: return 0
Run Code Online (Sandbox Code Playgroud) 我用python 2.7.2运行Ubuntu.
脚本
python \
/home/blainer/Desktop/convert/converter.py \
/home/blainer/Desktop/convert/urban.shp \
/home/blainer/Desktop/convert/result.js \
--width 900 \
--country_name_index 4 \
--where "ISO = 'USA'" \
--codes_file /home/blainer/Desktop/convert/codes-en.tsv \
--insets '[{"codes": ["US-AK"], "width": 200, "left": 10, "top": 370}, {"codes": ["US-HI"], "width": 100, "left": 220, "top": 400}]' \
--minimal_area 4000000 \
--buffer_distance -3000 \
--simplify_tolerance 1000 \
--longtitude0 10w \
--name us
Run Code Online (Sandbox Code Playgroud)
错误
blainer@ubuntu:~/Desktop/convert$ python script.py
File "script.py", line 5
--width 900 \
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)