使用 Python 从数组中查找重复项

Finding duplicates from array using Python

我是 Python 的新手,想知道是否有创建新的未重复用户列表的好方法。

我的问题是我有这样的问题

[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "987654321",
    "method": "PAYPAL",
    "first_name": "Leroy",
    "last_name": "Jenkins"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]

基本上,我需要在 userId 匹配时进行匹配,并在 "method": "CARD" 而不是 PAYPAL 时保留对象,然后再次重建基本相同的列表,但在用户同时拥有 CARD 和 PAYPAL

::编辑:: 用户可以只拥有 PAYPAL。如果它只有 PAYPAL,那么 return 那

需要示例输出

[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]
users = {}

for d in user_list:
    uid = d["userId"]
    # If user id not in users, we add it
    if uid not in users:
        users[uid] = d

    # Otherwise we check if the already recorded method was "PAYPAL", 
    # if so we overwrite it.
    elif users[uid]["method"] == "PAYPAL":
       users[uid] = d

# To convert dict we just created back to list:
user_list = list(users.values())
    

使用defaultdict:

from collections import defaultdict

newdata = defaultdict(dict)

for item in data:
    userid = newdata[item['userId']]
    if userid == {} and item['method'] == 'CARD':
        userid.update(item)

输出:

# newdata = list(newdata.values())
>>> newdata
[{'userId': '987654321',
  'method': 'CARD',
  'lastDigits': '1234',
  'type': 'mc',
  'first_name': 'Leroy',
  'last_name': 'Jenkins',
  'exp': '01/23'},
 {'userId': '123456789',
  'method': 'CARD',
  'lastDigits': '4567',
  'type': 'visa',
  'first_name': 'Joe',
  'last_name': 'Bloggs',
  'exp': '01/25'}]

如果我没有误解你的问题那么简单的过滤就可以解决问题,

user_ids = []
final_users = []
for user in users:
    user_ids.append(int(user['userId']))
    if user['userId'] not in user_ids and user['method'] != 'PAYPAL':
        final_users.append(user)
print(final_users)

工作代码: https://rextester.com/COBGVU63922

这将非常适合您。试试看

mylist=[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "987654321",
    "method": "PAYPAL",
    "first_name": "Leroy",
    "last_name": "Jenkins"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]
temp_list=[]
temp_id=[]
for x in mylist:
    if int(x['userId']) not in temp_id:
        temp_list.append(x)
        temp_id.append(int(x["userId"]))

print(temp_list)