小编Ev.*_*nis的帖子

Python - 如何获取python3中列表中的第一个项目

我在python中有一个列表,如下所示:

edge =  [[1, 2], [1, 3], [1, 4], [3, 4]]
Run Code Online (Sandbox Code Playgroud)

我想打印1113; 又名每个子列表的第一个元素.

我用这个代码:

for subList in edge:
    for firstItem in subList:
        print(firstItem)
Run Code Online (Sandbox Code Playgroud)

但它打印所有元素..

element list python-3.x

2
推荐指数
1
解决办法
2万
查看次数

用Python划分值

我做了一个xlrd到json解析脚本,它不分割变量.它总是返回零...我用来分割变量的代码是:

if not row_values[2]:
   key['nr_one'] = 0
else:
   key['nr_one'] = int(row_values[2])
if not row_values[4]:
   key['nr_two'] = 0
else:     
   key['nr_two'] = int(row_values[4])
try:   
    key['perc'] = float(key['nr_two']/key['nr_one']*100)
except ZeroDivisionError:
    key['perc'] = 0
Run Code Online (Sandbox Code Playgroud)

我在脚本的末尾打印了以下代码:

print('one')
print(key['nr_one'])
print('two')
print(key['nr_two']) 
print('perc')
print(key['perc'])
Run Code Online (Sandbox Code Playgroud)

返回:

one
103386547
two
135680054
perc
0.0
Run Code Online (Sandbox Code Playgroud)

所以.我不明白为什么分裂失败并返回0?请有人帮我格式化计算百分比的好方法

python python-2.x

2
推荐指数
2
解决办法
463
查看次数

如何在python中组合字典

我有2本词典

dict1 = {'color': {'attri': ['Black']}, 'diameter': {'attri': ['(300, 600)']}}

dict2 = {'size': {'op':'in'}, 'diameter': {'op':'range'}, 'color': {'op':'in'}}
Run Code Online (Sandbox Code Playgroud)

我想结合这两个词典

dict3 = {'color': {'op': 'in', 'attri': ['Black']}, 'diameter': {'op': 'range', 'attri': ['(300,600)']}}
Run Code Online (Sandbox Code Playgroud)

python django

2
推荐指数
2
解决办法
209
查看次数

Python:比较和替换list [i]和方括号

给定两个列表,我想比较list1和list2,并在list1中替换它,并添加方括号。

str1 = "red man juice"
str2 = "the red man drank the juice"

one_lst = ['red','man','juice']
lst1 = ['the','red','man','drank','the','juice']
Run Code Online (Sandbox Code Playgroud)

预期输出:

lst1 = ['the','[red]','[man]','drank','the','[juice]']
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的是:

lst1 = list(str1.split())
for i in range(0,len(lst1)):
    for j in range(0,len(one_lst)):
        if one_lst[j] == lst1[i]:
            str1 = str1.replace(lst1[i],'{}').format(*one_lst)
lst1 = list(str1.split())
print(lst1)
Run Code Online (Sandbox Code Playgroud)

我可以更换它,但是没有括号。

谢谢您的帮助!

python string replace list

2
推荐指数
1
解决办法
50
查看次数

奇怪的字典构造 - 重复键

我需要编写一个函数,将'map'-1映射到' - ',1映射到''(是的,没有),其他所有值都写入自身的字符串表示.而不是使用if语句我虽然我会用这样的字典这样做:

def adj(my_value):
    return {-1: '-', 1: '', my_value: str(my_value)}[my_value]
Run Code Online (Sandbox Code Playgroud)

然后它打了我,这个字典允许重复键,行为如下:

for x in [-1, 1, 4]:
    print(adj(x))
# -> -1
# -> 1
# -> 4
Run Code Online (Sandbox Code Playgroud)

我确实运行了多次,以确保print()不是"随机",因为dicts没有排序,但我一直得到这个输出.


现在这绝对不是我想要的行为,并且绝不可以信任这样的构造但是有人知道为什么甚至允许这样的事情,因为重复的密钥不是吗?如何进行计算?

python dictionary python-3.x

1
推荐指数
1
解决办法
51
查看次数

为什么打印-0.0?

任何人都可以解释为什么会发生以下情况?

print(-1 * (605 % 11))        #-> 0
print(-1 * (0.5*1210 % 11))   #-> -0.0
print(-1 * (0.5*1210) % 11)   #-> 0.0
Run Code Online (Sandbox Code Playgroud)

特别-0.0是莫名其妙..

python floating-point

1
推荐指数
1
解决办法
89
查看次数

如何在列表中的最后一个单词之前添加单词?

您好我是这种编程语言的新手

我想'and'在列表中的最后一项之前添加单词.

例如:

myList = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)

如果我打印它输出必须像:

1,2,3 and 4
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
157
查看次数

更新Python字典中的多个值

你好,我是 python 新手,我正在观看很多 youtube 视频来学习。我知道如何单独更新字典值。我想一次性更新它们。

例子:

students = {'Eric': 15, 'Bob': 13, 'Chris': 16, 'Min': 25} 
Run Code Online (Sandbox Code Playgroud)

我想将其更新为

{'Eric': 16, 'Bob': 14, 'Chris': 17, 'Min': 26}.
Run Code Online (Sandbox Code Playgroud)

每个人的年龄增加1。

python dictionary

1
推荐指数
1
解决办法
1万
查看次数

使用默认后备选项筛选列表值

给出2个数字列表:

real_solutions_sols1 =  [-53.2909210236, -8.31658000998, 1.87689837129, 1.4]
real_solutions_sols2 =  [-21.1439685227, -19.2]
Run Code Online (Sandbox Code Playgroud)

我想相同的代码重写名单,使它们包含之间数字0.14.0:

real_solutions_sols1 =  [ 1.87689837129, 1.4]
Run Code Online (Sandbox Code Playgroud)

对于第二个列表,这是不可能的,所以我希望代码返回完整列表:

real_solutions_sols2 =  [-21.1439685227, -19.2]
Run Code Online (Sandbox Code Playgroud)

以下代码适用于real_solutions_sols2:

real_roots_zero_to_four = []
for i in real_solutions_sols2:
   if (i >= 0.1) and (i <= 4.0):
      real_roots_zero_to_four.append(i)
   else:
     real_roots_zero_to_four = real_solutions_sols2

print 'real_roots_zero_to_four = ', real_roots_zero_to_four
Run Code Online (Sandbox Code Playgroud)

if条件未得到满足,所以我们跳转到else的语句.

real_roots_zero_to_four = [-21.14396852,-19.2]

但是,对于第一个列表,它无限循环:

real_roots_zero_to_four = []
for i in real_solutions_sols1:
   if (i >= 0.1) and (i <= 4.0):
      real_roots_zero_to_four.append(i)
   else: …
Run Code Online (Sandbox Code Playgroud)

python for-loop if-statement list append

1
推荐指数
1
解决办法
132
查看次数

从列表中找到两个加起来为特定数字的数字

这是超级糟糕和凌乱,我是新来的,请帮助我.

基本上,我试图从列表中找到两个加起来为目标数字的数字.

我已经设置了一个示例,lst = [2, 4, 6, 10]目标值为target = 8.这个例子中的答案是(2, 6)(6, 2).

下面是我的代码,但它很长很丑,我相信有更好的方法.您能否从我的代码中了解如何改进?

from itertools import product, permutations

numbers = [2, 4, 6, 10]
target_number = 8

two_nums = (list(permutations(numbers, 2)))
print(two_nums)

result1 = (two_nums[0][0] + two_nums[0][1])
result2 = (two_nums[1][0] + two_nums[1][1])
result3 = (two_nums[2][0] + two_nums[2][1])
result4 = (two_nums[3][0] + two_nums[3][1])
result5 = (two_nums[4][0] + two_nums[4][1])
result6 = (two_nums[5][0] + two_nums[5][1])
result7 = (two_nums[6][0] + two_nums[6][1])
result8 = (two_nums[7][0] + two_nums[7][1]) …
Run Code Online (Sandbox Code Playgroud)

python for-loop permutation

1
推荐指数
1
解决办法
2257
查看次数