获取python中每个2个字母单词的第一个字母

时间:2023-02-14 18:08:22

I have this code:

我有这个代码:

hand=["TS","AD"]
test=['--23456789TJQKA'.index(a) for a, b in hand]
print (test)

result is :

结果是:

[10, 14]

How does this snippet work? Is it a built-in function for [a for a, b in list] to get the first letter of each 2-letter word in list in python?

这个代码段如何工作?它是[a for a,b in list]的内置函数来获取python中列表中每个2个字母单词的第一个字母吗?

4 个解决方案

#1


First, let's turn the code into a for loop:

首先,让我们将代码转换为for循环:

hand = ["TS","AD"]
test = []
for a,b in hand:
    test.append('--23456789TJQKA'.index(a))
# note that we didn't use b

print(test)

So what's happening here?
Well, each element in hand is an iterable with two elements. This means that for a,b in hand iterates through each string in hand, assigning the first character to a and the second to b. This is effectively the same as:

那么这里发生了什么?好吧,手中的每个元素都是具有两个元素的可迭代元素。这意味着对于a,b手持迭代手中的每个字符串,将第一个字符分配给a,将第二个字符分配给b。这实际上与:

for mystr in hand:
    a = mystr[0]
    b = mystr[1]
    # or equivalently, a,b = mystr

The next piece is '--23456789TJQKA'.index(a), which simply returns the index of the first occurrence of a in string '--23456789TJQKA'.

下一篇是'--23456789TJQKA'.index(a),它只返回字符串'--23456789TJQKA'中第一次出现的索引。

So the output ends up being a list of two numbers - the indices of the first character of each string in hand, namely, 'T' and 'A'

所以输出最终是两个数字的列表 - 手中每个字符串的第一个字符的索引,即'T'和'A'

#2


This is a normal list comprehension that is splitting the two letter strings in hand into tuple of letters

这是一个正常的列表理解,它将手中的两个字母串分成字母元组

for the first element of hand:

对于手的第一个元素:

a, b in 'TS'
# a == 'T'
# b == 'S'

'--23456789TJQKA'.index('T')
# 10

for the second element of hand:

对于手的第二个元素:

a, b in 'AD'
# a == 'A'
# b == 'D'

'--23456789TJQKA'.index('A')
# 14

#3


Here for a, b in hand part actually unpacks the strings TS and AD. loop variable a and b is assigned to a hand element. Which is like

这里a,b in hand部分实际上解包了字符串TS和AD。循环变量a和b分配给手元素。这是什么样的

(a, b) = 'TS'

here a is assigned to T and b is set to S.

这里a分配给T,b设置为S.

After that .index method just looks for a and returns the index of it.

之后.index方法只查找a并返回它的索引。

Note: This will not work if hand contains a non-two letter word.

注意:如果hand包含非双字母单词,则无效。

#4


The code has something to do with poker or some other card game using a standard 52 card deck. test will be a list of the ordinal "ranks" of a player's hand (the "suit" is not saved). It uses tuple unpacking to put the "rank" of the hand into a and the "suit" into b within the list comprehension.

该代码与扑克或其他使用标准52卡牌组的纸牌游戏有关。 test将是玩家手牌的序数“等级”列表(“套装”未保存)。它使用元组解包将手的“等级”放入a中,并将“套装”放入列表理解中的b中。

#1


First, let's turn the code into a for loop:

首先,让我们将代码转换为for循环:

hand = ["TS","AD"]
test = []
for a,b in hand:
    test.append('--23456789TJQKA'.index(a))
# note that we didn't use b

print(test)

So what's happening here?
Well, each element in hand is an iterable with two elements. This means that for a,b in hand iterates through each string in hand, assigning the first character to a and the second to b. This is effectively the same as:

那么这里发生了什么?好吧,手中的每个元素都是具有两个元素的可迭代元素。这意味着对于a,b手持迭代手中的每个字符串,将第一个字符分配给a,将第二个字符分配给b。这实际上与:

for mystr in hand:
    a = mystr[0]
    b = mystr[1]
    # or equivalently, a,b = mystr

The next piece is '--23456789TJQKA'.index(a), which simply returns the index of the first occurrence of a in string '--23456789TJQKA'.

下一篇是'--23456789TJQKA'.index(a),它只返回字符串'--23456789TJQKA'中第一次出现的索引。

So the output ends up being a list of two numbers - the indices of the first character of each string in hand, namely, 'T' and 'A'

所以输出最终是两个数字的列表 - 手中每个字符串的第一个字符的索引,即'T'和'A'

#2


This is a normal list comprehension that is splitting the two letter strings in hand into tuple of letters

这是一个正常的列表理解,它将手中的两个字母串分成字母元组

for the first element of hand:

对于手的第一个元素:

a, b in 'TS'
# a == 'T'
# b == 'S'

'--23456789TJQKA'.index('T')
# 10

for the second element of hand:

对于手的第二个元素:

a, b in 'AD'
# a == 'A'
# b == 'D'

'--23456789TJQKA'.index('A')
# 14

#3


Here for a, b in hand part actually unpacks the strings TS and AD. loop variable a and b is assigned to a hand element. Which is like

这里a,b in hand部分实际上解包了字符串TS和AD。循环变量a和b分配给手元素。这是什么样的

(a, b) = 'TS'

here a is assigned to T and b is set to S.

这里a分配给T,b设置为S.

After that .index method just looks for a and returns the index of it.

之后.index方法只查找a并返回它的索引。

Note: This will not work if hand contains a non-two letter word.

注意:如果hand包含非双字母单词,则无效。

#4


The code has something to do with poker or some other card game using a standard 52 card deck. test will be a list of the ordinal "ranks" of a player's hand (the "suit" is not saved). It uses tuple unpacking to put the "rank" of the hand into a and the "suit" into b within the list comprehension.

该代码与扑克或其他使用标准52卡牌组的纸牌游戏有关。 test将是玩家手牌的序数“等级”列表(“套装”未保存)。它使用元组解包将手的“等级”放入a中,并将“套装”放入列表理解中的b中。