列表操作与索引

huangapple go评论56阅读模式
英文:

Lists manipulation with indexs

问题

我有两个列表。

列表 A:

A = ["apple", "cherry", "pear", "mango", "banana", "grape", "kiwi", "orange", "pineapple"]

列表 B:

B = [{"offset": 0, "xx": 789}, {"offset": 3, "xx": 921}, {"offset": 6, "xx": 89}]

思路是使用 B 中每个项目的偏移量作为结果数组中的 "xx" 值的索引偏移量。
例如,这将是预期结果:

C = [
{"fruit": "apple", "xx": 789},
{"fruit": "cherry", "xx": 789},
{"fruit": "pear", "xx": 789},
{"fruit": "mango", "xx": 921},
{"fruit": "banana", "xx": 921},
{"fruit": "grape", "xx": 921},
{"fruit": "kiwi", "xx": 89},
{"fruit": "orange", "xx": 89},
{"fruit": "pineapple", "xx": 89}
]

例如,B[0] 的 "offset" 为 0,这意味着 C 的索引 >= 0 将具有 "xx" 值等于 B[0]['xx']。然后,B[0]['offset'] 为 3,将为索引 >= 3 的 C 项目设置新的 "xx" 值,依此类推。

我可以使用数据框和 pandas 实现类似的结果。但由于 pandas 库相当庞大,我被要求在不使用 pandas 的情况下完成。

英文:

I have two lists.

List A :

A = ["apple","cherry","pear","mango","banana","grape","kiwi","orange","pineapple"]

List B :

B = [{"offset":0, "xx":789},{"offset":3, "xx":921},{"offset":6, "xx":89}]

The idea is to use the offset from each item in B as an index offset for setting the xx values in our results array.
For instance, this would be the expected result:

C=[
{"fruit":"apple","xx":789},
{"fruit":"cherry","xx":789},
{"fruit":"pear","xx":789},
{"fruit":"mango","xx":921},
{"fruit":"banana","xx":921},
{"fruit":"grape","xx":921},
{"fruit":"kiwi","xx":89},
{"fruit":"orange","xx":89},
{"fruit":"pineapple","xx":89},
]

For example, B[0] has "offset" of 0. this means that C of index >= 0 will have an "xx" value of B[0]['xx']. Then we have B[0]['offset'] of 3 that will set new "xx" values to the C items with index >= 3 and so on.

I am able to acheive a similar result using a dataframes and pandas. But since pandas library is quite heavy, I am requested to do it without using pandas.

答案1

得分: 6

使用一个简单的循环如何?

# 将 B 重新格式化
dic = {d['offset']: d['xx'] for d in B}
# {0: 789, 3: 921, 6: 89}

C = []

v = None
for i, a in enumerate(A):
    v = dic.get(i, v)  # 如果达到阈值,更新值
    C.append({'fruit': a, 'xx': v})

print(C)

输出:

[{'fruit': 'apple', 'xx': 789},
 {'fruit': 'cherry', 'xx': 789},
 {'fruit': 'pear', 'xx': 789},
 {'fruit': 'mango', 'xx': 921},
 {'fruit': 'banana', 'xx': 921},
 {'fruit': 'grape', 'xx': 921},
 {'fruit': 'kiwi', 'xx': 89},
 {'fruit': 'orange', 'xx': 89},
 {'fruit': 'pineapple', 'xx': 89}]
英文:

What about using a simple loop?

# rework B in a better format
dic = {d['offset']:d['xx']  for d in B}
# {0: 789, 3: 921, 6: 89}

C = []

v = None
for i, a in enumerate(A):
    v = dic.get(i, v) # if we reached a threshold, update the value
    C.append({'fruit':a, 'xx': v})

print(C)

Output:

[{'fruit': 'apple', 'xx': 789},
 {'fruit': 'cherry', 'xx': 789},
 {'fruit': 'pear', 'xx': 789},
 {'fruit': 'mango', 'xx': 921},
 {'fruit': 'banana', 'xx': 921},
 {'fruit': 'grape', 'xx': 921},
 {'fruit': 'kiwi', 'xx': 89},
 {'fruit': 'orange', 'xx': 89},
 {'fruit': 'pineapple', 'xx': 89}]

答案2

得分: 1

如果需要使B的结构如此,你可以这样做:

A = ["apple", "cherry", "pear", "mango", "banana", "grape", "kiwi", "orange", "pineapple"]
B = [{"offset": 0, "xx": 789}, {"offset": 3, "xx": 921}, {"offset": 6, "xx": 89}]
C = []
B_iter = 0

for i, fruit in enumerate(A):
    # 检查是否不是最后一个元素并且下一个元素是新范围的开始
    if B[B_iter] != B[-1] and B[B_iter + 1]["offset"] == i:
        B_iter += 1
    C.append({"fruit": fruit, "xx": B[B_iter]["xx"]})

print(C)

输出:

[{'fruit': 'apple', 'xx': 789},
 {'fruit': 'cherry', 'xx': 789},
 {'fruit': 'pear', 'xx': 789},
 {'fruit': 'mango', 'xx': 921},
 {'fruit': 'banana', 'xx': 921},
 {'fruit': 'grape', 'xx': 921},
 {'fruit': 'kiwi', 'xx': 89},
 {'fruit': 'orange', 'xx': 89},
 {'fruit': 'pineapple', 'xx': 89}]
英文:

If the structure of B is required to be this way, you can do this:

A = ["apple","cherry","pear","mango","banana","grape","kiwi","orange","pineapple"]
B = [{"offset":0, "xx":789},{"offset":3, "xx":921},{"offset":6, "xx":89}]
C = []
B_iter = 0

for i, fruit in enumerate(A):
    # check if not the last element and next element is start of new range
    if B[B_iter] != B[-1] and B[B_iter+1]["offset"] == i:
        B_iter += 1
    C.append({"fruit": fruit, "xx": B[B_iter]["xx"]})

print(C)

Output:

[{'fruit': 'apple', 'xx': 789}, 
 {'fruit': 'cherry', 'xx': 789}, 
 {'fruit': 'pear', 'xx': 789}, 
 {'fruit': 'mango', 'xx': 921}, 
 {'fruit': 'banana', 'xx': 921}, 
 {'fruit': 'grape', 'xx': 921}, 
 {'fruit': 'kiwi', 'xx': 89}, 
 {'fruit': 'orange', 'xx': 89}, 
 {'fruit': 'pineapple', 'xx': 89}]

答案3

得分: 0

如果偏移量始终是3的倍数,您可以简单地进行整数除法来将实际索引映射到偏移量。

A = ["apple","cherry","pear","mango","banana","grape","kiwi","orange","pineapple"]
B = [{"offset": 0, "xx": 789}, {"offset": 3, "xx": 921}, {"offset": 6, "xx": 89}]
C = [{"fruit": fruit, "xx": B[int(idx/3)]["xx"]} for idx, fruit in enumerate(A)]

输出:

[{'fruit': 'apple', 'xx': 789},
 {'fruit': 'cherry', 'xx': 789},
 {'fruit': 'pear', 'xx': 789},
 {'fruit': 'mango', 'xx': 921},
 {'fruit': 'banana', 'xx': 921},
 {'fruit': 'grape', 'xx': 921},
 {'fruit': 'kiwi', 'xx': 89},
 {'fruit': 'orange', 'xx': 89},
 {'fruit': 'pineapple', 'xx': 89}]
英文:

If the offset is always multiple of 3 you can simply do integer division to map the actual index to offset.

A = ["apple","cherry","pear","mango","banana","grape","kiwi","orange","pineapple"]
B = [{"offset": 0, "xx": 789}, {"offset": 3, "xx": 921}, {"offset": 6, "xx": 89}]
C = [{"fruit": fruit,"xx": B[int(idx/3)]["xx"]} for idx, fruit in enumerate(A)]

Output:

[{'fruit': 'apple', 'xx': 789},
 {'fruit': 'cherry', 'xx': 789}, 
 {'fruit': 'pear', 'xx': 789}, 
 {'fruit': 'mango', 'xx': 921}, 
 {'fruit': 'banana', 'xx': 921},
 {'fruit': 'grape', 'xx': 921},
 {'fruit': 'kiwi', 'xx': 89}, 
 {'fruit': 'orange', 'xx': 89}, 
 {'fruit': 'pineapple', 'xx': 89}]

huangapple
  • 本文由 发表于 2023年2月16日 19:13:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定