Python中=号左边可以使用变量吗?
In Python, can variables be used on the left side of the = sign?
我使用 Python 3.6.3 和 visual studio 代码
x=[],y=[]'a=[],d+[],i=0
a = ['Al', 'red', '1', '1', 'blue', 'green','', '65', \
'Bill', 'yellow', '1', '2', 'blue', 'red','', '55', \
'Alice', 'pink', '1', '3', 'blue', 'green','', '66', \
'Fred', 'pink', '1', '4', 'orange', 'puce','', '65]
for p in range(1,5):
x=[a[2+i]]
y=[a[3+i]]
d(x,y)=a[0:8] # variables x and y on left of = sign.
i=i+8
print(d('1','2'))
print(d('1','3')[0])
# want to get the following
'Bill', 'yellow', '1', '2', 'blue', 'red','', '55'
'Alice'
我的问题是:在Python中,变量可以用在
=
标志的左侧?什么数据结构或其他什么
会做吗?它在 BASIC 中有效。
我为什么要这样做?假设我有 1000 个组
而不仅仅是这 4.
当我得到答案时,我如何确认和批准或
无论我需要做什么。我还没有找到指南。
您似乎想使用字典:
a = ['Al', 'red', '1', '1', 'blue', 'green','', '65',
'Bill', 'yellow', '1', '2', 'blue', 'red','', '55',
'Alice', 'pink', '1', '3', 'blue', 'green','', '66',
'Fred', 'pink', '1', '4', 'orange', 'puce','', '65']
step = 8
d = {}
for start in range(0, len(a), step):
end = start + step
part = a[start:end]
d[tuple(part[2:4])] = part
print(d['1','2'])
print(d['1','3'][0])
输出:
['Bill', 'yellow', '1', '2', 'blue', 'red', '', '55']
Alice
这适用于任意数量的组。
Mike Muller 的回答非常有效,即使我搞砸了并且忘记增加保持在 1 的变量。它适用于我的 110 组大列表,步长值为 1435。我无法从一个移动组到下一个。
我将通过 Mike Muller 给我的代码和他对字典的评论让我了解如何在 = 符号的左侧使用变量来结束这个问题。
所以我的问题的答案是肯定的。这是我的代码和结果。
step = 1435 # Mike Muller's
d = {}
for start in range(0, len(l), step):
end = start + step
part = l[start:end]
d[tuple(part[2:4])] = part
uu=1
vari = {}
for x in range(1,12):
for y in range(1,15):
if (str(x),str(y)) in d:
vari[uu] = d[str(x),str(y)][50]
print('key ',x,y) # check results
print ('value ',vari[uu]) # check results
uu=uu+1
print('counter ',uu) # check results
print('out of loop check ', vari[87]) # check results
key 1 1
value 121
counter 2
key 1 2
value 124
and so on to the end
counter 87
key 11 6
value 121
counter 88
key 11 7
value 124
counter 89
key 11 8
value 121
counter 90
out of loop check 121
我使用 Python 3.6.3 和 visual studio 代码
x=[],y=[]'a=[],d+[],i=0
a = ['Al', 'red', '1', '1', 'blue', 'green','', '65', \
'Bill', 'yellow', '1', '2', 'blue', 'red','', '55', \
'Alice', 'pink', '1', '3', 'blue', 'green','', '66', \
'Fred', 'pink', '1', '4', 'orange', 'puce','', '65]
for p in range(1,5):
x=[a[2+i]]
y=[a[3+i]]
d(x,y)=a[0:8] # variables x and y on left of = sign.
i=i+8
print(d('1','2'))
print(d('1','3')[0])
# want to get the following
'Bill', 'yellow', '1', '2', 'blue', 'red','', '55'
'Alice'
我的问题是:在Python中,变量可以用在
=
标志的左侧?什么数据结构或其他什么
会做吗?它在 BASIC 中有效。
我为什么要这样做?假设我有 1000 个组 而不仅仅是这 4.
当我得到答案时,我如何确认和批准或 无论我需要做什么。我还没有找到指南。
您似乎想使用字典:
a = ['Al', 'red', '1', '1', 'blue', 'green','', '65',
'Bill', 'yellow', '1', '2', 'blue', 'red','', '55',
'Alice', 'pink', '1', '3', 'blue', 'green','', '66',
'Fred', 'pink', '1', '4', 'orange', 'puce','', '65']
step = 8
d = {}
for start in range(0, len(a), step):
end = start + step
part = a[start:end]
d[tuple(part[2:4])] = part
print(d['1','2'])
print(d['1','3'][0])
输出:
['Bill', 'yellow', '1', '2', 'blue', 'red', '', '55']
Alice
这适用于任意数量的组。
Mike Muller 的回答非常有效,即使我搞砸了并且忘记增加保持在 1 的变量。它适用于我的 110 组大列表,步长值为 1435。我无法从一个移动组到下一个。
我将通过 Mike Muller 给我的代码和他对字典的评论让我了解如何在 = 符号的左侧使用变量来结束这个问题。 所以我的问题的答案是肯定的。这是我的代码和结果。
step = 1435 # Mike Muller's
d = {}
for start in range(0, len(l), step):
end = start + step
part = l[start:end]
d[tuple(part[2:4])] = part
uu=1
vari = {}
for x in range(1,12):
for y in range(1,15):
if (str(x),str(y)) in d:
vari[uu] = d[str(x),str(y)][50]
print('key ',x,y) # check results
print ('value ',vari[uu]) # check results
uu=uu+1
print('counter ',uu) # check results
print('out of loop check ', vari[87]) # check results
key 1 1
value 121
counter 2
key 1 2
value 124
and so on to the end
counter 87
key 11 6
value 121
counter 88
key 11 7
value 124
counter 89
key 11 8
value 121
counter 90
out of loop check 121